Code: Detail

asp.net+core - (Portuguese)

Injeção de dependencia nativa do asp.net core

A versão Core ja possui sua propria injeção de dependencia que dispensaa utilização de componentes de terceiros como Ninject, SimpleInjector etc, este exemplo utiliza da Injeção Nativa do Asp.net Core

Last update: 14/07/2016
asp.net+core  Ioc     
 
N/A
N/A
N/A
N/A
 

    public class Startup
    {

        public void ConfigureServices(IServiceCollection services)
        {

            /*
             * .AddTransient - Cria uma nova instancia cada vez que for acessado
             * .AddScoped - Cria uma estancia por escopo de solicitacao ou seja 1 chamada 3 solicitacoes as 3 solicitações usam a mesma instancia
             * .AddSingleton - Cria uma estancia que será utilizada em todos os requests
             * .AddInstance - Similar ao AddSingleton PORÉM neste caso o desenvolvedor deverá criar manualmente a instancia no contrutor da Startup
             */

            services.AddScoped<IAppService, AppService>();
            services.AddScoped<IService, Service>();
            services.AddScoped<IRepository, Repository>();

            //Este trecho de código injeta a instancia o enterprise library na camada de Repositório.
            DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
            services.AddSingleton(typeof(Database), DatabaseFactory.CreateDatabase("DbConnection"));
        }
    }


    public class AppService : IAppService
    {
        private readonly IService _Service;

        public AppService(IService Service)
        {
            _Service = Service;
        }
    }


    public class Service : BaseService, IService
    {
        private readonly IRepository _Repository;

        public Service(IRepository Repository)
        {
            _Repository = Repository;
        }
    }



    public class Repository : BaseRepository, IRepository
    {

        private readonly Database _database;

        public Repository(Database database)
        {
            _database = database;
        }
    }

Source:
 
Users who have marked this routine as a favorite
 
 
 
The site ti4fun is not responsible for the content on sites for which you have external links

Articles, routines, tips, forums, blogs or any other content posted on ti4fun site is not tested and not validated, so you should test and validate any information collected on the ti4fun site before applying it to final use environment, such as example, production. the TI4FUN site is not responsible for quality or for any damages, direct, indirect or consequential, from use of any content posted by the authors in the site.

All content published on the ti4fun site is the responsibility of the author and do not necessarily express the views of the site ti4fun and its employees.