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