Autofac用的WebAPI&安培;业务层 [英] Autofac with WebApi & Business Layer

查看:257
本文介绍了Autofac用的WebAPI&安培;业务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的AutoFac,我试图用我用的WebAPI和合同业务层和它们各自的实施的新项目。

我已经写了IocConfiguration为的WebAPI和Global.asax的调用。

不过,对于我的业务逻辑,我怎么设置我所有的合同和实现与autofac?

我也通过一些在线教程,但我却无法找到任何有用的,如果有人有一个示例应用程序,链接,确实有帮助。

编辑:

AutoMapper个人资料。

 公共类CustomProfile:简介
{
    保护覆盖无效配置()
    {
        CreateMap< MyViewModel,为MyModel>()
            .ForMember(D => d.Id,S = GT; s.MapFrom(SRC => src.Id));
    }
}

编辑:

花在这几个时间长后,我想通了如何设置AutoMapper 4.2.1 AutoFac。显然,我是在AutoMapper 3.3.0使用ConfigurationStore但我升级到4.2.1轮廓登记改变了一点点。下面是我工作。

 公共类AutoMapperModule:模块
{
    保护覆盖无效负载(ContainerBuilder建设者)
    {
        Mapper.Initialize(CFG =>
        {
            cfg.AddProfile< MyProfile1>();
            cfg.AddProfile< MyProfile2>();
        });        base.Load(制造商);
    }
}


解决方案

如果您使用构造函数注入(和它`真是一个好主意)。
首先你需要的是通过添加到的NuGet添加引用Autofac.WebApi2装配。让我们觉得你的控制器都在不同的装配主机(Service.dll或somethink这样),那么你

服务
  项目与我们的所有控制器:

 公共类DependenyInitializer
{   公共静态只读DependenyInitializer实例=新DependenyInitializer();      私人DependenyInitializer()
        {
          VAR建设者=新ContainerBuilder();
          builder.RegisterModule< BusinessLayerModule>(); //注册已设置了该模块中所有的依赖
          builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
          this.Container = builder.Build();
        }公共集装箱的IContainer {搞定; }}

商务大图层

您`ll必须创建一个模块

 使用的System.Reflection;
  使用Autofac;
  使用DataAccessLayer;
  采用模块= Autofac.Module; 公共类BusinessLayerModule:模块
  {
     保护覆盖无效负载(ContainerBuilder建设者)
      {
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())AsImplementedInterfaces(); //链接所有clases与实现的接口(它它们映射1:1到彼此)
      }

主机(注册WebApiConfig.cs(HttpConfiguration配置))

  VAR容器= DependenyInitializer.Instance.Container;
  config.DependencyResolver =新AutofacWebApiDependencyResolver(容器);

在这里主要compexity是知道你需要Autofac.WebApi2和它`RegisterApiControllers。试试。

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.

I have written the IocConfiguration for webapi and invoke from global.asax.

However, for my Business Logic how do I setup all my contracts and implementations with 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.

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. Apparently I was using ConfigurationStore in AutoMapper 3.3.0 but I upgraded to 4.2.1 the profile registration changed a little bit. 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. Lets think that your controllers are in the different Assembly that the host (Service.dll or somethink like this) then you

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))

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

Main compexity here is knowing that you need Autofac.WebApi2 and it`s RegisterApiControllers. Try that.

这篇关于Autofac用的WebAPI&安培;业务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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