简体   繁体   English

使用WebApi和业务层进行Autofac

[英]Autofac with WebApi & Business Layer

I am very new to AutoFac and am trying to use it for my new project with WebApi and Business Layer with contracts and their respective implementations. 我是AutoFac的新手,我正在尝试将它用于我的新项目,包括WebApi和Business Layer以及合同及其各自的实现。

I have written the IocConfiguration for webapi and invoke from global.asax. 我已经编写了webapi的IocConfiguration并从global.asax调用。

However, for my Business Logic how do I setup all my contracts and implementations with autofac? 但是,对于我的Business Logic,如何使用autofac设置所有合同和实现?

I did go through some tutorials online but however I could not find anything helpful, If someone has a sample app, links that really helps. 我确实在线浏览了一些教程但是我找不到任何有用的东西,如果有人有一个示例应用程序,链接真的有帮助。

Edit: 编辑:

AutoMapper profile. AutoMapper配置文件。

public class CustomProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<MyViewModel, MyModel>()
            .ForMember(d => d.Id, s => s.MapFrom(src => src.Id));
    }
}

Edit: 编辑:

After few long hours spent on this I figured out how to setup AutoMapper 4.2.1 with AutoFac. 经过几个小时的花费,我想出了如何使用AutoFac设置AutoMapper 4.2.1。 Apparently I was using ConfigurationStore in AutoMapper 3.3.0 but I upgraded to 4.2.1 the profile registration changed a little bit. 显然我在AutoMapper 3.3.0中使用了ConfigurationStore但是我升级到了4.2.1,配置文件注册改变了一点。 Below is what worked for me. 以下是对我有用的。

public class AutoMapperModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AddProfile<MyProfile1>();
            cfg.AddProfile<MyProfile2>();
        });

        base.Load(builder);
    }
}

If you use constructor injection (and it`s really a good idea). 如果你使用构造函数注入(它真的是一个好主意)。 First you need is add to add reference to Autofac.WebApi2 assembly via Nuget. 首先,您需要添加以通过Nuget添加对Autofac.WebApi2程序集的引用。 Lets think that your controllers are in the different Assembly that the host (Service.dll or somethink like this) then you 让我们认为你的控制器在主机(Service.dll或类似的东西)的不同程序集中

Services Project with all our controllers: 与我们所有控制器的服务项目:

public class DependenyInitializer
{

   public static readonly DependenyInitializer Instance = new DependenyInitializer();

      private DependenyInitializer()
        {
          var builder = new ContainerBuilder();
          builder.RegisterModule<BusinessLayerModule>(); // register all dependencies that has been set up in that module
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
          this.Container = builder.Build();
        }

public IContainer Container { get; }

}

Buisness Layer 商业层

you`ll have to create a module 你必须创建一个模块

  using System.Reflection;
  using Autofac;
  using DataAccessLayer;
  using Module = Autofac.Module;

 public class BusinessLayerModule : Module
  {
     protected override void Load(ContainerBuilder builder)
      {
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces(); // that links all clases with the implemented interfaces (it they mapped 1:1 to each other)
      }

Hosting (WebApiConfig.cs in Register(HttpConfiguration config)) 托管 (注册中的WebApiConfig.cs(HttpConfiguration配置))

  var container = DependenyInitializer.Instance.Container;
  config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Main compexity here is knowing that you need Autofac.WebApi2 and it`s RegisterApiControllers. 这里的主要内容是知道你需要Autofac.WebApi2和它的RegisterApiControllers。 Try that. 试试吧。

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

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