c#实现“where TEntity: class”接口

时间:2022-09-02 09:19:47

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?

也就是说,这个类应该实现一个接口。既然我已经使用了:operator,我该怎么做呢?

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

我试着去公共类GenericRepository

4 个解决方案

#1


2  

Since I already used the : operator, how do I do that?

既然我已经使用了:operator,我该怎么做呢?

: 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

#2


0  

Use a comma to add multiple generic constraints:

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

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

#3


0  

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;
        }

#4


0  

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

您可能会说public类GenericRepository 接口:baseclass1,>

The : you used refers to the generic type parameter needing to be a class (rather than a struct), so you can add an additional ":".

您使用的:是指需要成为类(而不是结构)的泛型类型参数,因此您可以添加一个附加的“:”。

#1


2  

Since I already used the : operator, how do I do that?

既然我已经使用了:operator,我该怎么做呢?

: 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

#2


0  

Use a comma to add multiple generic constraints:

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

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

#3


0  

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;
        }

#4


0  

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

您可能会说public类GenericRepository 接口:baseclass1,>

The : you used refers to the generic type parameter needing to be a class (rather than a struct), so you can add an additional ":".

您使用的:是指需要成为类(而不是结构)的泛型类型参数,因此您可以添加一个附加的“:”。