jeudi 2 février 2017

Can constructor initialization be used for dependency injection instead of having DI containers?

here's an example of what i am talking about

public interface IService<T>
    where T : class
{
    List<T> GetAll();
    T GetById(object id);
    .......... other methods here
}

public class Service<T> : IService<T>
    where T : class
{
    ... implement interface here
}

public class ServiceClient
{
    private readonly IService<User> _service;

    public ServiceClient(IService<User> service)
    {
        _service = service;
    }

    public ServiceClient() : this(new Service<User>()){}
}

can someone tell me the difference between this and a Dependency Resolver? I normally Use SimpleInjector for Dependency Injection, I just want to know the benefits of the Container over doing the above..

Thanks

Aucun commentaire:

Enregistrer un commentaire