This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, The UnitOfWork scopes kinda confuse me. From what I read there is the PerThreadUnitOfWork and the PerRequestUnitOfWork. Since I'm creating a WinForms application the latter doesn't apply to me. In my application I show a list of Patients and, if a patient is selected, a list of complaints per patient. I also give the option to add a new complaint for the currently selected user. ComplaintRepository complaintRepository = new ComplaintRepository(); complaintRepository.AddComplaint(complaint); complaintRepository.SaveChanges(); ReloadPatientComplaintsList(); When I call ReloadPatientComplaintsList() I don't get my new complaint in it. I guess this is because the current selectedpatient has already been loaded and so it's loaded from the IdentityMap?! How could I best solve my problem? public class ComplaintRepository : RepositoryBase, IComplaintRepository { public ComplaintRepository() : base(new PerThreadUnitOfWorkScope(MyConnectionManager.Context)) { } public IQueryable GetComplaintsForPatient(int patientId) { throw new NotImplementedException(); } public Complaint GetComplaint(int complaintId) { return UnitOfWorkScope.Current.FindById(complaintId); } public void AddComplaint(Complaint complaint) { UnitOfWorkScope.Current.Add(complaint); } } I guess the PerThreadUnitOfWorkScope really lasts the whole lifetime of a thread? Wouldn't that keep a connection open unnecessary? Should I instead create a new UnitOfWork inside every repository instead of every thread? Best regards, |
|
|
[quote user="Frankeeeh"]I guess the PerThreadUnitOfWorkScope really lasts the whole lifetime of a thread?[/quote] Yes. [quote user="Frankeeeh"]Wouldn't that keep a connection open unnecessary?[/quote] It does keep the connection open. However, it's not "unnecessary" because that's exactly what PerThreadUnitOfWorkScope asks for. You would use PTUOWS if keeping the same unit of work (and therefore the same connection) for the entire thread is what you want; if you want to be able to close the connection between units of work, don't use PTUOWS. (For what it's worth, I usually tell people not to use PTUOWS anyway -- its cleanup behaviour does not play nicely with many common scenarios.) [quote user="Frankeeeh"] Should I instead create a new UnitOfWork inside every repository instead of every thread?[/quote] You should create a new unit of work whenever you have a new 'business transaction' -- a new chunk of work. If you represent this with a new Repository object, then each Repository should indeed create its own UOW. JD has an article on UOW lifecycles in WinForms applications at: http://www.mindscapehq.com/blog/index.php/2009/08/19/tips-for-rich-client-development/ |
|