Unity静态工厂扩展 [英] Unity Static Factory Extension
问题描述
我似乎在任何地方都找不到Microsoft.Practices.Unity.StaticFactory.dll.
I can't seem to find Microsoft.Practices.Unity.StaticFactory.dll anywhere.
还有另一种注册静态工厂的方法吗?
Is there another way of registering a static factory?
正在寻找这样的东西
container.RegisterFactory(()=> FooFactory.CreateFoo());
container.RegisterFactory(()=> FooFactory.CreateFoo());
推荐答案
StaticFactory.dll作为Unity 2.0的一部分被滚动到主程序集中.通常它足够有用,以至于我们不想强迫人们随身携带一个单独的DLL来获取它.
StaticFactory.dll was rolled into the main assembly as part of Unity 2.0. It was generally useful enough that we didn't want to force people to carry around a separate DLL just to get it.
这样,您仍然可以使用现有的API,只是不需要添加程序集引用.但是,我们已经淘汰了旧的API.可以添加该扩展名,但不执行任何操作,该扩展名已包含在容器中.现在,您可以通过以下方式在容器中注册工厂:
As such, you can still use the existing API, you just don't need to add the assembly reference. However, we've deprecated the old API. The extension can be added, but does nothing, it's already included in the container. And you can now register factories in the container by saying:
container.RegisterType<IFoo, Foo>(new InjectionFactory(c => new Foo());
其中c是解析实例的容器.还有一个选项也可以传递要解析的类型和名称.
where c is the container that's resolving the instance. There's also an option to pass in the type and name being resolved as well.
我们不赞成使用旧的API,因为它使用起来很尴尬,而且现在也不再是扩展了.
We deprecated the old API because it was very awkward to use and it's not an extension anymore anyway.
这篇关于Unity静态工厂扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!