温莎城堡不是 creating/exposing 基地 class

Castle Windsor not creating/exposing Base class

我在使用 Entity Framework DbContext Typed base class.

的 Repository 项目中有一些 Repository classes

基础Class

public class RepositoryBase<TC> : IDisposable
    where TC : DbContext, new()
{

    public virtual T Get<T>(...) where T : class
    {
        ...
    }

    // ... additional code
}

员工回购接口

namespace Insight.Repository.Interfaces
{
    public interface IEmployeeRepository
    {
        Result CreateEmployee(Employee employee);
        // ... additional code
    }
}

员工回购

namespace Insight.Repository.Repositories
{
    public class EmployeeRepository : RepositoryBase<InsightContext>, IEmployeeRepository
    {
        public Result CreateEmployee(Employee employee)
        {
            return Save(employee);
        }
    }
    // ... additional code
}

我正在使用基于 Repository 项目中的接口的安装程序将 Repo classes 添加到 Asp.Mvc 网站中的 Windsor 容器。此时我已经查看了容器,我正在获取我的三个存储库。

public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Classes.FromAssemblyNamed("Insight.Repository")
           .Where(Component.IsInNamespace("Insight.Repository.Repositories"))
            .WithService.DefaultInterfaces()
            .LifestyleTransient());
    }

我试过如下注册 DbContext 但我得到了相同的结果。

container.Register(Component.For<DbContext>()
            .ImplementedBy<InsightContext>()
            .LifestyleTransient());

然后,在我的 ApiController 中,我使用 属性 注入来实例化 Repo class 但我无法访问任何基础 class 函数。

public class EmployeeController : ApiController
{
    public IEmployeeRepository EmployeeRepo { get; set; }

    [Route("GetEmployee")]
    public void Get()
    {
        var newEmployeeRepo = new EmployeeRepository();
    }
}

EmployeeRepo 无法访问基础 class 中的 Get() 函数,但 newEmployeeRepo 可以。

这是我使用 Castle.Windsor 的第一个项目,我显然缺少某种注册步骤,但可以解决。

如果有人能指出正确的方向,非常感谢。

编辑:使用 tym32167 提供的 Linqpad 代码进行测试

void Main()
{
    var container = new WindsorContainer();

    var type = typeof(II);
    container.Register(Classes.FromAssembly(type.Assembly)
       .Where(Component.IsInNamespace(type.Namespace))
        .WithService.AllInterfaces()
        .LifestyleTransient());

    var cls = container.Resolve<II>().Dump();
    cls.GetFirstName().Dump();
    cls.GetLastName().Dump();
}

public interface II
{
    string GetFirstName();
}

public class MyClass : II
{
    public string GetFirstName()
    {
    return "First Name";
    }

    public string GetLastName()
    {
    return "Last Name";
    }
}

代码在这一行中断:

cls.GetLastName().Dump();

这是预期的行为吗?

尝试替换

.WithService.DefaultInterfaces()

.WithService.AllInterfaces()

作为示例(刚刚用 linqpad 检查)

void Main()
{   
    var container = new WindsorContainer();

    var type = typeof(II);
    container.Register(Classes.FromAssembly(type.Assembly)
           .Where(Component.IsInNamespace(type.Namespace))
            .WithService.AllInterfaces()
            .LifestyleTransient());

    container.Resolve<II>().Dump();
}

public interface II
{
}

public class MyCl : II
{
}

要访问基础 class 上的方法,您需要有一个基础接口:)

public interface IBaseRepo<T>
{
     T Get(int id);
     // ...
}

public interface IEmployeeRepo : IBaseRepo<Employee>
{
     Employee GetByFirstName(string name);
     // ...
}

注意子接口是如何继承基接口的。因此,任何实现 IEmployeeRepo 的类型都保证它也实现了 IBaseRepo<Employee>,并且当您注入一个类型时,您也可以访问基本接口上的方法。

事实上,您在基础 class 中实现了基础仓库,您的具体员工仓库继承了这一事实,这只是客户端代码不必关心的实现细节 - 现在它不需要关心't!