gpt4 book ai didi

c# - 将 CaSTLe.Windsor 与 SSW Data Onion 一起用于 Entity Framework 6

转载 作者:行者123 更新时间:2023-11-30 18:20:17 27 4
gpt4 key购买 nike

我正在努力将遗留数据库与 Asp.Net Zero 集成.我使用 EntityFramework Reverse POCO Generator 创建了模型类在一个单独的模型类库项目中。我还将 DbContext 反向工程到一个单独的数据类库项目中。我想使用 Data Onion我的存储库和工作单元的框架。当我使用推荐的 IOC 容器时 Autofaq我的测试 Winform 应用程序工作正常。

但是,Web 项目利用了 Castle.Windsor . 我不确定如何进行接线。

我正在创建一个名为 ClientDesktopContainer 的新容器:

internal class ClientDesktopContainer : WindsorContainer
{
public ClientDesktopContainer()
{
RegisterComponents();
}

private void RegisterComponents()
{
var connectionstring = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;

// Data Onion
Component.For<IDbContextFactory>().ImplementedBy<DbContextFactory>()
.DependsOn(new DbContextConfig(connectionstring, typeof(MyDbContext), new MigrateToLatestVersion(new Seeder())));
Component.For<IDbContextScope>().ImplementedBy<DbContextScope>();
Component.For<IDbContextScopeFactory>().ImplementedBy<DbContextScopeFactory>();
Component.For<IAmbientDbContextLocator>().ImplementedBy<AmbientDbContextLocator>();
Component.For<IDbContextReadOnlyScope>().ImplementedBy<DbContextReadOnlyScope>();

// Data Onion Unit of Work
Component.For<IRepositoryLocator>().ImplementedBy<RepositoryLocator>();
// Component.For<IRepositoryResolver>().ImplementedBy<CastleWindsorRepositoryResolver>();
Component.For<IUnitOfWorkFactory>().ImplementedBy<UnitOfWorkFactory>();
Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>();
Component.For<IReadOnlyUnitOfWork>().ImplementedBy<IReadOnlyUnitOfWork>();

// Custom
Component.For<IRepository<Enrollment>>()
.ImplementedBy<BaseRepository<Enrollment, MyDbContext>>();
}

我的应用调用代码是Program:

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
IoC.Initialize(new ClientDesktopContainer());

var dbContextScopeFactor = IoC.Resolve<IDbContextScopeFactory>();
using (var dbReadOnly = dbContextScopeFactor.CreateReadOnly())
{
var context = dbReadOnly.DbContexts.Get<MyDbContext>();

var individuals = context.Enrollments.ToList();
foreach (var individual in individuals)
{
// do stuff
}
}

Application.Run(new ViewMain());
}
}

我创建了一个静态IOC:

public static class IoC
{
private static IWindsorContainer _container;

public static void Initialize(IWindsorContainer container)
{
_container = container;
}

public static T Resolve<T>()
{
try
{
return _container.Resolve<T>();
}
catch
{
throw;
}
}
}

Data Onion 文档提到为 IRepositoryResolver 注册自定义解析器。

我创建了一个CaSTLeWindsorRepositoryResolver:

public class CastleWindsorRepositoryResolver : IRepositoryResolver
{
public IRepository<TEntity> Resolve<TEntity>() where TEntity : class
{
// TODO: Resolve wire-up goes here
throw new System.NotImplementedException();
}
}

我收到一个 ComponentNotFoundExpection: Exception

已更新以修复 DbContextFactory 的构造函数参数(到 RegisterComponents 方法):

   var dbContextConfig = new DbContextConfig[]
{
new DbContextConfig(
connectionString,
typeof(MyDbContext),
new MigrateToLatestVersion(new Seeder())
)
};

// Data Onion
Register(Component.For<IDbContextFactory>().ImplementedBy<DbContextFactory>()
.DependsOn(Dependency.OnValue<DbContextConfig[]>(dbContextConfig)));

最佳答案

添加对 Register 的调用:

internal class ClientDesktopContainer : WindsorContainer
{
public ClientDesktopContainer()
{
RegisterComponents();
}

private void RegisterComponents()
{
var connectionstring = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;

/* HERE CALL TO REGISTER: */
this.Register(

// Data Onion
Component.For<IDbContextFactory>().ImplementedBy<DbContextFactory>()
.DependsOn(new DbContextConfig(connectionstring, typeof(MyDbContext), new MigrateToLatestVersion(new Seeder()))),
Component.For<IDbContextScope>().ImplementedBy<DbContextScope>(),
Component.For<IDbContextScopeFactory>().ImplementedBy<DbContextScopeFactory>(),
Component.For<IAmbientDbContextLocator>().ImplementedBy<AmbientDbContextLocator>(),
Component.For<IDbContextReadOnlyScope>().ImplementedBy<DbContextReadOnlyScope>(),

// Data Onion Unit of Work
Component.For<IRepositoryLocator>().ImplementedBy<RepositoryLocator>(),
// Component.For<IRepositoryResolver>().ImplementedBy<CastleWindsorRepositoryResolver>(),
Component.For<IUnitOfWorkFactory>().ImplementedBy<UnitOfWorkFactory>(),
Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>(),
Component.For<IReadOnlyUnitOfWork>().ImplementedBy<IReadOnlyUnitOfWork>(),

// Custom
Component.For<IRepository<Enrollment>>()
.ImplementedBy<BaseRepository<Enrollment, MyDbContext>>() );
}

如果没有 Register,您只是创建注册对象,实际上没有将类型放入容器中。另一件可能有帮助的事情是,默认情况下,CaSTLe 会将组件注册为 单例,将 LifestyleTranscientPerWebRequest 添加到您的 UnitOfWork 注册中。

关于c# - 将 CaSTLe.Windsor 与 SSW Data Onion 一起用于 Entity Framework 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37462471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com