0

I have a asp.net web application and it has few application events; say for example - Create User. One of my clients want a notification when a new user is created. So I have a dll specific to that client where I can put the logic for sending notification by registering to the application event onCreateUser.

Now I would like to know is there a better and generic way so that by just replacing this dll, for a different client, I can do something else instead of sending notification. Or by simply removing the dll I can turn this behavior off.

Edit: I know it is possible to do this by dynamically loading dlls applying reflection using Attributes just like PreApplicationStartMethodAttribute or using Interface and so on. But wanted to make sure I am not reinventing the wheel.

Mahesh V S
  • 552
  • 1
  • 8
  • 23
  • I would look in to dynamically loading the assembly. If you just delete/replace it, it would require an application restart to register the change. – Mike_G Apr 28 '17 at 12:50
  • Thanks Mike! By dynamically loading, do you mean searching for a specific Interface implementation or using any attributes? My initial thought that PreApplicationStartMethodAttribute will solve this; but not sure if I will be able to register my own events with that approach – Mahesh V S Apr 28 '17 at 12:53
  • My first inclination would be to load the dll dynamically then use reflection to call the methods needed. Here is a SO thread regarding something similar: http://stackoverflow.com/questions/1137781/c-sharp-correct-way-to-load-assembly-find-class-and-call-run-method but you can just search "C# dynamically load assembly" on google/bing and that should get you started – Mike_G Apr 28 '17 at 12:56
  • Mike, dynamic loading is my last resort; I can do that in a better way using Attributes. I can find classes with that attribute (may be even methods) and call them so that I can perform multiple actions in multiple dlls on an event. But I was asking for any other approach to solve this problem using something already in the framework than me reinventing the wheel – Mahesh V S Apr 28 '17 at 13:16

1 Answers1

0

I am assuming you are trying/have implemented the observer pattern for OOP? Because that is the pattern I would build my code from.

So pseudo-code:

 notification method{User createdUser}
{
   bool shouldNotify = dataHandler.getUserNotificationValue(createdUser);
   if(shouldNotify )
   {
       notifier.SendNotification();
   }
}

This would remove the requirement to even have a DLL pr client, just a db/xml file/etc. with a list of relations between clients who requires a notification.

Morten Bork
  • 1,413
  • 11
  • 23
  • I can not put that condition explicitly there in my original code. I am looking for a plug-able solution – Mahesh V S Apr 28 '17 at 12:49
  • If you cannot alter the original code at all, I am not sure how you would interface with the plugin. But perhaps you are looking for doing the validation against simply detecting if a DLL exists? http://stackoverflow.com/questions/2292578/check-if-a-dll-is-present-in-the-system – Morten Bork Apr 28 '17 at 12:54