简体   繁体   English

无法初始化 irepository<> 泛型

[英]Can not initialize irepository<> generic

I defined and implemented IRepository , IEntity code as below.我定义并实现了IRepositoryIEntity代码如下。

//define Repository
public interface IRepository {}
public interface IRepository<TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
{
    TEntity Insert(TEntity entity);
}
public interface IRepository<TEntity> : IRepository<TEntity, int> where TEntity : class, IEntity<int> { }
public class Repository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
{
    public TEntity Insert(TEntity entity)
    {
        return entity;
    }
}
public class Repository<TEntity> : Repository<TEntity, int> where TEntity : class, IEntity<int> { }
public interface IEntity<TPrimaryKey>
{
    TPrimaryKey Id { get; set; }
}

//define entity
public interface IEntity : IEntity<int> { }
public abstract class Entity : Entity<int>, IEntity { }
public abstract class Entity<TPrimaryKey> : IEntity<TPrimaryKey>
{
    public virtual TPrimaryKey Id { get; set; }
}

//customer
public class Customer : Entity
{
    public string Name { get; set; }
}

when i try to create initialize a Repository<customer>()当我尝试创建初始化Repository<customer>()

IRepository<Customer> repository = new Repository<Customer>();

I got this error:我收到此错误:

'Infrastructure.Repository.Repository' to 'Infrastructure.Repository.IRepository'. 'Infrastructure.Repository.Repository' 到 'Infrastructure.Repository.IRepository'。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少演员表?)

But when i use code above, it works.但是当我使用上面的代码时,它起作用了。

IRepository<Customer,int> repository = new Repository<Customer>();

i thought IRepository<customer,int> is equal to IRepository<customer> What is the problem when initial a repository like this:我认为IRepository<customer,int>等于IRepository<customer>初始化这样的存储库时有什么问题:

IRepository<Customer> repository = new Repository<Customer>();

As mentioned by @Reasurria in the comments, your Repository<TEntity> class does not inherit from IRepository<TEntity> so the compiler cannot perform an implicit conversion between them.正如@Reasurria 在评论中提到的,您的Repository<TEntity>类不是从IRepository<TEntity>继承的,因此编译器无法在它们之间执行隐式转换。 You need to update your class definition to include the missing interface:您需要更新类定义以包含缺少的接口:

public class Repository<TEntity> : Repository<TEntity, int>, IRepository<TEntity> where TEntity : class, IEntity<int> { }

I dont know if this can solve your problem: When i test your code like this我不知道这是否可以解决您的问题:当我像这样测试您的代码时

IRepository<Customer> repository =(IRepository<Customer>) new Repository<Customer>();

Casting to IRepository, the error has gone.投射到 IRepository,错误消失了。 Maybe the compiler don t know what to do because you have 2 declarations of Repository` class.也许编译器不t know what to do because you have 2 declarations of Repository` 类的t know what to do because you have 2 declarations of

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通用存储库 - IRepository <T>或IRepository - Generic repository - IRepository<T> or IRepository 如何以这种模式将EF POCO与通用IRepository分离? - How can I decouple the EF POCO from the generic IRepository in this pattern? 具有通用Factory模式的IRepository模式 - IRepository pattern with generic Factory pattern 我如何抽象IRepository <Invoice> 和IRepository <Estimate> 作为IRepository <SalesTransaction> ? - How can I abstract IRepository<Invoice> and IRepository<Estimate> as IRepository<SalesTransaction>? Autofac。 通过实体属性解析通用 IRepository - Autofac. Resolve Generic IRepository by Entity Attribute autofac打开通用iRepository <T> xml配置 - autofac open generic iRepository<T> xml configuration 如何对IRepository进行单元测试? - How can an IRepository be unit tested? 实体框架4.1和通用IRepository <T> 模式和温莎 - Entity Framework 4.1 and the Generic IRepository<T> Pattern and Windsor 使用通用类型&#39;System.Collections.Generic.List <T> &#39;在IRepository中需要1个类型参数 - Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments in IRepository 通过字符串初始化泛型类 - Initialize generic class by string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM