ASP.NET Core中的依赖注入 [英] Dependency Injection in ASP.NET Core

查看:275
本文介绍了ASP.NET Core中的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Autofac中,您可以使用RegisterAssemblyTypes注册依赖项 因此您将能够执行类似的操作,是否有办法在DI中的.net Core

In Autofac you can register your dependencies with RegisterAssemblyTypes so you will be able todo something like this, is there a way to do the somthing similar in the build in DI for .net Core

builder.RegisterAssemblyTypes(Assembly.Load("SomeProject.Data"))
    .Where(t => t.Name.EndsWith("Repository"))
    .AsImplementedInterfaces()
    .InstancePerLifetimeScope();

这就是我要注册的内容

LeadService.cs

LeadService.cs

public class LeadService : ILeadService
{
    private readonly ILeadTransDetailRepository _leadTransDetailRepository;

    public LeadService(ILeadTransDetailRepository leadTransDetailRepository)
    {
        _leadTransDetailRepository = leadTransDetailRepository;
    }
}

LeadTransDetailRepository.cs

LeadTransDetailRepository.cs

public class LeadTransDetailRepository : RepositoryBase<LeadTransDetail>, 
    ILeadTransDetailRepository
{
    public LeadTransDetailRepository(IDatabaseFactory databaseFactory) 
        : base(databaseFactory) { }
}

public interface ILeadTransDetailRepository : IRepository<LeadTransDetail> { }

这就是我当时尝试注册的方式,但是我不知道如何注册存储库 Startup.cs

This is how i am trying to Regisyer then, but i cant figure out how to register the repositories Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddTransient<ILeadService, LeadService>();

    //not sure how to register the repositories
    services.Add(new ServiceDescriptor(typeof(ILeadTransDetailRepository),
        typeof(IRepository<>), ServiceLifetime.Transient));

    services.Add(new ServiceDescriptor(typeof(IDatabaseFactory),
        typeof(DatabaseFactory), ServiceLifetime.Transient));
    services.AddTransient<DbContext>(_ => new DataContext(
        this.Configuration["Data:DefaultConnection:ConnectionString"]));
}

推荐答案

使用ASP.NET Core Dependency Injection/IoC容器没有开箱即用的方法,但这是设计使然"的.

There is no out of the box way to do it with ASP.NET Core Dependency Injection/IoC container, but it's "by design".

ASP.NET IoC容器/DI是添加DI功能的简便方法,它是将其他IoC容器框架内置到ASP.NET Core应用程序中的基础.

ASP.NET IoC Container/DI is meant to be an easy way to add DI functionality and works as a base for other IoC Container frameworks to be built into ASP.NET Core apps.

话虽这么说,它支持简单的场景(注册,尝试使用具有满足依赖关系和范围依赖关系的大多数参数的第一个构造函数),但是缺少诸如自动注册或装饰器支持之类的高级场景.

That being said it supports simple scenarios (registrations, trying to use the first constructor with most parameters that fulfills the dependencies and scoped dependencies), but it lacks advanced scenarios like auto-registration or decorator support.

要使用此功能,您将必须使用第三方库和/或第三方IoC容器(AutoFac,StructureMap等).它们仍然可以插入IServiceCollection中,您以前的注册仍然可以使用,但是您可以在上面获得更多功能.

For this features you'll have to use 3rd party libraries and/or 3rd party IoC container (AutoFac, StructureMap etc.). They can still be plugged into the IServiceCollection your your previous registrations will still work, but you get additional features on top of it.

这篇关于ASP.NET Core中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆