Hi I would like to call method Foo after StructureMap creates instance of repository, so there is a method for that, called OnCreation.
x.For(typeof(IRepository<>))
.Use(typeof(Repository<>))
.OnCreation((ctx, instance) => { instance.Foo() });
But compiler of course can’t infer the type, so I tried to supply the generic type like this:
x.For(typeof(IRepository<>))
.Use(typeof(Repository<>))
.OnCreation<Repository<>>((ctx, instance) => { instance.Foo() });
This won’t compile, it would not even parse(Invalid expression term ‘>). I tried to build Action object by myself but with no luck. Then I found about InstanceInterceptor, so I’ve written a class, but I can’t figure how to plugin it in. There should be InterceptWith method, but it is not available for ConfiguredInstance which is result type of not generic Use method.
I know I can HACK that in many other ways but i would like to do it within StructureMap mapping.
Please help 🙂
If your
Foo()method does not require access to the generic type then you could abstract this into a different interface:Then to configure StructureMap:
Things get incredibly tricky with open generic types and I’ve often found that it’s easier to hand additional initialization logic off to a factory. Rather than injecting an
IRepository<T>, inject aIRepositoryFactory<T>that can perform any additional initialization you need (you could still injectIRepository<T>into your factory if you don’t like the static StructureMap reference):