0

我是新来的体系结构,我正在学习和设计应用程序的过程中端到端。我有以下体系结构,并使用Autofac来管理对象创建。管理UnitOfWork的AutoFac对象创建

enter image description here

所有BusinessObject的合同已在启动时的WebAPI设置,即能启动其实我所有的autofac配置/模块的唯一启动。

我使用UnitOfWork/Repository模式,它超出了我的业务层,我不想在我的WebAPi中引用UnitOfWork,但是我无法启动UnitOfWork。

有人可以给我一些什么应该是我的架构/设计/ autofac unitofwork实施的投入?

+1

你应该在驱动程序项目的所有直接引用( web api),否则你不能申请ioc,因为你必须在一个地方管理生命。 –

+0

@ErkanDemirel感谢您的评论。当然,我明白,但是,这并不意味着泄漏的架构,在web api中引用了Repository? – Immortal

+0

[Ioc/DI - 为什么必须引用入口应用程序中的所有图层/程序集?](http://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to -reference-all-layers-assemblies-in-entry-application) –

回答

1

在App_start中注册web项目特定的依赖关系(控制器等)。在BL层的静态方法,登记工作,仓库等单位,当所有网络的依赖正在注册如下调用在App_start这种静态方法:

//App_Start (web project) 
var builder = new ContainerBuilder(); 
var config = GlobalConfiguration.Configuration; 
MyProject.BusinessLayer.RegisterDependancies.Register(builder); <-- Register Unit of Work here in static BL method 
builder.RegisterControllers(typeof(MvcApplication).Assembly); 
builder.RegisterApiControllers(typeof(MvcApplication).Assembly); 
builder.RegisterModule<AutofacWebTypesModule>(); 
builder.RegisterWebApiFilterProvider(config); 
builder.RegisterModule(new AutofacModules.AutoMapperModule()); 
builder.RegisterModule(new AutofacModules.Log4NetModule()); 

var container = builder.Build(); 

DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 
config.DependencyResolver = new AutofacWebApiDependencyResolver(container); 


//Static method in BL 
namespace MyProject.BusinessLayer 
{ 
    public static class RegisterDependancies 
    { 
     public static void Register(ContainerBuilder builder) 
     { 
      builder.RegisterType<MyContext>().As<IDataContextAsync>().InstancePerLifetimeScope(); 
      builder.RegisterType<UnitOfWork>().As<IUnitOfWorkAsync>().InstancePerLifetimeScope(); 
      builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepositoryAsync<>)).InstancePerLifetimeScope(); 
      builder.RegisterAssemblyTypes(typeof(BusinessService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerLifetimeScope(); 
     } 
    } 
} 
+0

你是一个绝对的传奇人物:) – Immortal

+0

@immortal这是你想要的,但是这完全是在扼杀IoC逻辑。 –

+0

@ErkanDemirel我现在没有更好的方法。除非你可以建议。 – Immortal