标签: unit-of-work

如何从 Nhibernate 会话中获取已更改(脏)实体的列表?

在刷新所有更改的实体之前,我需要编写一些业务逻辑结构。我尝试过的解决方案之一是 IPreUpdateEventListener。但是这个事件监听器已经将对象非规范化为键值。在非规范化之前,甚至在刷新之前,我都需要一些东西。

所以问题是如何获取已更改(脏)实体的列表。

nhibernate unit-of-work

3
推荐指数
1
解决办法
2500
查看次数

使用 Moq 和 EF 4.1 模拟 UnitOfWork

我正在通过 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)

unit-testing moq unit-of-work asp.net-mvc-3

3
推荐指数
1
解决办法
3642
查看次数

通用工作单元

我已经实现了 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()”。现在我不能使用通用的工作单元类,因为它总是指向父存储库。

如何实施通用的工作单元来处理这种情况?提前致谢。

c# entity-framework unit-of-work repository-pattern

3
推荐指数
2
解决办法
8491
查看次数

如何强制触发 OnModelCreating 每个 DataContext 初始化

我想每次都触发 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# unit-of-work .net-framework-version entity-framework-6

3
推荐指数
1
解决办法
3841
查看次数

什么是数据库对象的工作单元?

用外行人的话来说,关于数据库对象的工作单元是什么?我正在研究如何将数据库表转换为C#类,我经常遇到这个术语,但每个人似乎都在描述它,好像你应该已经知道它是什么.

database database-design unit-of-work

2
推荐指数
1
解决办法
2459
查看次数

使用Castle Windsor的存储库类的适当生命周期

当我开始使用温莎时,我认为DI很简单.现在它让我越来越困惑.

作为具有单例生命周期的类,存储库让我感到震惊.在应用程序的生命周期中,我应该有一个FooRepository实例来加载和保存Foos到数据库.

但是,每个存储库都包含对UnitOfWork的引用,它执行脏检查,与数据库等一起工作.UnitOfWork具有PerWebRequest的生命周期 - 对于UnitOfWork来说单独存在没有任何意义,因为单例实例可以(例如)同时刷新多个用户会话所做的更改.

那么我有一个单例FooRepository,它持有对UnitOfWork的引用,在会话结束时会被处理掉!我甚至不确定会对存储库的行为产生什么影响,但听起来不太好.

任何人都可以用简单的英语(好吧,可能有一些代码)来解释在Web应用程序中管理Repository和UnitOfWork类的生命周期的适当方法吗?

lifecycle castle-windsor unit-of-work repository-pattern

2
推荐指数
1
解决办法
1290
查看次数

单元工作中有多个通用存储库?

可以说我有2张桌子.ProductCategoryProduct.我有一个可以处理两个表的通用存储库:

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

2
推荐指数
1
解决办法
4390
查看次数

将其转换为通用存储库模式

我已经开始将项目转换为通用存储库和工作单元模式.到目前为止,我已经能够将控制器中的所有直接上下文引用反向工程到通用存储库; 但是,我遇到以下两行代码的问题:

`context.Entry(ticket).Collection(i => i.TicketItems).Load();
            ticket.TicketItems.Clear();`
Run Code Online (Sandbox Code Playgroud)

这是我的控制器在删除a Ticket和a 之间的任何引用之前所做的事情TicketItem.之间存在许多一对多的关系TicketTicketItem.所以这些代码两行是什么,我在使用前删除所有TicketItems来自Ticket

entity-framework unit-of-work repository-pattern asp.net-mvc-3

2
推荐指数
1
解决办法
861
查看次数

导航属性的包含是否可以使用EF6嵌套在Web API中?

从我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

2
推荐指数
1
解决办法
1328
查看次数

使用GenericRepository和MVC 5/EF实现工作单元

更新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

2
推荐指数
1
解决办法
3865
查看次数