Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#(DI情况)中的继承和派生类_C#_Asp.net_.net_Inheritance_Dependency Injection - Fatal编程技术网

C#(DI情况)中的继承和派生类

C#(DI情况)中的继承和派生类,c#,asp.net,.net,inheritance,dependency-injection,C#,Asp.net,.net,Inheritance,Dependency Injection,因此,我将遵循一个关于在C#中使用Unity进行依赖注入的教程。在本教程中,他们以存储库类为例来演示这个概念。尝试将其应用于一个示例项目时,我遇到了一个继承问题。所以我有 public interface IRepository<T> where T : class { List<T> GetAll(); T Get(int id); void Add(T entity); void SaveChanges(); } public

因此,我将遵循一个关于在C#中使用Unity进行依赖注入的教程。在本教程中,他们以存储库类为例来演示这个概念。尝试将其应用于一个示例项目时,我遇到了一个继承问题。所以我有

public interface IRepository<T> where T : class
{
    List<T> GetAll();

    T Get(int id);

    void Add(T entity);

    void SaveChanges();
}

public class Repository<T> : IRepository<T> where T : class
{
    private CoffeeMachineDbContext context = null;

    protected virtual DbSet<T> DbSet { get; set; }

    public Repository()
    {
        context = new CoffeeMachineDbContext();
        DbSet = context.Set<T>();
    }

    public Repository(CoffeeMachineDbContext context)
    {
        this.context = context;
    }

    public virtual List<T> GetAll()
    {
        return DbSet.ToList();
    }

    public virtual T Get(int id)
    {
        return DbSet.Find(id);
    }

    public virtual void Add(T entity)
    {
        DbSet.Add(entity);
    }

    public void SaveChanges()
    {
        context.SaveChanges();
    }
}
公共接口i假设,其中T:class
{
List GetAll();
T Get(int-id);
无效添加(T实体);
void SaveChanges();
}
公共类存储库:IRepository,其中T:class
{
private CoffeeMachineDbContext context=null;
受保护的虚拟DbSet DbSet{get;set;}
公共存储库()
{
context=新的CoffeeMachineDbContext();
DbSet=context.Set();
}
公共存储库(CoffeeMachineDbContext上下文)
{
this.context=上下文;
}
公共虚拟列表GetAll()
{
返回DbSet.ToList();
}
公共虚拟T Get(int id)
{
返回DbSet.Find(id);
}
公共虚拟空添加(T实体)
{
添加(实体);
}
公共void SaveChanges()
{
SaveChanges();
}
}
repository类实现接口和常用方法。 现在,为了能够按照前面的解释(或者至少按照我的理解)应用依赖项注入,我创建了一个名为iclientropository的新接口,该接口继承自IRepository,如下所示:

public interface IClientRepository : IRepository<Client>
{
    Order GetLastOrder(int id);
}
container.RegisterType<IClientRepository, ClientRepository>(new HierarchicalLifetimeManager());
公共接口IClientPository:IRepository
{
订单GetLastOrder(int id);
}
请注意,接口声明了一个新方法,该方法特定于客户端上下文

最后,IClientPository接口的实现是:

public class ClientRepository : Repository<Client>, IClientRepository
{
    /// <summary>
    /// Gets the client's last order
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Order GetLastOrder(int id)
    {
        Order lastOrder = null;
        Client client = DbSet.Find(id);
        if (client != null)
        {
            lastOrder = client.Orders.OrderByDescending(o => o.DateCreated).FirstOrDefault();
        }

        return lastOrder;
    }
public类ClientRepository:Repository,IClientRepository
{
/// 
///获取客户端的最后一个订单
/// 
/// 
/// 
公共秩序GetLastOrder(内部id)
{
订单lastOrder=null;
Client=DbSet.Find(id);
如果(客户端!=null)
{
lastOrder=client.Orders.OrderByDescending(o=>o.DateCreated).FirstOrDefault();
}
退回上次订单;
}
我不需要实现IRepository方法,因为它们在所有其他方法中都很常见

我面临的问题是,当我试图在unity容器中注册类型时,如下所示:

public interface IClientRepository : IRepository<Client>
{
    Order GetLastOrder(int id);
}
container.RegisterType<IClientRepository, ClientRepository>(new HierarchicalLifetimeManager());
container.RegisterType(新的层次结构CallifetimeManager());
我得到以下错误

类型“CoffeeMachine.Models.Repositories.ClientRepository”不能用作泛型类型或方法“UnityContainerExtensions.RegisterType”(IUnityContainer、LifetimeManager、params InjectionMember[])中的类型参数“TTo”“.没有从“CoffeeMachine.Models.Repositories.ClientRepository”到“CoffeeMachine.Models.Repositories.IClientRepository”的隐式引用转换。


有人知道我在这里做错了什么吗?

发生这个错误是因为您的类实际上没有实现
iclientpository


只有当接口的类实际实现该接口时,才能将实例强制转换为该接口。

为什么您的ClientRepository不实现IClientRepository?因为这样做时,我会得到错误:ClientRepository不实现GetAll(),get(int)它认为您需要基类和接口:
公共类ClientRepository:Repository,IClientRepository{…}
IClientRepository是IRepository。存储库是IRepository。ClientRepository是存储库。但根据您的代码,ClientRepository不是IClientRepository。就像狗是动物,猫是动物,拉布拉多猎犬是狗和动物,但拉布拉多猎犬不是猫一样。有实体规范正如中所解释的,像
IClientRepository
这样的信息库抽象是一个坏主意。这是我一开始的想法。但是,一旦我这样做,我就会得到错误:ClientRepository没有实现GetAll()、get(int)等。。。