문제

Using Autofac, I tried to use my generic type :

public interface IRepository<T> where T : class
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    void Delete(Expression<Func<T, bool>> where);
    T GetById(long Id);
    T GetById(string Id);
    T Get(Expression<Func<T, bool>> where);
    IEnumerable<T> GetAll();
    IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
}

to register my generic class :

 public abstract class RepositoryBase<T> where T : class
{
    private iMOSSContainer dataContext;
    private readonly IDbSet<T> dbset;

    protected RepositoryBase()
    {
        DatabaseFactory = new DatabaseFactory();
        dbset = DataContext.Set<T>();
    }

    protected RepositoryBase(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected iMOSSContainer DataContext
    {
        get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
    }
    public virtual void Add(T entity)
    {
        dbset.Add(entity);
    }
    public virtual void Update(T entity)
    {
        dbset.Attach(entity);
        dataContext.Entry(entity).State = EntityState.Modified;
    }
    public virtual void Delete(T entity)
    {
        dbset.Remove(entity);
    }
    public virtual void Delete(Expression<Func<T, bool>> where)
    {
        IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
        foreach (T obj in objects)
            dbset.Remove(obj);
    }
    public virtual T GetById(long id)
    {
        return dbset.Find(id);
    }
    public virtual T GetById(string id)
    {
        return dbset.Find(id);
    }
    public virtual IEnumerable<T> GetAll()
    {
        return dbset.ToList();
    }
    public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).ToList();
    }
    public T Get(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).FirstOrDefault<T>();
    }
}

this way :

            var builder = new ContainerBuilder();
        builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepository<>)); //.InstancePerLifetimeScope();
        Container = builder.Build();

And returns an error "sequence contains no element" when building a container.

Did i do something wrong with the code? I already tried to search for an answer to no avail. So, your help would be pretty much appreciated.

도움이 되었습니까?

해결책

Ok, then i changed the RepositoryBase class like this :

public class RepositoryBase<T> : IRepository<T> where T : class
{
    private iMOSSContainer dataContext;
    private readonly IDbSet<T> dbset;

    public RepositoryBase()
    {
        DatabaseFactory = new DatabaseFactory();
        dbset = DataContext.Set<T>();
    }

So :

  • i use the "iRepository" interface (and move the "where T : class" statement at the end of the line)
  • i remove the "abstract" class (because it leads to another error)
  • set the default constructor to "public" (because it leads to another error, as well)

And everything is working good right now.

다른 팁

Your RepositoryBase type is abstract, therefore it can't be registered. You need to register non-abstract types. I'm guessing what you're intending to do is register all subtypes of RepositoryBase.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top