INotifyPropertyChanged, AOP, and Structure Map

46 views
Skip to first unread message

JB

unread,
Oct 26, 2009, 11:29:20 AM10/26/09
to structuremap-users
First of all.. I hate INotifyPropertyChanged. That being said, it is
an evil I am saddled with on this WPF project.

I am currently using Structure Map as my IoC and am loving it. I
wanted to use some of the AOP functionality and get rid of the pain of
INotifyPropertyChanged.

I brought in Castle's DynamicProxy and then used a SM TypeInterceptor
returning the generated proxy.

Everything worked great and I was doing a happy dance.

But then I found a problem. The ViewModels are being built twice. Once
by StructureMap and once by the ProxyGenerator. That makes total
sense, but now I am stuck not knowing how I can remedy the problem.

I looked briefly into IInstanceFactory but felt it was getting a bit
out of hand. I could continue down this path if its the right path to
take.

Any recommendations? I don't mind doing some heavy lifting, I just
want to know if what I want is even possible.

Ideally, I would like to intercept before the object is created. I can
just have some conventions on which constructor to use and then use SM
to resolve the parameters, etc.

In the end, I could always drop in the PostSharp solution to the
issue, but I would rather not use IL weaving.

And although I have some (probably unfounded) concerns using PostSharp
for INotifyPropertyChanged, if its the best solution, then its the
best solution.

Thanks!

Kyle Malloy

unread,
Oct 26, 2009, 6:00:25 PM10/26/09
to structuremap-users
It sounds like you may want to delegate the construction of an object
to another factory. This can be done utilizing a ConstructorInstance.
Here is an example configuration in a Registry.

this.ForRequestedType<YourType>()
.TheDefault.Is.ConstructedBy(context =>
{
var factory = context.GetInstance<IMyTypeFactory>();
return factory.Create<YourType>();
});

You can use IContext to resolve any of you dependencies. It is kind of
like a trimmed down version of IContainer except it contains extra
information about the BuildStack and BuildFrames.

Hopefully this helps.
~Kyle

JB

unread,
Oct 29, 2009, 11:50:15 AM10/29/09
to structuremap-users
Thanks for the response. I am using a different version of SM ... I
think. My syntax wasn't exactly the same.

My SM version is 2.5.3.0

But it did help point me in the right direction.

I ended up using an IBuildInterceptor and registering my ViewModels
with a convention using a TypeScanner.

I blogged about it here : http://www.theabsentmindedcoder.com/2009/10/aop-inotifypropertychanged-structuremap.html

I also put my solution on Git Hub here : http://github.com/RookieOne/StructureMapAopNotify

I am sure I will need future tweaking but the rough POC works. :D

Thanks again!
Reply all
Reply to author
Forward
0 new messages