Ejemplo n.º 1
0
        /// <summary>
        ///     Registrar um serviço com sua respectiva implementação.
        /// </summary>
        /// <param name="service">Serviço (interface).</param>
        /// <param name="implementation">Implementação (class).</param>
        /// <param name="lifetime">Tempo de vida.</param>
        public void Register(Type service, Type implementation, DependencyResolverLifeTime lifetime)
        {
            if (IsAlreadyRegistered(service, implementation, lifetime))
            {
                return;
            }

            switch (lifetime)
            {
            case DependencyResolverLifeTime.PerContainer:
                _container.ServiceCollection.AddSingleton(service, implementation);
                break;

            case DependencyResolverLifeTime.PerScope:
                _container.ServiceCollection.AddScoped(service, implementation);
                break;

            case DependencyResolverLifeTime.PerRequest:
                _container.ServiceCollection.AddTransient(service, implementation);
                break;

            default:
                throw new WrongArgumentException(
                          nameof(DependencyResolver),
                          nameof(Register),
                          nameof(lifetime));
            }

            _container.DisposeServiceProvider();
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Registrar um serviço com sua respectiva implementação.
 /// </summary>
 /// <typeparam name="TService">Serviço (interface).</typeparam>
 /// <typeparam name="TImplementation">Implementação (class).</typeparam>
 /// <param name="lifetime">Tempo de vida.</param>
 public void Register <TService, TImplementation>(
     DependencyResolverLifeTime lifetime = DependencyResolverLifeTime.PerContainer)
     where TService : class
     where TImplementation : class, TService
 {
     Register(typeof(TService), typeof(TImplementation), lifetime);
 }