Asp.net mvc asp中的存储库模式。净mvc3
我最近开始自学C#和Asp.net。我正在尝试构建一个简单的博客应用程序。我对存储库模式的使用感到困惑。我很少看到教程,实现也各不相同 对于我的博客应用程序,我有两个数据库表(模型)——博客和评论。目前,我有一个IDbContext,如下所示:Asp.net mvc asp中的存储库模式。净mvc3,asp.net-mvc,repository-pattern,Asp.net Mvc,Repository Pattern,我最近开始自学C#和Asp.net。我正在尝试构建一个简单的博客应用程序。我对存储库模式的使用感到困惑。我很少看到教程,实现也各不相同 对于我的博客应用程序,我有两个数据库表(模型)——博客和评论。目前,我有一个IDbContext,如下所示: public interface IDBContext { IQueryable<Blog> FindAllBlogs(); IQueryable<Blog> FindBlogsIn
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);
}
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);
}
...
}
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;
}
公共接口IDBContext
{
IQueryable FindAllBlogs();
IQueryable FindBlogsInMonth(整数月);
Blog GetBlog(intid);
无效添加(博客);
无效更新(博客);
作废删除(博客);
无效添加(注释);
//无效删除(注释);
}
我有这样一个存储库:
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);
}
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);
}
...
}
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;
}
公共类BlogRepository:IDBContext
{
private BlogDb=new BlogDb();
公共可查询的FindAllBlogs()
{
返回db.Blogs.OrderByDescending(b=>b.PublishDate);
}
公共博客GetBlog(int-id)
{
返回db.Blogs.Single(b=>b.BlogID==id);
}
...
}
存储库模式的另一个实现如下:
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);
}
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);
}
...
}
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;
}
公共接口IDbContext
{
IQueryable博客{get;}
IQueryable注释{get;}
int SaveChanges();
T附加(T实体),其中T:类别;
T添加(T实体),其中T:类;
T删除(T实体),其中T:类;
}
它调用存储库,并且有单独的查询类
最好的方法是什么?最简单的方法是直接使用Entity Framework,特别是Entity Framework 4.1及更高版本中显示的新DbContext功能 上下文将包含DbSet属性——每个属性都是存储库模式的一个实现,可以实现上述所有目标。如果需要单元测试支持,可以使用IDbSet
我在网上看到了很多ASP.NET MVC存储库模式的演示,这些模式最终用自定义存储库包装实体框架。这是一种浪费时间的行为——代码包装了其他代码,除了增加不必要的复杂性之外,没有任何直接的用途 谢谢你的评论。您在这里谈论的是哪些新功能。如果您能为我指出一些我可以阅读的资源的方向,我将不胜感激。我使用了Microsoft EF团队的一篇博文来开始使用版本4.1:。如果您不熟悉“代码优先”、“模型优先”和“数据库优先”,那么我建议从“模型优先”开始。它支持相同的EDMX文件,但修改代码生成以使用DbContext和DbSet。