dimanche 19 mai 2019

I have problem in implementing generic repository

I want to build generic repository to make it easy in implementing..now I want to make interface to used it in dependency injection in my domain service but I can't

I want to build generic repository to make it easy in implementing.I created generic abstract repository that get entity and its context.now I want to make interface to used it in dependency injection in my domain service

my generic repository:

 public abstract class Repository<T,K>:IRepository<T,K>
    {
        private Type t;
        private K _Context;
        private bool disposed = false;
        public Repository(K Context)
        {
            _Context = Context;
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _Context.Dispose();
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        public void Delete(object id)
        {
            T t = _Context.Set<T>().Find(id);
            _Context.Set<T>().Remove(t);
        }

        public T Get(object id)
        {
            return _Context.Set<T>().Find(id);
        }

        public IEnumerable<T> getList()
        {
            return _Context.Set<T>().ToList();
        }

        public void insert(T t)
        {
            _Context.Set<T>().Add(t);
        }

        public void Save()
        {
            _Context.SaveChanges();
        }

        public  void Update(T t)
        {
            _Context.Entry(t).State = EntityState.Modified;
        }
    }
}

my repository interface:

    public interface IRepository<T,K> where T : BaseEntity where K : BaseContext<K>
    {
        T Get(object id);
        IEnumerable<T> getList();
        void insert(T t);
        void Delete(object id);
        void Update(T t);
        void Save();

    }

my error is "the Type 'T' can not be used as type parameter 'T' in the generic type...." and I want to know how can I resolve this problem

Aucun commentaire:

Enregistrer un commentaire