通过传递构造函数参数的ServiceLocator获得实例 [英] ServiceLocator get instance by passing construction parameter
问题描述
我怎样才能对象实例与服务定位器与下面的构造函数。产品code是用来初始化成员properties.For我已经在Global.asax文件中使用统一注册他们的所有其他构造函数参数构造函数的参数。
基本方式来获得对象实例,如果构造函数参数
引用类型VAR productSettingsRepo =
ServiceLocator.Current.GetInstance< ProductSettingsRepository>
();公共ProductSettingsRepository(ILogWriter日志写进程,
ISecurityFunctionRepository securityRepo,
IProductSettingsManager productSettingsManager,
IReferenceDataService referenceDataService,串
产品code)
{
_logWriter =日志写;
_securityRepo = securityRepo;
_productSettingsManager = productSettingsManager;
_referenceDataService = referenceDataService;
ProductInfo = GetProductData(产品code);
}
Service Locator模式的被认为是一种反模式,你可能要重构使用的依赖注入。
话虽如此,这里有一种方法可以解决你的问题,而无需重构:
创建一个工厂接口和类依赖于服务定位器来创建一个 ProductSettingsRepository
赋予了产品code这样的类:
公共接口IProductSettingsRepositoryFactory
{
ProductSettingsRepository创建(字符串产品code);
}公共类ProductSettingsRepositoryFactory:IProductSettingsRepositoryFactory
{
公共ProductSettingsRepository创建(字符串产品code)
{
返回新ProductSettingsRepository(
ServiceLocator.Current.GetInstance< ILogWriter>()
ServiceLocator.Current.GetInstance< ISecurityFunctionRepository>()
ServiceLocator.Current.GetInstance< IProductSettingsManager>()
ServiceLocator.Current.GetInstance< IReferenceDataService>()
产品code);
}
}
然后确保你注册这个厂,像这样的统一容器:
unity_container.RegisterType< IProductSettingsRepositoryFactory,ProductSettingsRepositoryFactory>();
然后,您可以使用它是这样的:
VAR厂= ServiceLocator.Current.GetInstance< IProductSettingsRepositoryFactory>();VAR productSettingsRepo = factory.Create(我的产品code);
How can i get object instance with service locator with the constructor below. ProductCode is the constructor parameter used to initialize member properties.For all other constructor parameters i have registered them using unity in global.asax file.
Basic way to get object instance if constructor parameters are
reference type
var productSettingsRepo =
ServiceLocator.Current.GetInstance<ProductSettingsRepository>
();
public ProductSettingsRepository(ILogWriter logWriter,
ISecurityFunctionRepository securityRepo,
IProductSettingsManager productSettingsManager,
IReferenceDataService referenceDataService, string
productCode)
{
_logWriter = logWriter;
_securityRepo = securityRepo;
_productSettingsManager = productSettingsManager;
_referenceDataService = referenceDataService;
ProductInfo = GetProductData(productCode);
}
The Service Locator pattern is considered an anti-pattern, you might want to refactor to use Dependency Injection.
Having said that, here is one way you can fix your problem without refactoring:
Create a factory interface and class that depends on the service locator to create a ProductSettingsRepository
class given the product code like this:
public interface IProductSettingsRepositoryFactory
{
ProductSettingsRepository Create(string productCode);
}
public class ProductSettingsRepositoryFactory : IProductSettingsRepositoryFactory
{
public ProductSettingsRepository Create(string productCode)
{
return new ProductSettingsRepository(
ServiceLocator.Current.GetInstance<ILogWriter>(),
ServiceLocator.Current.GetInstance<ISecurityFunctionRepository>(),
ServiceLocator.Current.GetInstance<IProductSettingsManager>(),
ServiceLocator.Current.GetInstance<IReferenceDataService>(),
productCode);
}
}
Then make sure that you register this factory with the unity container like this:
unity_container.RegisterType<IProductSettingsRepositoryFactory, ProductSettingsRepositoryFactory>();
Then, you can use it like this:
var factory = ServiceLocator.Current.GetInstance<IProductSettingsRepositoryFactory>();
var productSettingsRepo = factory.Create("my product code");
这篇关于通过传递构造函数参数的ServiceLocator获得实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!