Managing ISession in Windows Forms app with MVP pattern

26 views
Skip to first unread message

Broken Pipe

unread,
Dec 15, 2009, 9:39:09 PM12/15/09
to nhusers
I have a winForms app with MVP pattern, and i don't how to use
Nhibernate with lazy loading in this pattern. Currently I'm using
eager fetching with LOTS of left joins.

So for example how manage ISession with lazy loading in classic MVP
situation when you have app showing list (DataGridVIew) of
BuisnessObjects and a Form to add\edit this BuisnessObject?

In my current implementation i'm doing something like this:

in my Model:
public IList<BuisnessObject> BuisnessObjectGetAll()
{
using (ISession ses = SessionFactory.OpenSession())
{
IQuery query = ses.CreateQuery("from BuisnessObject as
obj");
return query.List<BuisnessObject>();
}
}

In my BuisnessObjectList Presenter:
View.Items = Model.BuisnessObjectGetAll();

and in my View implementation i bind Items to DataGridView

As you may guess with lazy loading a get lots of exceptions because
ISession is closed after i return Objects from my Model. So i want to
know how to manage ISession in this situation?

Broken Pipe

unread,
Dec 15, 2009, 9:51:55 PM12/15/09
to nhusers
The only option I currently think of is using some global Start() Stop
() methods for opening closing ISession so i will have to do things
like:

In my BuisnessObjectList Presenter:
Model.Start();
View.Items = Model.BuisnessObjectGetAll();
Model.Stop();

And this looks VERY ugly.

Oskar Berggren

unread,
Dec 16, 2009, 4:18:52 AM12/16/09
to nhu...@googlegroups.com
You could replace Model.Start()/Stop() with the using-statement that
you showed inside your Model, to make it perhaps a little less ugly.
But it wouldn't help you if then later on the view needs something
that isn't eager loaded.

In one app I built using the MVP pattern I used DTOs or view models
that I assembled from the actual persistent entities. The views only
dealt with the view models, not with the entities. I don't quite
remember now, if I built the DTOs inside the presenter or in some form
of service layer. The latter case would be analogous to the case where
the app calls e.g. a web service the set and retrieve data. In this
case the view doesn't deal directly with entities either.

/Oskar


2009/12/16 Broken Pipe <brokenpi...@gmail.com>:
> --
>
> You received this message because you are subscribed to the Google Groups "nhusers" group.
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
>
>
>

Diego Mijelshon

unread,
Dec 16, 2009, 5:49:06 AM12/16/09
to nhusers
Ayende wrote a great article for MSDN where he uses a session per presenter, which is kept open.

Fabio Maulo

unread,
Dec 16, 2009, 7:25:19 AM12/16/09
to nhu...@googlegroups.com
CpBT with various examples for winForm with MVP, WPF with MVVM and even with some example for WEB (ASP.MVC and MonoRail).

2009/12/15 Broken Pipe <brokenpi...@gmail.com>
--

You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.





--
Fabio Maulo

José F. Romaniello

unread,
Dec 16, 2009, 8:01:01 AM12/16/09
to nhu...@googlegroups.com

Fabio Maulo

unread,
Dec 16, 2009, 10:56:27 AM12/16/09
to nhu...@googlegroups.com
2009/12/16 José F. Romaniello <jfroma...@gmail.com>

 IIRC this example was made by Fabio Maulo and Gustavo Ringel.
You can read Gustavo Ringel's posts in his blog: http://gustavoringel.blogspot.com/search/label/Conversation


The example was done only by Gustavo.
(dai a Cesare quel che é di Cesare) 
--
Fabio Maulo

Broken Pipe

unread,
Dec 16, 2009, 11:58:32 AM12/16/09
to nhusers
Thanx guys for links, i read\scrolled all of them + some child links +
i read\scrolled chapters 8-11 from book "NHibernate In Action",
though i didn't understood some stuff, now i know what options i have.
Also i found this interesting post: http://daniel.wertheim.se/2009/11/04/nhibernate-repository-pattern/

I' ll try to implement different approaches (Repository, Dao) with
(Ses-per-Conversation, ses-per-Presenter, and some hybrid variants).
I'm going to start from the concept in the link above it's very
similar to what i have now, and any way i was going to rewrite my
IRepository to use Linq expressions instead of ICriteria (though i
havn't tried Nh.Linq yet).

Gustavo Ringel

unread,
Dec 16, 2009, 12:21:50 PM12/16/09
to nhusers
I won't go that path, having a repository which commits...what if you want to commit a transaction across some aggregates? 

There are some other things to improve there...i don't mean it won't work, but using one of the frameworks that a lot of people is already using may be a better approach for the future.

I will go for uNHAddins but i'm a bit opinionated with it. I used with success in the past Castle NHibernate Integration and Rhino Tools, and they all do the work...

Gustavo.

Broken Pipe

unread,
Dec 16, 2009, 2:15:27 PM12/16/09
to nhu...@googlegroups.com
Ok, i will consider you opinion and start with uNHAddins.
I don't quite understand a problem with aggregates across transaction? (maybe i havn't faced it yet) or maybe i have but didn't know it is called so -) I'm not very good with "Terms" yet.
Can you give me link or post some abstract code that explains this problem.

On Wed, Dec 16, 2009 at 8:21 PM, Gustavo Ringel <gustavo...@gmail.com> wrote:
I won't go that path, having a repository which commits...what if you want to commit a transaction across some aggregates?

P.S
I started .NET Winforms development about 2 years ago, and only now (2 months ago) i began to use patterns -) Before this i have finished 2 big projects (3-tier WinForms SmartClient) and they are so ugly (from programers poing of view) you can't even imagine -))
They were implemented in Anti-Pattern, Autonomous view, copy-past style -)

Here is the code metrics for Client part of one of my old project -)
Project: Client
Configuration: Release
Scope: Project
Assembly: ########
Maintainability Index: 76
Cyclomatic Complexity: 1 860
Depth of Inheritance: 7
Class Coupling: 415
Lines of Code: 10 807
And my MainForm hand written part is 5626 lines -) and contains all layers -)

I hope my new project will have better implementation -)

Gustavo Ringel

unread,
Dec 17, 2009, 1:17:24 AM12/17/09
to nhusers
Commit is an Infrastructure concept, you commit because you are working with a Database, a Repository / Dao, doesn't need to know it is commiting something, but it should do its work (abstract the access to the Database)

If two repositories work in the same Use Case, you will want to commit both of them together, you don't want each repository to Commit its work, the transaction is a concept on top of that.

That's all.

Gustavo.

--

Broken Pipe

unread,
Dec 17, 2009, 3:22:21 AM12/17/09
to nhusers
Yes, your are right.
Reply all
Reply to author
Forward
0 new messages