Rinsing at NHibernate

This question is a bit of a trick, but I still don’t understand how best to deal with flushing.

I am migrating an existing code base that contains a lot of code, as shown below:

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();
}

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

+1
source share
3 answers

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

Think of “placing objects in a session” instead of “receiving and storing data”. NH will store all new and changed objects in the session without any special call.

Consider the following scenarios:

Data change:

  • . NH.
  • . .

:

  • , "". .
  • -
  • . .

, SaveOrUpdate, .

, NH -. , .

+3

, Foo Foos . NHibernate , , , , .

" ". , . , , , . , - , NHibernate.

+1

I would vote for Stefan Moser, if I could, I would deal with Nh anyway, but I think it would be nice to write this code:

private void SaveForm()
{
    using (var unitofwork = UnitOfWork.Start())
    {
        var foo = FooRepository.Get(_editingFooId);
        var bar = BarRepository.Get(_barId);

        foo.Name = txtName.Text;
        bar.SomeOtherProperty = txtBlah.Text;

        FooRepository.Save(foo);
        BarRepository.Save(bar);

        UnitOfWork.CommitChanges();
    }
}

Thus, either the whole action succeeds, or it fails and rolls back, while retaining flushing / transaction control outside the repositories.

+1
source

Source: https://habr.com/ru/post/1718140/


All Articles