How to implement generic GetById() where Id can be of various types

前端 未结 3 1393
眼角桃花
眼角桃花 2020-12-08 22:55

I am trying to implement a generic GetById(T id) method which will cater for types which may have differing ID types. In my example, I have an entity which has

相关标签:
3条回答
  • 2020-12-08 23:31
    public interface IEntity<TId> where TId : class
    {
        TId Id { get; set; }
    }
    

    The where TId : class constraint requires every implementation to have a Id which derives from object which is not true for value types like int.

    Thats what the error message tells you: The type 'int' must be a reference type in order to use it as parameter 'TId' in the generic type of method IEntity

    Just remove the constraint where TId : class from IEntity<TId>

    0 讨论(0)
  • 2020-12-08 23:32

    You should remove the constraint on TId from your Repository class

    public abstract class Repository<TEntity, TId> : IRepository<TEntity, TId>
    where TEntity : class, IEntity<TId>
    {
        public virtual TEntity GetById(TId id)
        {
            return context.Set<TEntity>().Find(id);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 23:48

    To your question:
    I am trying to implement a generic GetById(T id) method which will cater for types which may have differing ID types. In my example, I have an entity which has an ID of type int, and one of type string.

        public virtual TEntity GetById<TId>(TId id)
        {
            return context.Set<TEntity>().SingleOrDefault(x => x.Id == id);
        }
    

    For generic parameter, just make a generic method like above

    0 讨论(0)
提交回复
热议问题