简体   繁体   English

在 ASP.Net Core 3+ 中使用 Autofac。 参数不在容器中或无法自动解析时 controller 注册的最佳实践

[英]Using Autofac in ASP.Net Core 3+. Best practices for controller registration when parameters aren't in container or cannot be resolved automatically

When working with asp.net core 3 and autofac, if your controller's constructor has some additional parameters, that can not be resolved automatically, there are two (known to me) ways to properly register such controller:使用 asp.net 核心 3 和 autofac 时,如果您的控制器的构造函数有一些无法自动解析的附加参数,则有两种(我知道)方法可以正确注册此类 controller:

  1. Use WithParameter() method.使用WithParameter()方法。
  2. Pass a delegate (or lambda expression) that creates an instance of the controller to the Register() method将创建 controller 实例的委托(或 lambda 表达式)传递给Register()方法

With all controllers in .net core being registered using InstancePerLifetimeScope() , which way would be a better practice and why?使用InstancePerLifetimeScope()注册 .net 内核中的所有控制器,哪种方式更好,为什么?

I think you caring about how to register singular and or multiple parameters automatically while using AutoFac Core in.NetCore 3x environment.我认为您关心如何在.NetCore 3x 环境中使用 AutoFac Core 时自动注册单个和/或多个参数。 In according this acrchitecture i think you have working on MediaTr and Mapster which is the best practice.根据这个架构,我认为您正在研究 MediaTr 和 Mapster,这是最佳实践。 The below code snippet is from my usage, maybe it can help to imagine下面的代码片段来自我的使用,也许它可以帮助想象

InProgram.cs: InProgram.cs:

var host = CreateHostBuilder(args).UseServiceProviderFactory(new AutofacServiceProviderFactory()).Build();

In Middleware, startup.cs:在中间件中,startup.cs:

private static void RegisterDomain(ContainerBuilder builder)
{
    var domainAssembly = typeof(ArticleDto).Assembly;

    builder.RegisterType<DesAlgorithm>().As<ICryptAlgorithm>().SingleInstance();
    builder.RegisterType<SHA1Algorithm>().As<IHashAlgorithm>().SingleInstance();
    builder.RegisterType<FastHash>().As<IHash64Algorithm>().SingleInstance();

    // CQRS
    builder.RegisterType<Mediator>().As<IMediator>().InstancePerLifetimeScope();
    builder.Register<SingleInstanceFactory>(ctx =>
    {
        var c = ctx.Resolve<IComponentContext>();
        return t => c.Resolve(t);
    });
    builder.Register<MultiInstanceFactory>(ctx =>
    {
        var c = ctx.Resolve<IComponentContext>();
        return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
    });

    Type handlerType = typeof(IHandler);
    builder.RegisterAssemblyTypes(domainAssembly, Assembly.GetExecutingAssembly())
         .Where(t => t.IsClass && !t.IsAbstract && handlerType.IsAssignableFrom(t))
         .AsImplementedInterfaces().PropertiesAutowired().InstancePerLifetimeScope();

    // Repositories
    Type dbContextType = typeof(IDbContext);
    Type repositoryType = typeof(GenericRepository);

    builder.RegisterAssemblyTypes(domainAssembly)
         .Where(t => t.IsClass && !t.IsAbstract && dbContextType.IsAssignableFrom(t))
         .AsSelf().InstancePerLifetimeScope();

    builder.RegisterAssemblyTypes(domainAssembly)
         .Where(t => t.IsClass && !t.IsAbstract && repositoryType.IsAssignableFrom(t))
         .AsImplementedInterfaces().InstancePerLifetimeScope();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM