I have recently started teaching myself C# and Asp.net. I am trying to build a simple blog application. I am confused about repository pattern usage. I have seen few tutorials and the implementation varies.
For my blog application, I have two database tables (models) - blogs and comments. Currently, I have a IDbContext which looks like this:
public interface IDBContext
{
IQueryable<Blog> FindAllBlogs();
IQueryable<Blog> FindBlogsInMonth(int month);
Blog GetBlog(int id);
void Add(Blog blog);
void Update(Blog blog);
void Delete(Blog blog);
void Add(Comment comment);
//void Remove(Comment comment);
}
and I have repository which looks like this:
public class BlogRepository : IDBContext
{
private BlogDb db = new BlogDb();
public IQueryable<Blog> FindAllBlogs()
{
return db.Blogs.OrderByDescending(b => b.PublishDate);
}
public Blog GetBlog(int id)
{
return db.Blogs.Single(b => b.BlogID == id);
}
...
}
The other implementation of repository pattern is something like this:
public interface IDbContext
{
IQueryable<Blog> Blogs { get; }
IQueryable<Comments> Comments { get; }
int SaveChanges();
T Attach<T>(T entity) where T : class;
T Add<T>(T entity) where T : class;
T Delete<T>(T entity) where T : class;
}
which calls a repository and there is separate class for queries.
What is the best method to do it?