简体   繁体   English

C#在“其中TEntity:类”上实现接口

[英]C# implement interface on “where TEntity : class”

I'm relatively new to .NET and I've stumbled on this particular issue: while following a tutorial on repository pattern, the definition of the class goes like this: 我是.NET的新手,我偶然发现了这个特定问题:在遵循有关存储库模式的教程时,该类的定义如下所示:

public class GenericRepository<TEntity> where TEntity : class { ...

That being said, this class is supposed to implement an interface. 话虽如此,该类应该实现一个接口。 Since I already used the : operator, how do I do that? 既然我已经使用过:运算符,该怎么做?

I tried going public class GenericRepository<TEntity> : IGenericRepository where TEntity : class { and also public class GenericRepository<TEntity> where TEntity : class : IGenericRepository { but it doesnt work 我尝试去public class GenericRepository<TEntity> : IGenericRepository where TEntity : class {以及public class GenericRepository<TEntity> where TEntity : class : IGenericRepository {但它不起作用

Since I already used the : operator, how do I do that? 既然我已经使用过:运算符,该怎么做?

: isn't an operator - you're just using it in the generic type constraint. :不是运算符-您只是在泛型类型约束中使用它。 You can still specify an interface to implement. 您仍然可以指定要实现的接口。

This should be fine: 这应该很好:

public class GenericRepository<TEntity> : IGenericRepository where TEntity : class

Or if IGenericRepository is a generic interface, you might want: 或者,如果IGenericRepository是通用接口,则可能需要:

public class GenericRepository<TEntity> : IGenericRepository<TEntity>
    where TEntity : class

使用逗号添加多个通用约束:

public class GenericRepository<TEntity> where TEntity : class, IGenericRepository {}

You'd say public class GenericRepository<TEntity> : BaseClass1, IInterface1, IInterface2, ... where TEntity : class { ... 您会说public class GenericRepository<TEntity> : BaseClass1, IInterface1, IInterface2, ... where TEntity : class { ...

The : you used refers to the generic type parameter needing to be a class (rather than a struct ), so you can add an additional " : ". 您使用的:表示泛型类型参数需要是一个class (而不是struct ),因此您可以添加一个额外的“ : ”。

public class GenericRepository<TEntity> : **IGenericRepository**<TEntity> where TEntity : class

or 要么

In my case all classes are inheriting from IdentityBaseClass thus my signature looks like: 在我的情况下,所有类都从IdentityBaseClass继承,因此我的签名看起来像:

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : IdentityBase

Having said that what it mean is my classes, whoever wants to use GenericRepository, must inherit from IdentityBase class. 话虽如此,这意味着我的类(无论谁想要使用GenericRepository)都必须继承IdentityBase类。

My IdentityBase class has got two properties. 我的IdentityBase类具有两个属性。

 public class IdentityBase
    {
        /// <summary>
        /// Gets or sets ID.
        /// </summary>
        [NonNegative]
        [DataMember(IsRequired = true)]
        public int ID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the unique identifier of a row; this is to do with replication in SQL.
        /// </summary>

        public Guid UniqueIdentifier
       { 
            get;
            set;
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM