构建NHibernate DTO的最佳方法 [英] Best approach for building NHibernate DTO's

查看:126
本文介绍了构建NHibernate DTO的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NHibernate(和ORMS)的新手,试图去处理它提供的各种不同的选项。作为参考,我使用Fluent NHibernate单独的业务对象,反过来使用DTO的纯粹的数据访问。我的应用程序体系结构必须同时支持Windows和Web前端。

我的困惑是一种普遍的方法,因为似乎有这么多的选择。我的DTO看起来像下面的示例。每个DTO都有一个从BO传给他们的ISession的引用。他们负责自己的加载和保存:

  public class EmployeeDTO ... 

//数据属性被持久化到数据库
public virtual int Id {get;私人设置}
公共虚拟字符串名字{get;组; }
public virtual string LastName {get;组; }
public virtual ISession Session {get;组; }

//保存逻辑
public virtual void Save()
{
var transaction = Session.BeginTransaction();
Session.SaveOrUpdate(this);
transaction.Commit();


//加载逻辑
public virtual void Load(int id)...

首先:
这是否是正确的方法?DTO是否有能力保存和加载?

其次:
无论保存/加载代码位于何处,您是否应该在生命周期或对象中使用相同的ISession,还是应该每次需要数据库交互时,都需要引用ISessionFactory并打开一个新会话?

  //每次打开一个新会话我与存储库交互
var session = FluentSupport.SessionFactory.OpenSession();
var transaction = Session.BeginTransaction();
Session.SaveOrUpdate(this);
transaction.Commit();
session.Close();
//完成后关闭会话

当然总是有选项3,以上都不是:)

解决方案

一般来说,DTO不包含行为(如保存,加载),不包含知识他们如何坚持(ISession)。这听起来像你真正创建的是一个数据层。理想情况下,您的业务层不应该了解ISession。也就是说,可以根据自己的需要快速调整层次结构,但如果您的ORM在所有层中流血,则可能很难在以后更改为其他ORM。



对于ISession生命周期管理,您必须决定是否要使用UnitOfWork模式,基本上说每个用户请求都获得一个新的ISession。还有其他的ISession生命周期的选择,你真的不限于这方面。通常,围绕Web应用程序和Windows应用程序的任何其他应用程序类型都可能会有最佳做法,但是您没有指定要编写的内容。


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".

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)...

First of all: Is this the correct approach to take - should the DTO have the ability to save and load itself?

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

Of course there's always option 3, none of the above :)

解决方案

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.

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的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆