ServiceLocator get instance by passing construction parameter

孤街浪徒 提交于 2020-01-03 15:34:17

问题


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 the 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);
      }

回答1:


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");


来源:https://stackoverflow.com/questions/30721678/servicelocator-get-instance-by-passing-construction-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!