NHibernate / Castle.ActiveRecord; Session management; Winforms

My first real (non-test) NHibernate / Castle.ActiveRecord project is developing rapidly. I have been working with NHibernate / Castle.ActiveRecord for about a month, but still don't know how to handle sessions in a WindowsForms application.

The general processing-processing method does not work for me:

  • SessionPerRequest, SessionPerConversation, etc. all work only for webapplications etc.
  • SessionPerApplication is not recomanded / very dangerous when I'm right.
  • SessionPerThread is not very useful, since I either have only one thread, or WindowsForms-thread, or for each button a new thread. The first thing that will make my application use too much memory and store old objects in memmory. With workflows for pressing the ech button, I would turn off lazy loading, since my loaded objects will live longer than the stream.
  • SessionPerPresenter also does not work, because I usually open the "sub-presenter" in a form that allows the user to search / load / select some reference objects (key key) and cause the presenter to be destroyed - which means the session is closed - but the object used in "super presenter "to populate the link property (foreigen key).

I used Google and Bing for several hours and read a lot, but found only one good site about my case: http://msdn.microsoft.com/en-us/magazine/ee819139.aspx . There SessionPerPresenter is used, but for the "sub-presenter" it is provided only with the id, and not the entire object! And this suggests that in this example there are no key keys and there are no scripts in which the object returns to the "super-presenter".

Qestions

  • Is there any other session processing method for windowsforms / desktop-application?
  • I can add the session-session property or the -session-constructor parameter for all my presenters, but I am not good at having session processing of all my u-code.
  • When an exception occurs, NHibernate wants me to kill the session. But if it is an β€œonly” business logic exception, not an NHibernate-Exception?

Example

I am trying to make an example covering most of my problem.

// The persisten classes public class Box { public virtual int BoxId{get;set;} public virtual Product Content{get;set;} ... } public class User { public virtual int UserId{get;set;} public virtual IList<Product> AssigenedProducts{get;set;} ... } public clas Product { public virtual int ProductId{get;set;} public virtual string PrductCode{get;set;} } 

.

 // The presenter-classes public class ProductSearchPresenter : SearchPresenter<Product> { ... } public class ProductEditPresenter : EditPresenter<Product> { ... } public class UserSearchPresenter : SearchPresenter<User> { ... } public class UserEditPresenter : EditPresenter<User> { ... } public class BoxSearchPresenter : SearchPresenter<Box> { ... } public class BoxEditPresenter : EditPresenter<Box> { ... } // The search-presenters allow the user to perform as search with criterias on the class defined as generic argument and to select one of the results // The edit-presenters allow to edit a new or loaded (and given as parameter) object of the class defined as generic argument 

Now I have the following precedents: all of them can be executed in one application at the same time asynchronously (use just switches between the leading ones). A.

  • using an instance of BoxSearchPresenter to search and select an object
    • part of this utility is to use an instance of ProductSearchPresenter to populate BoxSearchPresenter criteria
    • part of this utility is to use a BoxEditPresenter instance to edit and save the selected BoxSearchPresenter instance object
  • Using an instance of UserSearchPresenter to search and select an object
    • part of this utility is to use an instance of UserEditPresenter to edit and save the highlighted UserSearchPresenter object
    • part of this utility is to use ProductSearchPresenter to search and select objects to be added to User.AssignedProducts.
  • Using an instance of ProductSearchPresenter to search and select an object.
    • part of this utility is to use an instance of ProductEditPresenter to edit and save the selected ProductSearchPresenter object.

This is just a small collection of truncations, but I have many problems that I have.

  • UseCase 1. and 2. are executed simultaneously on the same line.
  • UseCase 1.1. and 2.2. return there the selected objects to other leaders who use these objects longer than those who loaded the object exist.
  • UseCase 3.1. can change the object loaded from 2.2./1.1. up to 3.1. was started, but when 2.2./1.1. done before 3.1. completed, the object will be saved, and "rollback" 3.1 would not have been possible.
+4
source share
1 answer

Here is just a brief idea of ​​what I found most suitable for our WinForms application architecture (based on MVP).

Each presenter is a constructor depending on the repositories that it needs, for example, if you have an InvoicePresenter, then you have an InvoiceRepository as a dependency, but you will probably have a CustomerRepository and many others depending on complexity (CustomerRepsitory to load all customers in a customer list group if you want to change customer invoices, something like this).

Each repository then has a constuctor argument for UnitOfWork. Either you can abstract the session using the UnitOfWork template, or you can depend on how your replicas depend on ISession.

Everything is connected together by the IoC container, where we create presenters based on the "context". This is a very simple concept, a context for each presenter and all sub-presenters, which, in turn, we create as an integral block of more complex presenters to reduce complexitiy (if, for example, you have several tabs of options for editing an entity or something else).

Thus, in practice, this context is based on a 90% time basis, because one form is at least one presenter / presentation.

So, to answer your questions:

  • A session for each moderator and conversation session (also works with WinForms) are really useful templates here (and opening closing sessions all over the place, but not a good way to handle this) -

  • this is best resolved if the repositories are session-dependent rather than leading. You make speakers depend on repositories, repositories depend on a session, and when you create everything, you give them a shared session; but, as I have stated, this is practical only in context. You cannot use the session to compose invoices for the host and other editors; but you can exchange a session when editing an invoice using the main presenter and the details of the invoice and sub-presenter notes.

  • Please clarify, did not understand this ...

+2
source

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


All Articles