在NHibernate中冲洗 [英] Flushing in NHibernate

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

问题描述

这个问题有点儿愚蠢,但我仍然不了解处理冲洗的最佳方法.

This question is a bit of a dupe, but I still don't understand the best way to handle flushing.

我正在迁移现有的代码库,其中包含很多类似以下的代码:

I am migrating an existing code base, which contains a lot of code like the following:

private void btnSave_Click()
{
     SaveForm();
     ReloadList();
}

private void SaveForm()
{
    var foo = FooRepository.Get(_editingFooId);

    foo.Name = txtName.Text;

    FooRepository.Save(foo);
}

private void ReloadList()
{
     fooRepeater.DataSource = FooRepository.LoadAll();
     fooRepeater.DataBind();
}

现在我将FooRepository更改为Nhibernate,那么FooRepository.Save方法应该使用什么?保存实体后,FooRepository是否应该始终刷新会话?

Now that I am changing the FooRepository to Nhibernate, what should I use for the FooRepository.Save method? Should the FooRepository always flush the session when the entity is saved?

推荐答案

我不确定是否理解您的问题,但这是我的想法:

I'm not sure if I understand your question, but here is what I think:

请考虑将对象放入会话"而不是获取和存储数据". NH将在会话中存储所有新的和更改的对象,而无需对其进行任何特殊调用.

Think in "putting objects to the session" instead of "getting and storing data". NH will store all new and changed objects in the session without any special call to it.

请考虑以下情形:

数据更改:

  • 通过任何查询从数据库获取数据.这些实体现在正在NH会话中
  • 仅更改属性值即可更改实体
  • 提交事务.更改将被刷新并存储到数据库中.

创建一个新对象:

  • 调用构造函数创建一个新对象
  • 通过调用保存"将其存储到数据库中.现在在会议中.
  • 保存后,您仍然可以更改对象
  • 提交更改.最新状态将存储到数据库中.

如果使用分离的实体,则还需要Update或SaveOrUpdate才能将分离的实体放入会话中.

If you work with detached entities, you also need Update or SaveOrUpdate to put detached entities to the session.

当然,您可以将NH配置为不同的行为.但是,如果遵循此默认行为,效果最佳.

Of course you can configure NH to behave differently. But it works best if you follow this default behaviour.

这篇关于在NHibernate中冲洗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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