在windowsform中使用单元工作和存储库模式的DI使用autofac [英] DI with unitofwork and repository patterns in windowsform use autofac
问题描述
我在构建项目WindowsForm应用程序依赖注入时遇到问题。这是我在Program.cs文件中的代码。
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
//注册Web API控制器。
//builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterType< UnitOfWork>()。< IUnitOfWork>()。InstancePerDependency();
builder.RegisterType< DbFactory>()。< IDbFactory>()。InstancePerDependency();
builder.RegisterType< DITestDbContext>()。AsSelf()。InstancePerDependency();
//builder.Register(c => app.GetDataProtectionProvider())。InstancePerRequest();
//存储库
builder.RegisterAssemblyTypes(typeof(ProductCategoryRepository).Assembly)
.Where(t => t.Name.EndsWith(Repository))
.AsImplementedInterfaces()。InstancePerDependency();
//服务
builder.RegisterAssemblyTypes(typeof(ProductCategoryService).Assembly)
.Where(t => t.Name.EndsWith(Service))
.AsImplementedInterfaces()。InstancePerDependency();
Autofac.IContainer container = builder.Build();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(container.Resolve< Form1>());
这是我在Form1中的代码。 cs
private IProductCategoryService productCategoryService;
private IUnitOfWork unitOfWork;
public Form1(IProductCategoryService productCategoryService,IUnitOfWork unitOfWork)
{
this.productCategoryService = productCategoryService;
this.unitOfWork = unitOfWork;
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
LoadProductCategory();
}
private void LoadProductCategory()
{
var data = productCategoryService.GetAll();
gridControl1.DataSource = data;
}
我收到错误
< blockquote class =quote>
'在激活特定注册期间发生错误。有关详细信息,请参阅内部异常注册:Activator = Form1(ReflectionActivator),Services = [Presentation.Form1],Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime,Sharing = None,Ownership = OwnedByLifetimeScope'
Quote:NoConstructorsFoundException:找不到类型为DITest.Data.Infrastructure.UnitOfWork的可访问构造函数。
我尝试了什么:
任何人都可以帮我解决这个问题?谢谢!
我解决了这个问题。我创建UnitOfWork文件时犯了一个错误。它的构造函数必须是public public。
I have problem when I build project WindowsForm application Dependency Injection. Here this is my code in Program.cs file.
var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()); // Register your Web API controllers. //builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerDependency(); builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerDependency(); builder.RegisterType<DITestDbContext>().AsSelf().InstancePerDependency(); //builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest(); // Repositories builder.RegisterAssemblyTypes(typeof(ProductCategoryRepository).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces().InstancePerDependency(); // Services builder.RegisterAssemblyTypes(typeof(ProductCategoryService).Assembly) .Where(t => t.Name.EndsWith("Service")) .AsImplementedInterfaces().InstancePerDependency(); Autofac.IContainer container = builder.Build(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(container.Resolve<Form1>());
And here this is my code in Form1.cs
private IProductCategoryService productCategoryService; private IUnitOfWork unitOfWork; public Form1(IProductCategoryService productCategoryService, IUnitOfWork unitOfWork) { this.productCategoryService = productCategoryService; this.unitOfWork = unitOfWork; InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); LoadProductCategory(); } private void LoadProductCategory() { var data = productCategoryService.GetAll(); gridControl1.DataSource = data; }
And I get the error
Quote:'An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Form1 (ReflectionActivator), Services = [Presentation.Form1], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope'
Quote:NoConstructorsFoundException: No accessible constructors were found for the type 'DITest.Data.Infrastructure.UnitOfWork'.
What I have tried:
Anyone can help me how to fix this? Thanks you!
I fixed that. I made a mistake when I create UnitOfWork file. constructor of it must be create public.
这篇关于在windowsform中使用单元工作和存储库模式的DI使用autofac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!