{ ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal; //Int32 LogedInID = Convert.ToInt32(principal.Claims.FirstOrDefault(t => t.Type == "LogedInID").Value); string usertype = principal.Claims.FirstOrDefault(t => t.Type.Contains("role")).Value.ToString(); ReleaseNoteViewModel ReleaseNote = new ReleaseNoteViewModel(); AdjusterReleaseNoteViewModel AdjReleaseNote = new AdjusterReleaseNoteViewModel(); var ReleaseVersionvw = db.ReleaseVersions.AsNoTracking().Where(x => x.ReleaseVersionID == ID).OrderBy(x => x.ReleaseVersionName); try { #region Adjuster's ReleaseNote Data if (usertype == "Adjuster") { AdjReleaseNote.AdjReleaseView = ReleaseVersionvw.Select(x => new AdjusterReleaseNoteModel { ReleaseVersionID = x.ReleaseVersionID, ReleaseVersionName = x.ReleaseVersionName, IsCurrentVersion = x.IsCurrentVersion, Adjuster = db.UserLoginProfiles.Where(a => a.UserLoginProfileID == 2).Select(s => new ReleaseItemModel { Improvement = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 2 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList(), Feature = s.ReleaseNotes.Where(a => a.ReleaseItemTypeID == 1 && a.ReleaseVersionID == ID).OrderBy(a => a.ReleaseNoteDescription).Select(a => new ReleaseNotesModel { ReleaseNoteID = a.ReleaseNoteID, ReleaseNoteHeader = a.ReleaseNoteHeader, ReleaseNoteDescription = a.ReleaseNoteDescription }).ToList() }).FirstOrDefault(), }).ToList
here there is repitition of code is there a way to optimize it?
Mohammad HussainPosted Sep 18, 2023, 10:58 AM
Basit NisarPosted Dec 14, 2023, 7:27 AM
i am very much thank ful for all the help you provided
Prasad RaveendranPosted Sep 20, 2023, 7:05 PM
Here's another alternative improvement using a factory pattern to create release note views for different user types. This approach further reduces code duplication and allows for easy extensibility:
In this alternative improvement:
We define an
IReleaseNoteViewFactoryinterface that provides aCreatemethod to generate release note views.We implement separate factory classes (
AdjusterReleaseNoteViewFactoryandCatStaffReleaseNoteViewFactory) for each user type. You can easily add more factory classes for other user types.We use a factory method (
GetReleaseNoteViewFactory) to get the appropriate factory instance based on the user type.The factory method allows for easy extensibility by adding more cases for different user types and their corresponding factories.
The main code block remains concise and easy to read, as it delegates the release note generation to the appropriate factory based on the user type.
Prasad RaveendranPosted Sep 20, 2023, 6:49 PM
Another alternative to improve the code further is by using a more data-driven approach. You can maintain a configuration or mapping that associates user types with their respective
userLoginProfileIDvalues and then use that mapping to generate the release note data dynamically. Here's an example of how you can do this:In this alternative approach:
We use a
UserTypeToProfileIdMappingdictionary to map user types to their respectiveuserLoginProfileIDvalues.Instead of using conditional statements, we use the dictionary to look up the
userLoginProfileIDbased on the user type.This approach makes it easy to add more user types and their corresponding
userLoginProfileIDvalues without modifying the code logic.It provides a more flexible and data-driven way to handle different user types and their associated profiles.
Remember to update the
UserTypeToProfileIdMappingdictionary with any additional user types as needed.Cr BhargaviPosted Sep 20, 2023, 10:15 AM
Nitin KumarPosted Sep 19, 2023, 9:15 AM
Please note that you'll need to replace
SpecificExceptionTypeit with the actual exception type that you want to catch specifically.This code maintains the structure of your original code but uses methods to reduce code duplication, meaningful variable names, and proper exception handling.
Amit MohantyPosted Sep 19, 2023, 6:17 AM
It seems there is a lot of repeated code for different user types (Adjuster, CatStaff, etc.). You can consolidate similar code into reusable methods or a data structure.
Helper method to retrieve ReleaseNotes for a specific UserLoginProfileID and ReleaseVersionID:
Ali BenchaabanPosted Sep 18, 2023, 10:32 AM