samedi 31 janvier 2015

Code re-Design due to changing requirement

I have a method "AddUser" that validates User DTO and saves the record to the Database.


The requirement to send an email after the user is created and add the record to the messaging queue for some other application to consume got added later on. Due to changes/updates in requirements there were several updates to the userservice class each time. The code to send an email and adding to the queue is in a separate class exposed via an interface.


To avoid making change to userservice class each time the requirement changes, I am thinking of re-designing the class:

1. Create a command object AddUserCommand that saves the record to the database and decorate it with "EmailManager" and "MessagingQueue" classes that dynamically assigns more responsibility to "AddUserCommand" object.

2. Create a XML file that has the name of the class and method to invoke for AddUser method. Read the xml file(read once in a static constructor) and invoke methods at runtime. This would obviously be done by reflection so would be expensive.

e.g. <method name="userservice">

<add methodname="sendemail" type="EmailManager"/>

</method>


My question: Is there any existing open source framework or any design pattern that can be used to best solve this problem.



UserService.cs class
public void AddUser(UserDTO)
{
userRepository.Add(UserBO);
emailManager.SendEmail(userBO);
messagingQueue.Add(userBO);
}

Aucun commentaire:

Enregistrer un commentaire