在多个项目解决方案中使用autofac [英] Use autofac in multiple project solution
问题描述
我有大型的wpf应用程序.我用autofac简化了我的问题.假设我在创建约束器的地方有ViewModelLocator. ViewModelLocator在Company.WPF项目中,此项目引用Company.ViewModels项目.
I have large wpf application. I simplify my problem with autofac. Let say I have ViewModelLocator where I create contrainer. ViewModelLocator is in Company.WPF project, this project refers Company.ViewModels project.
var builder = new ContainerBuilder();
builder.RegisterType<MainWindowViewModel>().AsSelf().SingleInstance();
container = builder.Build();
问题:MainWindowViewModel需要Company.Services项目中的ICompanyService(我使用CI),不应从Company.WPF引用此项目. ICompanyService是公共的,并且在同一项目(Company.Services)中也是实现CompanyService,但它仅是内部的.
Problem: MainWindowViewModel needs ICompanyService (I use CI) which is in Company.Services project, this project should not be reference from Company.WPF. ICompanyService is public and in same project (Company.Services) is also implementation CompanyService, but it is only internal.
如何为这些设置Autofac?我通常使用Castel Wisndor,在这种情况下有安装程序,Autofac中是否也有类似选项?
How can I setup Autofac for these? I normally use castel Wisndor, there are installers for these situation, is similar option in Autofac too?
推荐答案
您正在寻找模块在autofac中.对于体系结构中的每个层,为该层添加一个新的autofac模块,在其中注册该层的类型.在您构建autofac容器的ViewModelLocator
中,您只需注册autofac模块,而不是直接注册所有类型.
You are looking for the concept of Modules in autofac. For each layer in your architecture you add a new autofac module for that layer, where you register the types of that layer. In your ViewModelLocator
, where you build your autofac container, you just register autofac modules instead of registering all types directly.
根据您的情况,可能会更详细:
In more detail and for your case this could look something like this:
在您的Company.Services
项目中:
In your Company.Services
project:
您添加一个具有以下内容的新模块ServicesModule
. :
You add a new module ServicesModule
with something like this. :
public class ServiceModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
// optional: chain ServiceModule with other modules for going deeper down in the architecture:
// builder.RegisterModule<DataModule>();
builder.RegisterType<CompanyService>().As<ICompanyService>();
// ... register more services for that layer
}
}
在您的Company.ViewModels
项目中:
In your Company.ViewModels
project:
您还将创建一个ViewModelModule
,在其中注册与ServiceModule
类似的所有ViewModel.
You also create a ViewModelModule
where you register all your ViewModels similar to the ServiceModule
.
public class ViewModelModule : Autofac.Module
{
protected override void Load(ContainerBuilder builder)
{
// in your ViewModelModule we register the ServiceModule
// because we are dependent on that module
// and we do not want to register all modules in the container directly
builder.RegisterModule<ServiceModule>();
builder.RegisterType<MainViewModel>().AsSelf().InSingletonScope();
// ... register more view models
}
}
在您的Company.Wpf
项目(ViewModelLocator
)中:
In your Company.Wpf
project (ViewModelLocator
):
var builder = new ContainerBuilder();
builder.RegisterModule<ViewModelModule>();
builder.Build();
请注意,由于我们在ViewModelModule
中注册了ServiceModule
,因此我们只需要直接在ContainerBuilder
中注册ViewModelModule
.这样的优点是不需要在Company.Wpf
项目中添加对Company.Service
项目的引用.
Note that since we registered the ServiceModule
within the ViewModelModule
, we just have to register the ViewModelModule
directly in the ContainerBuilder
. This has the advantage of not needing to add a reference to the Company.Service
project within the Company.Wpf
project.
这篇关于在多个项目解决方案中使用autofac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!