我正在将遗留数据库与Asp.Net Zero集成在一起。我在单独的Models类库项目中使用EntityFramework Reverse POCO Generator创建了模型类。我还将DbContext反向工程到一个单独的数据类库项目中。我想在我的存储库和工作单元中使用Data Onion框架。当我使用推荐的IOC容器Autofaq时,我的Test 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:
c# - 将CaSTLe.Windsor与SSW Data Onion一起用于Entity Framework 6-LMLPHP

更新以修复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将在单例中将组件注册为在您的UnitOfWork注册中添加LifestyleTranscientPerWebRequest

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

10-12 00:34