Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top