简体   繁体   English

使用Autofac注册以Service结尾的所有内容

[英]Using Autofac registering everything that ends with Service

So Im using autofac in a MVC so my controllers can have there dependencies injected on there constructor, I have in my Global.asax I have the following snippet of code, which works. 所以我在MVC中使用autofac,所以我的控制器可以在那里注入构造函数,我有我的Global.asax我有以下代码片段,它的工作原理。

// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

builder.RegisterType<PurchaseOrderSearchService>().As<IPurchaseOrderSearchService>().WithParameter("context", new PurchaseOrderManagerContext());

// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

The problem is I don't want to do builder.RegisterType over and over again, for all my Services. 问题是我不想一遍又一遍地为我的所有服务做builder.RegisterType。 So how do I do that? 那我该怎么做?

I think the kind of thing I want is 我想我想要的是那种东西

builder.RegisterAssemblyTypes(foo)
   .Where(t => t.Name.EndsWith("Services"))
   .WithParameter("context", new PurchaseOrderManagerContext());

But no idea what foo should be. 但不知道foo应该是什么。 Or if RegisterAssemblyTypes is correct way. 或者如果RegisterAssemblyTypes是正确的方式。 I know coding by convention is the solution but not sure what the convention is. 我知道按惯例编码是解决方案,但不确定约定是什么。 All my services will end int word Service and will have interface 我的所有服务都将以字服务结束并具有界面

so FooService will have interface IFooService and BarService will have interface IBarService 所以FooService将具有接口IFooService,而BarService将具有接口IBarService

Should also point out that all the services live in a class library called PurchaseOrderManager.Service 还应该指出所有服务都存在于名为PurchaseOrderManager.Service的类库中

You're on the right track. 你走在正确的轨道上。 "Foo" should be the assembly containing the types to register - if you're using a single assembly then the following should work: “Foo”应该是包含要注册的类型的程序集 - 如果您使用的是单个程序集,则以下内容应该有效:

builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly)
    .Where(t => t.Name.EndsWith("Services"))
    .WithParameter("context", new PurchaseOrderManagerContext())
    .AsImplementedInterfaces();

The .AsImplementedInterfaces() is needed to register them as IFooService - without it, they would only be registered as FooService etc. .AsImplementedInterfaces()注册为IFooService需要.AsImplementedInterfaces() - 没有它,它们只会被注册为FooService等。

If your classes live in a separate assembly, I would normally recommend you define an autofac module within that assembly: 如果您的类位于单独的程序集中,我通常建议您在该程序集中定义一个autofac模块:

public class ServiceModule : Module 
{
    protected override void Load(ContainerBuilder builder)
    {
        // "ThisAssembly" means "any types in the same assembly as the module"
        builder.RegisterAssemblyTypes(ThisAssembly)
            .Where(....
    }
}

Then register this in your web application: 然后在您的Web应用程序中注册:

builder.RegisterModule<PurchaseOrderManager.Service.ServiceModule>();

Alternatively, use my original suggestion but explicitly specify the assembly containing the services: 或者,使用我的原始建议,但明确指定包含服务的程序集:

builder.RegisterAssemblyTypes(typeof(PurchaseOrderManager.Service.FooService).Assembly)
    .Where(t => t.Name.EndsWith("Services"))
    .WithParameter("context", new PurchaseOrderManagerContext())
    .AsImplementedInterfaces();

You just need to pick any class which exists in that assembly. 您只需要选择该程序集中存在的任何类。

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

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