Domanda

I have a class that has some methods, two of them (ADD and UPDATE) want to be generic.

Here is my class:

public class CatalogRepository : ICatalogRepository
    {
        public CatalogRepository(DbContext dbContext)
        {
            if (dbContext == null)
                throw new ArgumentNullException("dbContext");
            DbContext = dbContext;
        }

        private DbContext DbContext { get; set; }

        #region Generic ADD and UPDATE

        public void Add<T>(T entity) where T : DbSet
        {
            DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
            if (dbEntityEntry.State != System.Data.Entity.EntityState.Detached)
            {
                dbEntityEntry.State = System.Data.Entity.EntityState.Added;
            }
            else
            {
                DbContext.Set<T>().Add(entity);
            }
        }

        public void Update<T>(T entity) where T : DbSet
        {
            DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
            if (dbEntityEntry.State == System.Data.Entity.EntityState.Detached)
            {
                DbContext.Set<T>().Attach(entity);
            }
            dbEntityEntry.State = System.Data.Entity.EntityState.Modified;
        }

        #endregion

        #region SetupSensor


        public IEnumerable<SetupSensor> GetSetupSensors(string masterEntity)
        {
            return DbContext.Set<SetupSensor>().Where(c => c.MasterEntity == masterEntity).ToList();
        }

        public IEnumerable<SetupSensor> ReadOnlySetupSensors(string masterEntity)
        {
            return DbContext.Set<SetupSensor>().AsNoTracking().Where(c => c.MasterEntity == masterEntity).ToList();
        }

        public SetupSensor GetSetupSensor(int sensorId)
        {
            return DbContext.Set<SetupSensor>().Where(c => c.SensorId == sensorId).FirstOrDefault();
        }


        #endregion
}

Here is the Interface Implementation:

public interface ICatalogRepository
    {
        SetupSensor GetSetupSensor(int sensorId);
        IEnumerable<SetupSensor> GetSetupSensors(string masterEntity);
        void Add<T>(T entity);
        void Update<T>(T entity);
    }

When I build I get the following error on the Two Generic Methods:

The constraints for type parameter 'T' of method 'CatalogRepository.Add<T>(T)' must match the constraints for type parameter 'T' of interface method 'ICatalogRepository.Add<T>(T)'. Consider using an explicit interface implementation instead.

Any clue on how to deal with this?

È stato utile?

Soluzione 2

In your implementation you do this:

public void Add<T>(T entity) where T : DbSet
{ … }

While your interface specifies this:

void Add<T>(T entity);

So, essentially, you need to make the constraints (the where part) identical for both sides. In your case, as you need the DbSet constaint for the implementation, you should add it to the interface:

void Add<T>(T entity) where T : DbSet;

Altri suggerimenti

Well, the error is pretty self-explanatory. When implementing an interface, you must implement all its members exactly as they are defined. Since you've introduced additional generic constraints in the implementation that are not present in the interface, implementation does not match the interface.

There are two ways to fix this: either add the constraints to the interface, or remove them from the implementation.

As a sidenote, you may want to think about making the whole interface generic, that is, to declare it like this:

// you may or may not want to have the constraint here
public interface ICatalogRepository<T> where T : DbSet
{
    // sensor methods
    void Add(T entity);
    void Update(T entity);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top