Nic*_*ahn 2 asp.net-mvc entity-framework unit-of-work repository-pattern
更新3:
我看到这个视频以及作者如何强调使用Repository/UOW ......反对我劝阻的内容.btw作者正在使用ORM(EF)
http://pluralsight.com/training/Courses/TableOfContents/spa
Run Code Online (Sandbox Code Playgroud)
更新2:
当我正在玩Repository时,我有这个scanrio来解决,我不确定我是否正在按照正确的方向......所以在我的控制器中:
public class PersonsController : Controller
{
GenericRepository<Person> _genericRepository = new GenericRepository<Person>(new PersonsContext());
public ActionResult Index()
{
GenericRepository<Actors> _genericActorRepository = new GenericRepository<Actors>(new PersonsContext());
IEnumerable<Actors> _actorList = _genericActorRepository.GetAll();
//IList<Actors> _actorList1 = _genericActorRepository.GetAll().ToList();
ViewBag.ActorList = new SelectList(_actorList);
return View(_genericRepository.GetAll());
}
Run Code Online (Sandbox Code Playgroud)
}
更新:
以下是Microsoft Developer Network关于GenericRepository 的链接!
我正在尝试在系统的设计阶段实施最佳实践.我将使用实体框架,ASP.NET MVC 5 C#和通用存储库/工作模式单元(希望如此).
我的问题:如何在GenericRepository中引入工作单元?
这是我的GenericRepository类:
public interface IGenericRepository<TEntity> : IDisposable
{
Task<TEntity> GetByIdAsync(int id);
IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate);
IQueryable<TEntity> GetAll();
Task EditAsync(TEntity entity);
Task InsertAsync(TEntity entity);
Task DeleteAsync(TEntity entity);
}
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
protected DbSet<TEntity> _dbSet;
private readonly DbContext _dbContext;
public GenericRepository(DbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<TEntity>();
}
public GenericRepository() {}
public IQueryable<TEntity> GetAll()
{
return _dbSet;
}
public async Task<TEntity> GetByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return _dbSet.Where(predicate);
}
public async Task EditAsync(TEntity entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
public async Task InsertAsync(TEntity entity)
{
_dbSet.Add(entity);
await _dbContext.SaveChangesAsync();
}
public async Task DeleteAsync(TEntity entity)
{
//if (context.Entry(entityToDelete).State == EntityState.Detached)
//{
// dbSet.Attach(entityToDelete);
//}
_dbSet.Remove(entity);
await _dbContext.SaveChangesAsync();
}
public void Dispose(bool disposing)
{
if (_dbContext != null)
{
_dbContext.Dispose();
}
GC.SuppressFinalize(this);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Run Code Online (Sandbox Code Playgroud)
型号类:
public class Person
{
public int Id { get; set; }
public String Fullname { get; set; }
public String Profession { get; set; }
public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
语境:
public class PersonsContext : DbContext
{
public PersonsContext() : base("name=PersonsContext")
{
}
public DbSet<Person> People { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class PersonsController : Controller
{
GenericRepository<Person> _genericRepository = new GenericRepository<Person>(new PersonsContext());
//
// GET: /Persons/
public ActionResult Index()
{
return View(_genericRepository.GetAll());
}
//
// GET: /Persons/Details/5
public async Task<ActionResult> Details(Int32 id)
{
Person person = await _genericRepository.GetByIdAsync(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
//
// GET: /Persons/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Persons/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.InsertAsync(person);
return RedirectToAction("Index");
}
return View(person);
}
//
// GET: /Persons/Edit/5
public async Task<ActionResult> Edit(Int32 id)
{
Person person = await _genericRepository.GetByIdAsync(id);
if (person == null)
{
return HttpNotFound();
}
return View(person);
}
//
// POST: /Persons/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.EditAsync(person);
return RedirectToAction("Index");
}
return View(person);
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这在很大程度上已成为一种反模式.使用工作单元和存储库是重要且良好的设计,但大多数人忽略了实体框架已经是工作单元和通用存储库的实现这一事实.
如果您确实认为有必要(例如,您打算支持不同类型的数据访问),或者您认为您的应用程序将特别长久并需要大量持续维护,那么只能在EF之上实现您自己的UoW和通用存储库.如果您的应用程序相对简单,和/或不太可能发生重大变化,那么就没有理由实施其他抽象.
我不建议在控制器中直接使用EF代码,因此您应该使用某种数据抽象,例如服务层或具体存储库(与通用相反,具体的存储库具有类似于GetCustomers()将业务逻辑抽象为方法).
实现您自己的UoW或Generic Repository的另一个原因是,如果您不打算使用支持UoW的ORM,那么您需要自己的实现.
| 归档时间: |
|
| 查看次数: |
3865 次 |
| 最近记录: |