如何在Castle Windsor中向非通用IUnitOfWork注册通用UnitOfWork ?] >> 这是我的代码: public interface IUnitOfWork : IDisposable { IRepository<TEntity> GetRepository<TEntity>() where TEntity : class; void Save(); } public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new() { private readonly IDbContext _ctx; private readonly Dictionary<Type, object> _repositories; private bool _disposed; ................... EmployeeService.cs public class EmployeeService : EntityService<Employee>, IEmployeeService { readonly IUnitOfWork _unitOfWork; readonly IRepository<Employee> _repository; ........ 当我打电话时在控制台应用程序中: var employeeService = Injector.Instance.Resolve<IEmployeeService>(); 我收到以下错误消息: Castle.Windsor.dll中发生了'Castle.MicroKernel.Handlers.GenericHandlerTypeMismatchException类型的未处理异常”附加信息:类型EfContext.DAL.IDbContext不满足实现类型EfContext.DAL.UnitOfWork1 of component 'EfContext.DAL.UnitOfWork1'的一般约束。这可能是所使用的IGenericImplementationMatchingStrategy(EfContext.DependencyInjection.UseStringGenericStrategy)中的错误。 public class ConsoleAppInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store) { container.Register(Component.For(typeof(IUnitOfWork)).ImplementedBy(typeof(UnitOfWork<>), new UseStringGenericStrategy()).LifestyleTransient()); } } public class UseStringGenericStrategy : IGenericImplementationMatchingStrategy { public Type[] GetGenericArguments(ComponentModel model, CreationContext context) { if (context.RequestedType == typeof(IUnitOfWork)) { var res = new[] { typeof(object) }; return res; } return null; } } 这是我的代码:公共接口IUnitOfWork:IDisposable {IRepository GetRepository (),其中TEntity:类; void Save(); }公共类UnitOfWork 我已将注册行更改为:已解决我的问题, //寄存器container.Register(Component.For(typeof(IUnitOfWork))。ImplementedBy(typeof(UnitOfWork),新的UseTypeGenericStrategy())。LifestyleTransient());

问题描述 投票:0回答:1

这是我的代码:

public interface IUnitOfWork : IDisposable
{
    IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;

    void Save();
}




  public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
  {
    private readonly IDbContext _ctx;
    private readonly Dictionary<Type, object> _repositories;
    private bool _disposed;
...................

EmployeeService.cs

public class EmployeeService : EntityService<Employee>, IEmployeeService
{
    readonly IUnitOfWork _unitOfWork;
    readonly IRepository<Employee> _repository;
........

当我打电话时在控制台应用程序中:

var employeeService = Injector.Instance.Resolve<IEmployeeService>();

我收到以下错误消息:

Castle.Windsor.dll中发生了'Castle.MicroKernel.Handlers.GenericHandlerTypeMismatchException类型的未处理异常”附加信息:类型EfContext.DAL.IDbContext不满足实现类型EfContext.DAL.UnitOfWork1 of component 'EfContext.DAL.UnitOfWork1'的一般约束。这可能是所使用的IGenericImplementationMatchingStrategy(EfContext.DependencyInjection.UseStringGenericStrategy)中的错误。

public class ConsoleAppInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {


        container.Register(Component.For(typeof(IUnitOfWork)).ImplementedBy(typeof(UnitOfWork<>), new UseStringGenericStrategy()).LifestyleTransient());

    }
}

    public class UseStringGenericStrategy : IGenericImplementationMatchingStrategy
    {
        public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
        {
            if (context.RequestedType == typeof(IUnitOfWork))
            {
                var res = new[] { typeof(object) };
                return res;
            }
            return null;
        }
    }

这是我的代码:公共接口IUnitOfWork:IDisposable {IRepository GetRepository (),其中TEntity:类; void Save(); }公共类UnitOfWork <...>

castle-windsor castle
1个回答
0
投票

我已将注册行更改为:已解决我的问题,

//寄存器container.Register(Component.For(typeof(IUnitOfWork))。ImplementedBy(typeof(UnitOfWork),新的UseTypeGenericStrategy())。LifestyleTransient());

© www.soinside.com 2019 - 2024. All rights reserved.