Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceLocator get instance by passing construction parameter

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);
      }
like image 906
user845405 Avatar asked Sep 27 '22 16:09

user845405


1 Answers

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");
like image 73
Yacoub Massad Avatar answered Oct 11 '22 15:10

Yacoub Massad