Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

dependency injection - Can Windsor's TypedFactoryFacility's implicit delegate factory registration be disabled?

We're using Windsor's typed factory facility and think it's lovely. We use the interface-based factories. However we'd like to disable some sub-sets of the delegate-based factories, specifically the implicitly registered factories. These are counter-intuitive, not because they are delegates, but because they are magically created, and can delay failure.

If we have a class which takes a delegate as a dependency

class X { public X(Func<int,IPrincipal> d); }

And then register it with a container which has had the typed factory facility registered.

container.Kernel.Register(Component.For<X>().ImplementedBy<X>())

I can resolve it without any trouble, which is counterintuitive at first, since no one told the container we had anything to say about Func<int,IPrincipal>.

var x = container.Resolve<X>();

And I don't run into failure until I try to actually use that implicitly created factory.

x.D(0); // no registration for IPrincipal

While this makes sense from a certain point of view, the fact that this factory is implicitly created is troublesome. It's a very container-aware behavior. People who write arbitrary classes will find it useful to parameterize their behavior via delegates, and as soon as we put them into the IoC container we run into this suprising behavior.

That said, there is one clever implicit factory that seems like it's worth keeping. Right now, Windsor will create a simple factory for dependencies of the form Func<T>, allowing the consumer of the dependency to delay actual creation. It might make sense in the 4.0 framework to change this to recognize Lazy<T> as a clear indication that you're just delaying construction of T, not attempting to access a factory which will implement an interesting policy.

Is there a clever switch available for configuring the TypedFactoryFacility or do we need to implement some new objects to get the behavior we want?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you really want to you can remove the DelegateFactory component from the container, which is what creates the delegate-based factories.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...