[英]How to create objects using a static factory method?
I know Unity can be configured to use a class' constructor to create an instance of a class (like below) but that's not what I want. 我知道Unity可以配置为使用类的构造函数来创建类的实例(如下所示),但这不是我想要的。
container.RegisterType<IAuthoringRepository, AuthoringRepository>();
I would like to configure Unity to use a factory method with the windows identity passed as a parameter (ie: RepositoryFactory.CreateAuthoringRepository(WindowsIdentity.GetCurrent())
) when resolving a type of IAuthoringRepository
. 我想在解析一种IAuthoringRepository
时,将Unity配置为使用一个工厂方法,将windows身份作为参数传递(即: RepositoryFactory.CreateAuthoringRepository(WindowsIdentity.GetCurrent())
)。 How do i do this? 我该怎么做呢?
One way is to have RepositoryFactory implement IRepositoryFactory, then register that. 一种方法是让RepositoryFactory实现IRepositoryFactory,然后注册它。 Resolved types can get a factory, then call its CreateAuthoringRepository method. 已解析的类型可以获取工厂,然后调用其CreateAuthoringRepository方法。 You could create an overload called CreateAuthoringRepositoryForCurrentIdentity if desired, or register an IIdentity dependency of the factory with Unity. 如果需要,您可以创建一个名为CreateAuthoringRepositoryForCurrentIdentity的重载,或者使用Unity注册工厂的IIdentity依赖项。
I'd probably just inject a factory and leave the CreateAuthoringRepository method as you have it, then have the clients pass WindowsIdentity.GetCurrent(). 我可能只是注入一个工厂并保留CreateAuthoringRepository方法,然后让客户端通过WindowsIdentity.GetCurrent()。 That way the identity is always fresh, and you can mock the factory for testing. 这样,身份总是很新鲜,你可以嘲笑工厂进行测试。
Alternately, you can specify a delegate with InjectionFactory: 或者,您可以使用InjectionFactory指定委托:
void Main()
{
using (var container = new UnityContainer())
{
container.RegisterType<IAuthoringRepository>(
new InjectionFactory(c => CreateAuthoringRepository()));
Console.WriteLine("debug - resolving model");
var model = container.Resolve<Model>();
}
}
public IAuthoringRepository CreateAuthoringRepository()
{
Console.WriteLine("debug - calling factory");
return new AuthoringRepository
{ Identity = WindowsIdentity.GetCurrent() };
}
public class Model
{
public Model(IAuthoringRepository repository)
{
Console.WriteLine(
"Constructing model with repository identity of "
+ repository.Identity);
}
}
public interface IAuthoringRepository
{
IIdentity Identity { get; }
}
public class AuthoringRepository : IAuthoringRepository
{
public IIdentity Identity { get; set; }
}
This prints: 这打印:
debug - resolving model debug - calling factory Constructing model with repository identity of System.Security.Principal.WindowsIdentity
That's for Unity 2.0. 这是针对Unity 2.0的。 With earlier versions, see StaticFactoryExtension . 对于早期版本,请参阅StaticFactoryExtension 。
Now method InjectionFactory is obsolete. 现在方法InjectionFactory已经过时了。 That's why it'd be better to use method RegisterFactory. 这就是为什么使用RegisterFactory方法会更好。 Below I am showing how the previous code changed. 下面我将展示以前的代码如何更改。 How you see I changed the method CreateAuthoringRepository. 你怎么看我改变了CreateAuthoringRepository方法。 Now it is the static method with one param IUnityContainer container 现在它是带有一个参数IUnityContainer容器的静态方法
void Main()
{
using (var container = new UnityContainer())
{
container.RegisterFactory<IAuthoringRepository>(CreateAuthoringRepository);
Console.WriteLine("debug - resolving model");
var model = container.Resolve<Model>();
}
}
public static IAuthoringRepository CreateAuthoringRepository(IUnityContainer container)
{
Console.WriteLine("debug - calling factory");
return new AuthoringRepository
{ Identity = WindowsIdentity.GetCurrent() };
}
public class Model
{
public Model(IAuthoringRepository repository)
{
Console.WriteLine(
"Constructing model with repository identity of "
+ repository.Identity);
}
}
public interface IAuthoringRepository
{
IIdentity Identity { get; }
}
public class AuthoringRepository : IAuthoringRepository
{
public IIdentity Identity { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.