Is it possible to implement dependency injection without using the service locator at the beginning of the application?

I am well acquainted with the concepts of a service locator and dependency injection, but there is one thing that confuses me all the time, that is, to implement dependency injection for an application, we must use some kind of service locator at the beginning. Please consider the following code, let's say we have a simple DAL class:

public class UserProviderSimple : IUserProvider
{
    public void CreateUser(User user)
    {
        //some code to user here
    }
}

And then in the Business Logig layer, we have a simple class that uses IUserProvider, which is introduced using the constructor insert:

public class UserServiceSimple : IUserService
{
    public IUserProvider UserProvider { get; set; }
    public UserServiceSimple(IUserProvider userProvider)
    {
        UserProvider = userProvider;
    }
    public void CreateUser(User user)
    {
        UserProvider.CreateUser(user);
    }
}

, , , , , , oneton :

public class ServiceLocator
    {
        private readonly UnityContainer _container;

        private static ServiceLocator _instance;

        public static ServiceLocator Instance()
        {
            if (_instance == null)
            {
                _instance = new ServiceLocator();
                return _instance;
            }
            return _instance;
        }

        private ServiceLocator()
        {
            _container = new UnityContainer();
            _container.RegisterType<IUserProvider, UserProviderSimple>();
            _container.RegisterType<IUserService, UserServiceSimple>();
        }

        public T Resolve<T>()
        {
            return _container.Resolve<T>();
        }
    }
    class Program
    {
        private static IUserService _userService;
        private static void ConfigureDependencies()
        {
            _userService = ServiceLocator.Instance().Resolve<IUserService();
        }

        static void Main(string[] args)
        {
            ConfigureDependencies();
        }
    }

, , - , , ( )?

+6
4

, DI ServiceLocator, Startup, Bootstrap ContainerWrapper, .

, ServiceLocator -, . Resolve<T>, , .

https://en.m.wikipedia.org/wiki/Service_locator_pattern

, DI , DI, , . , !

+3

, Locator. , -, , , , , . :

DI, , - .

+4

. , ServiceLocator (, - , , awsome, ).

, . , root, root (Program) .

public class Bootstrapper
{
    private readonly UnityContainer _container;

    private Bootstrapper()
    {
        _container = new UnityContainer();
    }

    public Program Intialize()
    {
        this.ConfigureDependencies(UnityContainer container);
        return this.GetCompositionRoot();
    }

    private void ConfigureDependencies()
    {
        _container.RegisterType<IUserProvider, UserProviderSimple>();
        _container.RegisterType<IUserService, UserServiceSimple>();
        _container.RegisterType<Program, Program>();
    }

    private Program GetCompositionRoot()
    {
        return _container.Resolve<Program>();
    }
}

public class Program
{
    public Program(IUserService userService)
    {
        _userService = userService ?? throw AgrumentNullExcpetion(nameof(userService));
    }

    static void Main(string[] args)
    {
        var program = new Bootstrapper().Initialize();
        program.Run();
    }

    public void Run()
    {
        // Do your work using the injected dependency _userService
        // and return (exit) when done.
    }
}
+1

, , -.

, , Service Locator - .

- . , .

, . , .

0
source

Source: https://habr.com/ru/post/1016854/


All Articles