管理 UnitOfWork 的 AutoFac 对象创建
Managing AutoFac object creation for UnitOfWork
我是架构新手,正在学习和设计端到端的应用程序。我有以下架构并且正在使用 Autofac 来管理对象创建。
所有 businessobject 合同都已在 webapi 启动时设置,这是唯一可以实际启动我所有 autofac 的启动 configurations/modules。
我使用 UnitOfWork/Repository 模式,它位于我的业务层之外,我不想在我的 WebAPI 中引用 UnitOfWork,但我无法启动 UnitOfWork。
有人可以就我的 architecture/design/autofac 工作单元实现提供一些意见吗?
在 App_start 中注册 Web 项目特定的依赖项(控制器等)。在 BL 层中有一个静态方法,用于注册工作单元、存储库等。在 App_start 中调用此静态方法,当所有 Web 依赖项正在注册时如下所示:
//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();
}
}
}
我是架构新手,正在学习和设计端到端的应用程序。我有以下架构并且正在使用 Autofac 来管理对象创建。
所有 businessobject 合同都已在 webapi 启动时设置,这是唯一可以实际启动我所有 autofac 的启动 configurations/modules。
我使用 UnitOfWork/Repository 模式,它位于我的业务层之外,我不想在我的 WebAPI 中引用 UnitOfWork,但我无法启动 UnitOfWork。
有人可以就我的 architecture/design/autofac 工作单元实现提供一些意见吗?
在 App_start 中注册 Web 项目特定的依赖项(控制器等)。在 BL 层中有一个静态方法,用于注册工作单元、存储库等。在 App_start 中调用此静态方法,当所有 Web 依赖项正在注册时如下所示:
//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();
}
}
}