Proxymus89
Proxymus89

Reputation: 15

Inject multiple services in constructor VS inject a single ServiceFactory in constructor

I would like a tip on what is the best approach between those 2 solutions:

using unity I register N services interfaces/classes

internal class Configurator
    {
        private static IUnityContainer container = new UnityContainer();

        internal static void Configure()
        {
            container.RegisterType<ICustomerService, CustomerService>();
            container.RegisterType<IPhoneService, PhoneService>();
            container.RegisterType<IUserService, UserService>();
            ...
        }

        internal static T Resolve<T>()
        {
            return container.Resolve<T>();
        }
    }

Where all interfaces implements IService

public interface ICustomerService : IService
public interface IPhoneService: IService
public interface IUserService: IService

I've also created a ServiceFactory class

public class ServiceFactory : IServiceFactory
    {
        public T GetService<T>() where T : IService
        {
            return Configurator.Resolve<T>();
        }
    }

now my doubt is:

SOLUTION 1:

public class TestViewModel
{
    private ICustomerService _customerService;

    private IPhoneService _phoneService;

    private IUserService _userService;

    public TestViewModel(ICustomerService customerService, IPhoneService phoneService, IUserService userService)
    {
        _customerService = customerService;
        _phoneService = phoneService;
        _userService = userService;
    }

SOLUTION 2:

public class TestViewModel
{
    private IServiceFactory _serviceFactory;

    private IUserService _userService;
    public IUserService UserService
    {
        get
        {
            if(_userService == null)
                _userService = _serviceFactory.GetService<IUserService>();
            return _userService;
        }
    }

    public MainWindowViewModel(IServiceFactory serviceFactory)
    {
        _serviceFactory = serviceFactory;
    }

Personally i prefer solution 2 because

1) if I must inject a lot of service in a single constructor I can inject only the factory

2) the services are initialized only when requested (in solution 1 they are all initialize in constructors also if the user will never call/use all of them)

Are you agree that solution 2 is better?are there any contraindications?I want to hear some of your opinion...

Upvotes: 0

Views: 1102

Answers (1)

Ivan Mladenov
Ivan Mladenov

Reputation: 1847

That ServiceFactory class basically acts as a Service Locator which is considered an anti pattern. Reason is that this way your service has way more power than it needs to. By declaring your dependencies in the constructor, you have strict guidelines what you are going to need and use in your class.

If you have too many constructor parameters, it could be a code smell, indicating that your class probably does more than it should and maybe violates the Single Responsibility Principle. It that case, you should think about refactoring that class into smaller subclasses each doing smaller tasks.

My take on the question is that the first solution is better

Upvotes: 1

Related Questions