Easy Business Rules with Extension Methods
Posted By: nicocrm on June 1st, 2009 in Uncategorized
No Gravatar

I just realized that I could use C# extension methods to avoid the whole “open architect, expand the little tree to find my entity, add the business rule definition, select the assembly, select the method from the assembly, save, copy comment from the C# project, save again, build, deploy, pray that the deployment actually worked” process.

I can declare my business rule like this:

public static void CalculateProcessTask(this ITicketActivity ticketActivity)

save as TicketRules.dll, and add this to the code to use it:

using TicketRules;

Another huge benefit is I can now use F12 to navigate to the definition. One less reason to open the Application Architect for – YAY! Sadly there is no way to do something like that for events, or for code snippet properties. But if you add the namespace used for the extension rules to the web.config/system.web/pages/namespaces element (see http://www.west-wind.com/WebLog/posts/2287.aspx for an example) you can use it in snippet actions.

It also opens the door to something that is a bit scary… it looks like those rules could be defined in App_Code (the folder that contains dynamically compiled code for ASP.NET). This would let us edit them without having to recompile… or restart the IIS app… mucho más rápido! But this prevents the code from being unit-tested, and makes it harder to manage, so I am not very likely to use it (business rules typically don’t need to be adjusted very often anyway, once they are unit-tested)


Leave a Reply