简体   繁体   中英

Can't access functions in base generic class

I have a generic abstract base class with a service reference. When I try to access any properties form the concrete implementation class I receive the following error. 'Repository' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'Repository' could be found (are you missing a using directive or an assembly reference?)

**Base class**
namespace Services
{
    public abstract class BaseRepository<T> : IRepository<T>
    {
        public IService<T> _serviceContext;

        public BaseRepository(IService<T> serviceContext)
        {
            _serviceContext = serviceContext;
        }

        #region IRepository<T> Members

        public List<T> GetAll()
        {
            return _serviceContext.GetAll();
        }

        public T GetById(Guid id)
        {
            throw new NotImplementedException();
        }

        public void Add(T entity)
        {
            _serviceContext.Add(entity);
        }

        public void Remove(T entity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

**Concrete class**
namespace Services
{
    public class SpecialRepository : BaseRepository<SpecialItem>, ISpecialRepository
    {
        public SpecialRepository() : base(new DataAccess.SpecialList()) 
        {
        }
    }
}

**Service Class**
namespace DataAccess
{ 
    public class SpecialList : IService<SpecialItem>
    {
        public List<SpecialItem> GetAll()
        {
            //Implementation 
        }
    }
} 

**Repository Interface**
namespace Domain
{
    public interface IRepository<T>
    {
        List<T> GetAll();
        T GetById(Guid id);
        void Add(T entity);
        void Remove(T entity);
    }
}

**Service Interface**
namespace DataAccess
{
    public interface IService<T>
    {
        List<T> GetAll();
        void Add(T entity);
    }
}

Any help is appreciated. Thanks in advance!

You need to define an Add method in SpecialList.

I added one and I did not get an error calling

 SpecialRepository rep = new SpecialRepository();
 rep.Add(new SpecialItem());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM