Sunday, August 13, 2006 10:12 PM Olaf Conijn

Solving cross-cutting concerns with DSL's and Enterprise Library

Since I am currently experimenting with both the dsl-toolkit and PostSharp (which is a .net postcompiler) I thought of the following:

 

An ELExceptionManagementLanguage.

 

This 'language' allows you to draw a diagram defining your exception policies and drop classes from your project in them (meaning that exceptions thrown in these classes should be handled according to that exception policy).

 

It currently looks like the following:



1) In the toolbox you can see a list of classes currently in your project (organized by namespace).
2) On the drawing surface you can see 2 Exception policies “DataAccessExceptionPolicy” and “BusinessLogicExceptionPolicy”, they contain the classes “QouteDAL” and “GetQouteAction”

 

 

Drawing is fun, but what to do with this view?

Well, using PostSharp I’ve created a little program that:

1)     Loads the dsl-file you created

2)     Opens up the assembly the classes are part of

3)     Modifies the assembly and inserts all the exceptionhandling code you never wrote a line of c# for!

 

The code for “GetQuoteAction” in this project looks like this:

 

namespace BusinessLogic

{

    public class GetQuoteAction

    {

        public Quote Execute(string qoute)

        {

            double value = QouteDAL.GetQouteFromDB(qoute);

            return new Quote(value);

        }

    }

}

Though, after compiling (and having the program run as a post-build step), the actual IL in it the output-assembly would look like the following in C#:



 

In case you where wondering:

yes – I’ll put the solution on my weblog as soon as I got my mind around howto deploy it
no – I haven’t tested it on complex scenario’s. The mangling of assembly instructions is done by postsharp, and I know Gael is eager on receiving feedback.
yes – Writing dsl’s is hard, but after writing a couple of diagram’s you sort-of get the hang of it
no – Loading classes from your project into your dsl’s toolbox isn’t a smart thing to do, the toolbox items are loaded when visual studio is started – which means you will have to restart VS after adding classes to your project in order to add them to the model
yes – The days of writing exception management code are over! :-)

Filed under: , ,

# re: Solving cross-cutting concerns with DSL's and Enterprise Library

Sunday, August 13, 2006 11:05 PM by Marc Jacobi

Nice! One thing I'm a little concerned about is the all or nothing try-catch blocks that end up in the methods. I compare it to sychronization attributes that lock at method level: nice and simple and fast, but sometimes not so optimal. You'd still be hand-coding try-catch-finally blocks with more complex logic, such as resource cleanup. Another case would be when you'd wrap an exception into a custom exception to give more contextual information to client.

# re: Solving cross-cutting concerns with DSL's and Enterprise Library

Monday, August 14, 2006 12:29 AM by Gael Fraiteur

That's a nice application of PostSharp. I'm glad to see that, even in an early stage, it is used!

I'm not familiar with EntLib but some months ago I've had a look specifically at the Exception Handling Block and I just found out how it was demanding to copy-paste exception handlers everywhere in the code. This issue is now nicely solved!

One suggestion related to exception handling and PostSharp:

For those who are interested in scholar algorithms, it is possible to write in PostSharp a plugin that would analyze exceptions that may be throws by methods, then eventually ensure that these exceptions are properly "declared" by method-level custom attributes. The academic part is to write a constraint-solving algorithm. If someone is interested, he may contact me.

# re: Solving cross-cutting concerns with DSL's and Enterprise Library

Monday, August 14, 2006 3:05 AM by Teun

Nice example of a true cross-cutting concern. I think Marc is right that you wouldn't always want to manage your exceptions this way, but in many cases it would be just fine. And the remaining code looks soooo vanilla.

# re: Solving cross-cutting concerns with DSL's and Enterprise Library

Monday, August 14, 2006 3:19 AM by Olaf Conijn

Marc, Teun,

I agree that not all exceptions should be handled in this fashion (you might be able to 'fix' FileNotFoundException's within your (base-)code), though at a logic boundary you might want EnterpriseLibrary (or a similar framework) to translate all unknown exception in something else -and- add context to the new exception from configuration.

Enterprise library does this behind the ExceptionPolicy.HandleException method.

for more infromation about the ExceptionHandling blog, see MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/EntLibJan2006_ExceptionHandlingAppBlock.asp

# Aspect oriented modeling

Monday, August 14, 2006 4:06 PM by ^(?:[^$]*)$ --Matches everything, captures nothing

After writing my previous post on a modeling-language for exception-management logic I ended up in a

# Very much alive

Saturday, January 13, 2007 9:32 AM by ^(?:[^$]*)$ --Matches everything, captures nothing

Because of the lack postings on my weblogs, below is a little heads-up on whatever I have been doing