asp.net-mvc-2 – 实体框架4 CTP 4/CTP 5通用存储库模式和单元可测试

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-2 – 实体框架4 CTP 4/CTP 5通用存储库模式和单元可测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在玩最新的Entity Framework CTP 5版本,并建立一个简单的asp.net MVC博客,其中我只有两个表:发布和评论。这完全在POCO完成,我只需要帮助DbContext部分,我需要它是单元测试(使用IDbSet?)和我需要一个简单/通用存储库模式的添加,更新,删除,检索。任何帮助是赞赏。

谢谢。

解决方法

从你开始DbContext,创建一个名为Database.cs的新文件

Database.cs

  1. public class Database : DbContext
  2. {
  3.  
  4. private IDbSet<Post> _posts;
  5.  
  6. public IDbSet<Post> Posts {
  7. get { return _posts ?? (_posts = DbSet<Post>()); }
  8. }
  9.  
  10. public virtual IDbSet<T> DbSet<T>() where T : class {
  11. return Set<T>();
  12. }
  13. public virtual void Commit() {
  14. base.SaveChanges();
  15. }
  16. }

定义一个IDatabaseFactory并使用DatabaseFactory实现它:

IDatabaseFactory.cs

  1. public interface IDatabaseFactory : IDisposable
  2. {
  3. Database Get();
  4. }

DatabaseFactory.cs

  1. public class DatabaseFactory : Disposable,IDatabaseFactory {
  2. private Database _database;
  3. public Database Get() {
  4. return _database ?? (_database = new Database());
  5. }
  6. protected override void DisposeCore() {
  7. if (_database != null)
  8. _database.Dispose();
  9. }
  10. }

一次性扩展方法

Disposable.cs

  1. public class Disposable : IDisposable
  2. {
  3. private bool isDisposed;
  4.  
  5. ~Disposable()
  6. {
  7. Dispose(false);
  8. }
  9.  
  10. public void Dispose()
  11. {
  12. Dispose(true);
  13. GC.SuppressFinalize(this);
  14. }
  15. private void Dispose(bool disposing)
  16. {
  17. if(!isDisposed && disposing)
  18. {
  19. DisposeCore();
  20. }
  21.  
  22. isDisposed = true;
  23. }
  24.  
  25. protected virtual void DisposeCore()
  26. {
  27. }
  28. }

现在我们可以定义我们的IRepository和我们的RepositoryBase

IRepository.cs

  1. public interface IRepository<T> where T : class
  2. {
  3. void Add(T entity);
  4. void Delete(T entity);
  5. void Update(T entity);
  6. T GetById(long Id);
  7. IEnumerable<T> All();
  8. IEnumerable<T> AllReadOnly();
  9. }

RepositoryBase.cs

  1. public abstract class RepositoryBase<T> where T : class
  2. {
  3. private Database _database;
  4. private readonly IDbSet<T> _dbset;
  5. protected RepositoryBase(IDatabaseFactory databaseFactory)
  6. {
  7. DatabaseFactory = databaseFactory;
  8. _dbset = Database.Set<T>();
  9. }
  10.  
  11. protected IDatabaseFactory DatabaseFactory
  12. {
  13. get; private set;
  14. }
  15.  
  16. protected Database Database
  17. {
  18. get { return _database ?? (_database = DatabaseFactory.Get()); }
  19. }
  20. public virtual void Add(T entity)
  21. {
  22. _dbset.Add(entity);
  23. }
  24.  
  25. public virtual void Delete(T entity)
  26. {
  27. _dbset.Remove(entity);
  28. }
  29.  
  30. public virtual void Update(T entity)
  31. {
  32. _database.Entry(entity).State = EntityState.Modified;
  33. }
  34. public virtual T GetById(long id)
  35. {
  36. return _dbset.Find(id);
  37. }
  38.  
  39. public virtual IEnumerable<T> All()
  40. {
  41. return _dbset.ToList();
  42. }
  43. public virtual IEnumerable<T> AllReadOnly()
  44. {
  45. return _dbset.AsNoTracking().ToList();
  46. }
  47. }

现在你可以创建你的IPostRepository和PostRepository:

IPostRepository.cs

  1. public interface IPostRepository : IRepository<Post>
  2. {
  3. //Add custom methods here if needed
  4. Post ByTitle(string title);
  5. }

PostRepository.cs

  1. public class PostRepository : RepositoryBase<Post>,IPostRepository
  2. {
  3. public PostRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
  4. {
  5. }
  6. public Post ByTitle(string title) {
  7. return base.Database.Posts.Single(x => x.Title == title);
  8. }
  9. }

最后,UoW:

IUnitOfWork.cs

  1. public interface IUnitOfWork
  2. {
  3. void Commit();
  4. }

UnitOfWork.cs

  1. private readonly IDatabaseFactory _databaseFactory;
  2. private Database _database;
  3.  
  4. public UnitOfWork(IDatabaseFactory databaseFactory)
  5. {
  6. _databaseFactory = databaseFactory;
  7. }
  8.  
  9. protected Database Database
  10. {
  11. get { return _database ?? (_database = _databaseFactory.Get()); }
  12. }
  13.  
  14. public void Commit()
  15. {
  16. Database.Commit();
  17. }

在控制器中使用:

  1. private readonly IPostRepository _postRepository;
  2. private readonly IUnitOfWork_unitOfWork;
  3.  
  4. public PostController(IPostRepository postRepository,IUnitOfWork unitOfWork)
  5. {
  6. _postRepository = postRepository;
  7. _unitOfWork = unitOfWork;
  8. }
  9.  
  10. public ActionResult Add(Post post) {
  11. _postRepository.Add(post);
  12. _unitOfWork.Commit();
  13. }

您将需要使用IoC容器,如StructureMap,使这项工作。你可以通过NuGet安装结构图,或者如果你使用MVC 3,你可以安装StructureMap-MVC NuGet包。 (下面链接)

Install-Package StructureMap.MVC4

Install-Package StructureMap.MVC3

Install-Package Structuremap

如果你有问题,只是让我知道。希望它有帮助。

猜你在找的asp.Net相关文章