0

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:

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

2
  • 1
    do you mean the generic parameter should implement an interface or the type itself ? Commented Feb 5, 2015 at 21:57
  • @Selman22 I assumed the latter ("this class is supposed to implement an interface"), but it's not clear. Commented Feb 5, 2015 at 22:05

4 Answers 4

5

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:

public class GenericRepository<TEntity> : IGenericRepository<TEntity>
    where TEntity : class
Sign up to request clarification or add additional context in comments.

1 Comment

Accepted this answer because the interface is also a generic and it was also confusing how to declare: ... : IGenericRepository<TEntity> where...
0

Use a comma to add multiple generic constraints:

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

Comments

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:

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.

My IdentityBase class has got two properties.

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

Comments

0

You'd say 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 ":".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.