配置 Unity 以解析构造函数参数和接口
configure Unity to resolve a constructor parameter and interface
我有一个 FaxService class,它接受两个构造函数参数。
public FaxService(string phone, IFaxProvider faxProvider)
Unity 如何配置为第一个参数发送字符串,第二个参数发送 IFaxProvider 实例?我意识到我可以注入另一个提供字符串的服务,但我正在寻找一个解决方案,我不必更改 FaxService 构造函数参数。
这是我目前所拥有的...
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterType<IFaxService, FaxService>(phone);
var fax = container.Resolve<IFaxService>();
}
}
public interface IFaxService { }
public interface IFaxProvider { }
public class FaxService : IFaxService
{
public FaxService(string phone, IFaxProvider faxProvider) { }
}
public class EFaxProvider : IFaxProvider { }
但它抛出...
Unity.Exceptions.ResolutionFailedException HResult=0x80131500
Message=Resolution of the dependency failed, type =
'ConsoleApp3.IFaxService', name = '(none)'. Exception occurred while:
while resolving.
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterType<IFaxService, FaxService>(new InjectionConstructor(phone, typeof(IFaxProvider)));
var fax = container.Resolve<IFaxService>();
I am looking for a solution where I don't have to change the FaxService constructor parameters.
如果不至少指定所有构造函数参数的类型,Unity 中不会内置任何内容来执行此操作。
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxService, FaxService>(new InjectionConstructor(phone, typeof(IFaxProvider)));
container.RegisterType<IFaxProvider, EFaxProvider>();
var fax = container.Resolve<IFaxService>();
每次构造函数更改时,参数列表都需要更改。
解决方法
如果您不想在 FaxService
的构造函数更改时更改您的 DI 注册,请构建一个抽象工厂以将 string/primitive 参数与服务分开。通过工厂的构造函数参数传递服务。通过 Build
方法参数传递字符串和原始参数类型。
public class FaxServiceFactory
{
private readonly IFaxProvider faxProvider;
public FaxServiceFactory(IFaxProvider faxProvider)
{
this.faxProvider = faxProvider ?? throw new ArgumentNullException(nameof(faxProvider));
}
public IFaxService Create(string phone)
{
return new FaxService(phone, this.faxProvider);
}
}
然后像这样注册:
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<FaxServiceFactory>();
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterInstance<IFaxService>(container.Resolve<FaxServiceFactory>().Create(phone));
var fax = container.Resolve<IFaxService>();
另一种可能性是使用允许您指定部分构造函数参数列表的 DI 容器(Autofac、Ninject、StructureMap 和 Castle Windsor 都开箱即用)。
我有一个 FaxService class,它接受两个构造函数参数。
public FaxService(string phone, IFaxProvider faxProvider)
Unity 如何配置为第一个参数发送字符串,第二个参数发送 IFaxProvider 实例?我意识到我可以注入另一个提供字符串的服务,但我正在寻找一个解决方案,我不必更改 FaxService 构造函数参数。
这是我目前所拥有的...
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterType<IFaxService, FaxService>(phone);
var fax = container.Resolve<IFaxService>();
}
}
public interface IFaxService { }
public interface IFaxProvider { }
public class FaxService : IFaxService
{
public FaxService(string phone, IFaxProvider faxProvider) { }
}
public class EFaxProvider : IFaxProvider { }
但它抛出...
Unity.Exceptions.ResolutionFailedException HResult=0x80131500
Message=Resolution of the dependency failed, type = 'ConsoleApp3.IFaxService', name = '(none)'. Exception occurred while: while resolving.
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterType<IFaxService, FaxService>(new InjectionConstructor(phone, typeof(IFaxProvider)));
var fax = container.Resolve<IFaxService>();
I am looking for a solution where I don't have to change the FaxService constructor parameters.
如果不至少指定所有构造函数参数的类型,Unity 中不会内置任何内容来执行此操作。
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxService, FaxService>(new InjectionConstructor(phone, typeof(IFaxProvider)));
container.RegisterType<IFaxProvider, EFaxProvider>();
var fax = container.Resolve<IFaxService>();
每次构造函数更改时,参数列表都需要更改。
解决方法
如果您不想在 FaxService
的构造函数更改时更改您的 DI 注册,请构建一个抽象工厂以将 string/primitive 参数与服务分开。通过工厂的构造函数参数传递服务。通过 Build
方法参数传递字符串和原始参数类型。
public class FaxServiceFactory
{
private readonly IFaxProvider faxProvider;
public FaxServiceFactory(IFaxProvider faxProvider)
{
this.faxProvider = faxProvider ?? throw new ArgumentNullException(nameof(faxProvider));
}
public IFaxService Create(string phone)
{
return new FaxService(phone, this.faxProvider);
}
}
然后像这样注册:
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<FaxServiceFactory>();
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterInstance<IFaxService>(container.Resolve<FaxServiceFactory>().Create(phone));
var fax = container.Resolve<IFaxService>();
另一种可能性是使用允许您指定部分构造函数参数列表的 DI 容器(Autofac、Ninject、StructureMap 和 Castle Windsor 都开箱即用)。