在刷新所有更改的实体之前,我需要编写一些业务逻辑结构。我尝试过的解决方案之一是 IPreUpdateEventListener。但是这个事件监听器已经将对象非规范化为键值。在非规范化之前,甚至在刷新之前,我都需要一些东西。
所以问题是如何获取已更改(脏)实体的列表。
我正在通过 Contoso 示例进行一些 TDD 练习,我的检索学生测试正在通过。
我创建新学生的测试失败(尽管实际代码有效),因为我相信 DBContext 没有被嘲笑。
为了让这个测试过去,我应该重构什么?
测试失败如下:
Contoso.Tests.Controllers.StudentControllerTest.Create_HttpPost_Should_Save_New_Student:预期:9 但是:8
这是具体的 UnitOfWork
public class UnitOfWork : IUnitOfWork
{
private SchoolContext context = new SchoolContext();
private IStudentsRepository studentsRepository;
private bool disposed = false;
public IStudentsRepository StudentsRepository
{
get
{
if (this.studentsRepository == null)
{
this.studentsRepository = new StudentsRepository(context);
}
return studentsRepository;
}
}
public void Save()
{
context.SaveChanges();
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public …Run Code Online (Sandbox Code Playgroud) 我已经实现了 EntityFramework 模式以及 Repository 和 Unit Of Work。该实现类似于Code Project Repository Example,但是我需要对工作单元进行增强。
工作单位
public class GenericUnitOfWork : IDisposable
{
// Initialization code
public Dictionary<Type, object> repositories = new Dictionary<Type, object>();
public IRepository<T> Repository<T>() where T : class
{
if (repositories.Keys.Contains(typeof(T)) == true)
{
return repositories[typeof(T)] as IRepository<T>
}
IRepository<T> repo = new Repository<T>(entities);
repositories.Add(typeof(T), repo);
return repo;
}
// other methods
}
Run Code Online (Sandbox Code Playgroud)
上面的 UoW 是安静的概括,它始终针对父 Repository 类。我有另一个实体,例如学生,它有自己的存储库,扩展了 Repository 类。学生特定存储库有一个方法“GetStudentMarks()”。现在我不能使用通用的工作单元类,因为它总是指向父存储库。
如何实施通用的工作单元来处理这种情况?提前致谢。
我想每次都触发 OnModelCreating new DataContext(new Entity())......但是当我创建一个表的连接时,它可以工作。当为另一个表创建连接时,OnModelCreating 不再工作,所以因为我出错了,
the entity type <tableName> is not part of the model for the current context.
public class DataContext : DbContext
{
private BaseEntity _entity;
public DataContext(BaseEntity entity)
{
Database.Connection.ConnectionString = Parameters.ConnectionString;
_entity = entity;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
_entity.Map(modelBuilder); // this is dynamic fluent api here
}
}
Run Code Online (Sandbox Code Playgroud) 用外行人的话来说,关于数据库对象的工作单元是什么?我正在研究如何将数据库表转换为C#类,我经常遇到这个术语,但每个人似乎都在描述它,好像你应该已经知道它是什么.
当我开始使用温莎时,我认为DI很简单.现在它让我越来越困惑.
作为具有单例生命周期的类,存储库让我感到震惊.在应用程序的生命周期中,我应该有一个FooRepository实例来加载和保存Foos到数据库.
但是,每个存储库都包含对UnitOfWork的引用,它执行脏检查,与数据库等一起工作.UnitOfWork具有PerWebRequest的生命周期 - 对于UnitOfWork来说单独存在没有任何意义,因为单例实例可以(例如)同时刷新多个用户会话所做的更改.
那么我有一个单例FooRepository,它持有对UnitOfWork的引用,在会话结束时会被处理掉!我甚至不确定会对存储库的行为产生什么影响,但听起来不太好.
任何人都可以用简单的英语(好吧,可能有一些代码)来解释在Web应用程序中管理Repository和UnitOfWork类的生命周期的适当方法吗?
可以说我有2张桌子.ProductCategory和Product.我有一个可以处理两个表的通用存储库:
public class GenericRepository<T> : IRepository<T>
Run Code Online (Sandbox Code Playgroud)
但是当使用工作单元模式时,我是否被迫为数据库中的所有表创建存储库?
public interface IUnitOfWork : IDisposable
{
int SaveChanges();
IRepository<ProductCategory> ProductCategoryRepository { get; }
IRepository<Product> ProductRepository { get; }
}
Run Code Online (Sandbox Code Playgroud)
我没有办法将通用存储库添加到工作单元中吗?
asp.net-mvc entity-framework unit-of-work repository-pattern
我已经开始将项目转换为通用存储库和工作单元模式.到目前为止,我已经能够将控制器中的所有直接上下文引用反向工程到通用存储库; 但是,我遇到以下两行代码的问题:
`context.Entry(ticket).Collection(i => i.TicketItems).Load();
ticket.TicketItems.Clear();`
Run Code Online (Sandbox Code Playgroud)
这是我的控制器在删除a Ticket和a 之间的任何引用之前所做的事情TicketItem.之间存在许多一对多的关系Ticket和TicketItem.所以这些代码两行是什么,我在使用前删除所有TicketItems来自Ticket
entity-framework unit-of-work repository-pattern asp.net-mvc-3
从我Web API使用generic-repository/uow模式的服务开始EF6,我需要返回嵌套的导航属性.模型A具有模型B,模型B具有模型C等.像这样的东西:
public class A
{
public int SomeID { get; set; }
public ICollection<B> Bs { get; set; }
}
public class B
{
public int SomeID { get; set; }
public ICollection<C> Cs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我可以在A中获得B,但是B中的C是空的.这就是我这样做的方式:
// GENERIC REPOSITORY
public IQueryable<TEntity> Get(params Expression<Func<TEntity, object>>[] includes)
{
IQueryable<TEntity> query = _dbSet;
if (includes != null)
{
foreach (var include in includes)
query = query.Include(include);
}
return query;
}
// FROM THE "A" …Run Code Online (Sandbox Code Playgroud) c# entity-framework unit-of-work repository-pattern asp.net-web-api
更新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 …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework unit-of-work repository-pattern
unit-of-work ×10
c# ×3
asp.net-mvc ×2
database ×1
lifecycle ×1
moq ×1
nhibernate ×1
unit-testing ×1