When you do code, please avoid declaring not read only variable as a private property for a class. For example:
If your user report bug, then when you want to search the root cause of the problem, will be challenging because it is very hard to check.
Suggestion
For maintainability purpose, always remember KISS (Keep It Simple Stupid) principle. So for solving that problem, this is how I do it:
internal class RoomReservationHelper { private readonly IPluginExecutionContext _context; private readonly IOrganizationService _service; private readonly IOrganizationServiceFactory _serviceFactory;</code> public RoomReservationHelper(IPluginExecutionContext context, IOrganizationServiceFactory serviceFactory, IOrganizationService service) { _context = context; _serviceFactory = serviceFactory; _service = service; } public void Execute() { var roomReservation = (Entity)_context.InputParameters["Target"]; var resvNo = roomReservation.Attributes["gent_name"].ToString(); var anotherRoomReservations = GetRelatedRoomReservation(roomReservation); } private Entity[] GetRelatedRoomReservation(Entity roomReservation) { var name = roomReservation.Attributes["gent_name"].ToString(); var number = .Attributes["gent_roomnumber"].ToString(); ... } }
The Changes?
- We define variable when it’s needed.
- We break 1 function to smaller function. Make it more clear, more readable.
- All the property of the class is readonly, so this make sure you’re class is thread safe.
*This post is locked for comments