Using structuremap for method interception

705 views
Skip to first unread message

Chris Marisic

unread,
Nov 25, 2009, 12:15:44 PM11/25/09
to structuremap-users
A while back I wrote code using PostSharp that let me create an
attribute that I called BusinessConversation that when I put that
attribute on a method it so would be have like

[BusinessConversation]
public void myMethod {
Does Stuff
}

That PostSharp would weave into the assembly that it would actually do

public void myMethod {

Start NHibernate session transaction

try {
Does original code

Commits nhibernate session transaction
}
catch()
{
Rolls back transaction
}
}

Is this possible SM or is it not possible due to the fact that this
can only be accomplished with weaving new IL into the assembly?

If you want to see actual source to my BusinessConversation for
context: http://code.assembla.com/MarisicDotNet/subversion/nodes/StructuredWeb/StructuredWeb/Common/BusinessConversation.cs

Chris Marisic

unread,
Nov 30, 2009, 10:05:00 AM11/30/09
to structuremap-users
Can anyone offer any insight if this is possible with StructureMap or
only with a pure AOP library that does weaving at the IL level?

Sam Dlg

unread,
Nov 30, 2009, 11:55:19 AM11/30/09
to structure...@googlegroups.com
Yes, this is completely possible.  I recently did this with SM & Castle's DynamicProxy 2.1.  Basically you use Sm's "EnrichWith" and create an interceptor in DP.  I can send a working code sample tomorrow.

On Mon, Nov 30, 2009 at 9:05 AM, Chris Marisic <ch...@marisic.com> wrote:
Can anyone offer any insight if this is possible with StructureMap or
only with a pure AOP library that does weaving at the IL level?

--

You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/structuremap-users?hl=en.



Chris Marisic

unread,
Dec 3, 2009, 11:45:36 AM12/3/09
to structuremap-users
Sam that would definitely be appreciated, you can email me at chris at
marisic dot com or you can attach it here.

On Nov 30, 11:55 am, Sam Dlg <sam....@gmail.com> wrote:
> Yes, this is completely possible.  I recently did this with SM & Castle's
> DynamicProxy 2.1.  Basically you use Sm's "EnrichWith" and create an
> interceptor in DP.  I can send a working code sample tomorrow.
>
> On Mon, Nov 30, 2009 at 9:05 AM, Chris Marisic <ch...@marisic.com> wrote:
> > Can anyone offer any insight if this is possible with StructureMap or
> > only with a pure AOP library that does weaving at the IL level?
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "structuremap-users" group.
> > To post to this group, send email to structure...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > structuremap-us...@googlegroups.com<structuremap-users%2Bunsu...@googlegroups.com>
> > .

Sam Dlg

unread,
Dec 3, 2009, 2:38:39 PM12/3/09
to structure...@googlegroups.com
Here's what I have:

var dynamicProxy = new ProxyGenerator();

            ObjectFactory.Initialize(x =>
                                         {
                                             x.Scan(y =>
                                                        {
:
:
x.ForRequestedType<IDenialImporter>()
                                                 .TheDefault.Is.OfConcreteType<DenialImporter>()
                                                 .EnrichWith(z => dynamicProxy.CreateInterfaceProxyWithTarget(z, new LogInterceptor()));
:
:

Then the LogInterceptor is: 

public class LogInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            LogMethodRuns(invocation);
        }

        private void LogMethodRuns(IInvocation invocation)
        {
            if (!ContainsLogMethodRunAttributes(invocation.Method)) return;

            var startTime = DateTime.Now;

            invocation.Proceed();

            var endTime = DateTime.Now;
            var duration = endTime.Subtract(startTime).TotalSeconds.ToString("N3");

            Console.WriteLine(string.Format("Duration of raw {0}(): {1}s", invocation.Method.Name, duration));
        }

        // TODO: Extend this in order to handle multiple custom attributes.
        private bool ContainsLogMethodRunAttributes(ICustomAttributeProvider methodInfo)
        {
            var attribute = methodInfo.GetCustomAttributes(typeof (LogMethod), true);

            if(attribute == null)
                return false;

            return attribute is LogMethod[];
        }
    }

To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.

Chris Marisic

unread,
Dec 3, 2009, 4:37:10 PM12/3/09
to structuremap-users
Sam, that looks fantastic and was exactly what I was trying to figure
out how to do without needing build time assembly weaving
> > <structuremap-users%2Bunsu...@googlegroups.com<structuremap-users%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages