问题描述
我是 NHibernate(和 ORMS)的新手,并试图掌握它提供的无数不同选项.作为参考,我将 Fluent NHibernate 与单独的业务对象一起使用,而这些对象又将 DTO 纯粹用于数据访问.我的应用程序架构必须同时支持 windows 和 web前端".
I'm new to NHibernate (and ORMS) and trying to come to grips with the myriad of different options it presents. For reference, I'm using Fluent NHibernate with seperate business objects which in turn use DTO's purely for data access. My application architecture must support both windows and web "front ends".
我的困惑是一种通用方法,因为似乎有很多选择.我的 DTO 看起来像下面的示例.每个 DTO 都有对从 BO 传递给它们的 ISession 的引用.他们负责自己的加载和保存:
My quandry is one of general approach as there seem to be so many options. My DTO's look something like the sample below. Each DTO has a reference to an ISession which is passed to them from the BO. They are responsible for their own load and save:
public class EmployeeDTO...
// Data Properties to be persisted to the database
public virtual int Id { get; private set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual ISession Session { get; set; }
// Save logic
public virtual void Save()
{
var transaction = Session.BeginTransaction();
Session.SaveOrUpdate(this);
transaction.Commit();
}
// Load logic
public virtual void Load(int id)...
首先:这是正确的做法吗?DTO 是否应该有能力保存和加载自己?
First of all:Is this the correct approach to take - should the DTO have the ability to save and load itself?
其次:无论保存/加载代码位于何处,您应该在生命周期或对象中使用相同的 ISession,还是应该在每次需要数据库交互时使用对 ISessionFactory 的引用并打开一个新会话?
Secondly:Regardless of where the save/load code lies, should you use the same ISession for the lifetime or an object, or should they have a ref to the ISessionFactory and open a new session every time database interaction is required?
// Open a new session every time I interact with the repository
var session = FluentSupport.SessionFactory.OpenSession();
var transaction = Session.BeginTransaction();
Session.SaveOrUpdate(this);
transaction.Commit();
session.Close();
// Close the session when I'm done
当然总是有选项 3,以上都不是 :)
Of course there's always option 3, none of the above :)
推荐答案
一般来说,DTO 不包含行为(如保存、加载),也不包含关于它们如何持久化 (ISession) 的知识.听起来您真正创建的是数据层.理想情况下,您的业务层也不应该知道 ISession.也就是说,您可以根据需要将这种分层的所有快捷方式简化,因为它适合您的需求,但如果您的 ORM 渗透到您的所有层中,以后可能很难更改为不同的 ORM.
In general, DTOs do not contain behavior (like Save, Load) and do not contain knowledge of how they get persisted (ISession). It sounds like what you are really creating is a data layer. Your business layer ideally shouldn't know about ISession either. That said, you can shortcut this layering all you want as it fits your needs, but it will likely be difficult to change to a different ORM later if your ORM bleeds through all your layers.
对于 ISession 生命周期管理,您必须决定是否要使用 UnitOfWork 模式,它基本上是说每个用户请求都会获得一个新的 ISession.ISession 生命周期还有其他选项,您在这方面确实不受限制.通常,关于网络应用程序、Windows 应用程序和任何其他应用程序类型可能存在最佳实践,但您没有指定要编写的是哪种应用程序.
For ISession lifetime management, you have to decide if you are going to use the UnitOfWork pattern, which basically says every user request gets a new ISession. There are other options for ISession lifetime as well and you really aren't limited in that regard. Often, there may be best practices around web apps vs. windows apps vs. whatever other application types, but you didn't specify which you were writing.
这篇关于构建 NHibernate DTO 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!