我当前的应用程序结构是:
Data.IRepository<>注册ORM.GenericRepository<>IRepository<>这种结构本质上将业务层与实现IRepository<T>.
这种解耦结构的好处之一是我应该能够相对轻松地替换 ORM - 例如,从实体框架迁移到 NHibernate 或仅升级现有的 ORM。我目前首先使用 EF 4.1 代码,并正在为 NHibernate 构建另一个程序集。
我正在考虑实施工作单元模式。
我读到,这种模式应该在业务层中使用(在我的数据程序集中定义接口,并在 ORM 程序集中实现,就像我对存储库模式所做的那样)。目前,所有实例化存储库都有自己的 DbContext / 会话,并且其生命周期设置为存储库的生命周期 - 这可能很糟糕 - 我的问题是我不确定是否可以实现可以使用的工作单元模式不同的 ORM(呃,更确切地说,它可能是,我只是不跟上速度)。
这是我唯一想到的事情:
在我的数据程序集中创建具有以下功能的 IUnitOfWork:object GetCurrentSession();,然后在 ORM 程序集中的存储库的构造函数中添加一个参数,并将其转换为适当的会话/DbContext(如果是 NHibernate,则为 ISession,如果是 Entity Framework,则为 DbContext)
如果有人对这种情况有所了解,我将不胜感激。
.net dependency-injection inversion-of-control unit-of-work decoupling
我只是想开始学习 NHibernate,在使用极其简单的 POCO 进行测试时我已经遇到了问题。我收到 No Persister 异常,这是我的代码:
Account桌子:
create table Account
(
AccountID int primary key identity(1,1),
AccountName varchar(10),
CreateDate datetime
)
go
Run Code Online (Sandbox Code Playgroud)
Account班级:
public class Account
{
public virtual int AccountID { get; set; }
public virtual string AccountName { get; set; }
public virtual DateTime CreateDate { get; set; }
public Account()
{
AccountID = 0;
AccountName = "";
CreateDate = new DateTime();
}
}
Run Code Online (Sandbox Code Playgroud)
映射文件Account.hbm.xml(是的,它嵌入到程序集中):
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernateTesting" assembly="NHibernateTesting"> …Run Code Online (Sandbox Code Playgroud) 我想问如何构建一个类似于 TFS 用于查询的查询生成器引擎,该引擎将允许用户组合多个条件,并在后端使用 EF 来根据条件查询数据。

类似于:用户构建查询 -> 将 json 发送到后端 -> 后端以 EF 代码优先 + 存储库模式处理查询。
有一个 jquery 查询构建器插件,但如何将该 json 转换为 EF 存储库模式?
jQuery 查询生成器:jquery 查询生成器
首先,如果这个问题已经得到回答,我很抱歉。但我无法找到解决方案。
假设我想创建一个订单以及一些行,同时创建另一个实体。
服务层:
public class OrderService
{
private DbContext context;
public OrderService()
{
context = new DbContext();
}
public void AddOrder(Order order, List<Orderline> lines, AnotherEntity anotherEntity)
{
context.Orders.Add(order);
context.Orderlines.AddRange(lines);
var anotherService = new AnotherService();
anotherService.AddAnother(anotherEntity)
context.SaveChanges();
}
}
public class AnotherService
{
private DbContext context;
public AnotherService()
{
context = new DbContext();
}
public void AddAnother(AnotherEntity entity)
{
// Maybe some business rules here
context.SomeOtherEntities.Add(entity);
context.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
var orderService = new OrderService();
orderService.Add(order, lines, anotherEntity);
Run Code Online (Sandbox Code Playgroud)
第一个问题是我在两个服务中有不同的上下文,因此有两个不同的事务。我能想到的解决方案:
将 dbcontext …
.net entity-framework transactionscope unit-of-work dbcontext
我知道这个问题之前已经被问过好几次了,但我找不到我想要的答案。因此,我实现了存储库(通用)和 UOW 模式,因此我可以使用 EF 访问我的数据库。这是 UnitOfWork 类的一部分:
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
entities.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Run Code Online (Sandbox Code Playgroud)
问题是:谁在调用 void Dispose 方法?我还没有看到使用该方法的示例。
这是工作单元类的上半部分:
private BDEntities entities = null;
public UnitOfWork()
{
if (entities == null)
{
entities = new BDEntities();
}
}
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?我应该使用“使用”吗?如果是,那么为什么我需要 Dispose 方法?
您能给我一个正确且简单用法的例子吗?
谢谢你,罗特姆
在浏览了多篇堆栈溢出帖子和博客文章后,我做出了这样的决定:我们需要UnitOfWork 设计模式来维护事务完整性,同时将域对象写入各自的存储库。
然而,我们在阅读/搜索存储库时不需要这种完整性。鉴于此,将存储库和工作单元的用途分开,前者仅用于读取域对象,后者仅用于创建/写入/刷新/删除域对象,这是一个好的设计吗?
domain-driven-design repository ddd-repositories unit-of-work repository-pattern
在 Martin Fowler 的书中,我读到了有关UnitOfWork和IdentityMap 模式的内容。
作者提到将 IdentityMap 放入 UnitOfWork 中是一个好主意。但怎么办呢?
据我了解IdentityMap,受会话限制,但作者没有提到这一点UnitOfWork
实例是否UnitOfWork受会话限制?
假设我们有客户和订单实体。
public clas Client{
List<Order> orders;
...
}
Run Code Online (Sandbox Code Playgroud)我们收到更新客户信息(电话号码)并添加新订单的请求:
这里需要多少个unitOfWork实例?每个会话?我们应该为客户和订单提供单独的实例吗?
我们这里需要多少个 IdentityMap 实例?对于每个实例?我们应该为客户和订单提供单独的实例吗?
每个unitOfWork 实例需要多少个IdentityMap 实例?
如果我们有 2 个并发请求怎么办?
我在我的项目中使用 UnitOfWork 和 Repository 模式。我正在尝试编码干净。
这是我的IUnitOfWork.cs (应用层)
public interface IUnitOfWork : IDisposable
{
int Save();
IGenericRepository<TEntity> Repository<TEntity>() where TEntity : class;
}
Run Code Online (Sandbox Code Playgroud)
UnitOfWork.cs的实现:(持久层)
public class UnitOfWork : IUnitOfWork
{
private readonly DBContext _context;
private Hashtable _repositories;
public UnitOfWork(DBContext context)
{
_context = context;
}
public IGenericRepository<T> Repository<T>() where T : class
{
if (_repositories == null)
_repositories = new Hashtable();
var type = typeof(T).Name;
if (!_repositories.ContainsKey(type))
{
var repositoryType = typeof(GenericRepository<>);
var repositoryInstance =
Activator.CreateInstance(repositoryType
.MakeGenericType(typeof(T)), _context);
_repositories.Add(type, …Run Code Online (Sandbox Code Playgroud) c# dependency-injection unit-of-work repository-pattern solid-principles
我正在尝试在现有项目中使用Sequelize和 Typescript 来实现存储库和工作单元模式,遵循域驱动设计。然而,由于互联网上缺乏相关示例,我遇到了一些困难。
让我们考虑以下代码示例。
interface IPostRepo {
save(post: Post): Promise<void>;
}
interface ICommentRepo {
save(comment: Comment): Promise<void>;
saveBulk(comments: Comment[]): Promise<void>;
}
class SequelizePostRepo implements IPostRepo {
private models: any;
private commentRepo: ICommentRepo;
constructor(models: any, commentRepo: ICommentRepo) {
this.models = models;
this.commentRepo = commentRepo;
}
public async save(post: Post): Promise<void> {
const PostModel = this.models.Post;
const rawSequelizePost = await PostMap.toPersistence(post);
await PostModel.create(rawSequelizePost);
await this.commentRepo.saveBulk(comments);
}
}
class SequelizeCommentRepo implements ICommentRepo {
private models: any;
constructor(models: …Run Code Online (Sandbox Code Playgroud) domain-driven-design unit-of-work repository-pattern sequelize.js typescript
的DbContext在EF Code first器具的工作单位和存储库模式为
MSDN网站上说:
DbContext实例表示工作单元和存储库模式的组合,以便它可以用于从数据库进行查询并将更改组合在一起,然后将更改作为一个单元写回到存储中.DbContext在概念上类似于ObjectContext.
这是否意味着使用另一个UoW和Repository抽象(例如IRepository和IUnitOfWor),超过DbContext是错误的?
换句话说,在DbContext上使用另一个抽象层是否会为我们的代码添加任何其他值?
技术独立DAL(我们的域将取决于IRepository和IUnitofWork而不是DbContext)等值
entity-framework data-access-layer repository unit-of-work dbcontext
unit-of-work ×10
.net ×3
c# ×3
repository ×3
dbcontext ×2
decoupling ×1
enterprise ×1
identity-map ×1
java ×1
linq ×1
nhibernate ×1
sequelize.js ×1
typescript ×1