0

i am using ASP.NET mvc 5 in visual studio 2013. I have i process to implement generic repository and later on UnitOfWork. I have IGenericRepository which has IQueryable one function, because i want to learn and understand so i kept simple as possible. I have GenericRepository class where i am implementing this interface. I got FunctionContext which is inherited from baseContext. The reason i have baseContext so all the dbcontexts can use one path to hit database but same time keep number of table limited to business need.

I got error coming in GenericRepository class under GetAll function, i believe i am not extracting data properly.

many thanks in advanced....

IGernericRepository

  public interface IGenericRepository<TEntity> : IDisposable
{
    IQueryable<TEntity> GetAll { get; }

}

GenericRepository

 public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    private FunctionsContext _Context = new FunctionsContext();

    public GenericRepository()
    {

    }

    public IQueryable<TEntity> GetAll()
    {
        /* return _Context.Functions.Select(x => new Functions
            {
                Function_ID = x.Function_ID,
                Title = x.Title,
                Hierarchy_level = x.Hierarchy_level
            });*/

        ????????????????????? need help here!
    }


    public void Dispose()
    {

    }
}

FunctionsContext

public class FunctionsContext : BaseContext<FunctionsContext>
{
    public DbSet<App_Functions> Functions { get; set; }
}

BaseContext

public class BaseContext<TContext> : DbContext where TContext : DbContext
{
    static BaseContext()
    {
        Database.SetInitializer<TContext>(null);
    }

    protected BaseContext()
        : base("name = ApplicationDbConnection")
    { }
}

Functions Table (model)

[Table("Functions")]
public class App_Functions
{
     public App_Functions()
    {
      //  this.App_Controllers = new List<App_Controllers>();
    }

    [Key]
    public int Function_ID { get; set; }
    [StringLength(50)]
    [Required]
    public string Title { get; set; }
    public int Hierarchy_level { get; set; }

}

Controller class

   using (var repository = new GenericRepository<Functions>())
        {
            foreach(var functions in repository.GetAll)
            {
                var a7 = functions.Title;
               ??????
            }
        }
1
  • Please don't use the repository pattern, it is flawed on a lot of levels unless you are working with an API that is really close to the metal. The datacontext in EF is a good abstraction and there is no need to pile on another one. Commented Dec 19, 2013 at 18:02

3 Answers 3

4

DbSet<TEntity> implements IQueryable<> so you should be able to just do this:

public IQueryable<TEntity> GetAll()
{
    return _Context.Functions;
}

To make it generic, working for all types of TEntity:

public IQueryable<TEntity> GetAll()
{
    return _Context.Set<TEntity>();
}

Give it a try and let me know if it works for you.

Sign up to request clarification or add additional context in comments.

4 Comments

Wouldn't this need the reference constraint? where TEntity : class
The repo already has that at class level judging from @toxic code
yes it does work... so next i believe in controller class i call the GenericRepository with argument <className> that is function??
i have edit in controller class in my above code! but it seems i am doing something wrong! sorry ...
1

I don't think the other answer really addresses the ?????? bit.

This does:

public IQueryable<TEntity> GetAll()
{
    return _Context.Set<TEntity>();
}

Although, I wouldn't do it as a method. Do it as a read-only property.

public IQueryable<TEntity> Entities
{
    get { return _Context.Set<TEntity>(); }
}

Comments

0
// Generic Repository
public class Repository<T> where T : class
{
    private readonly DbSet<T> _dbSet;
    public Repository(DbSet<T> dbSet)
    {
        _dbSet = dbSet;
    }

    public IEnumerable<T> Where(Expression<Func<T, bool>> where)
    {
        return _dbSet.Where(where);
    }

    public T FirstOrDefault(Expression<Func<T, bool>> where)
    {
        return _dbSet.FirstOrDefault(where);
    }

    // other methods
}

// Unit of Work interface
public interface IUnitOfWork
{
    Repository<Product> ProductsRepository { get; }
}

// Unit of Work implementation
public class MyContext : DbContext, IUnitOfWork
{
    private readonly Repository<Product> _products = null;

    public DbSet<Product> Products { get; set; }

    public MyContext()
        : base("MyContext")
    {
        _products = new Repository<Product>();
    }

    public Repository<Product> ProductsRepository
    {
        get { return _products; }
    }
}

2 Comments

My intention was to give an example of a Generic Repository with EntityFramework and UnitOfWork. The question is "implement generic repository in ASP.NET MCV5", so i answered the question, not to the "??????" part
i also agree that this doesn't answer the question in the way that the poster was aiming for. i'd also say that by both mis answering the question AND adding additional fluff, you've created an anti-answer. as such, i've voted it down - sorry, not personal, just wanting to keep the bar high.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.