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
|
Hello, I'm getting InvalidCastException when I call Repository.Current I think it's due to multiple models I have in my web site project. To simplify things, I have Site model which serves list lookup items and Client model. I also have dedicated repository per each model, and dedicated data context: SiteDataContext = new LightSpeedContext<SiteUnitOfWork>(configurationName);ClientDataContext = new LightSpeedContext<ClientUnitOfWork>(configurationName);When I call new ClientRepository().GetClient(id) it works fine returning data. Then I call new SiteRepository().GetList("Title") to load list of titles for drop down. As soon as I callnew PerRequestUnitOfWorkScope<SiteUnitOfWork>(DataContext.SiteDataContext);and inspect result of returned UnitOfWorkScopeBase I can see that Current property throws an InvalidCastException. See below Unable to cast object of type 'SiLogicWeb.DataLayer.Models.ClientModel.ClientUnitOfWork' to type 'SiLogicWeb.DataLayer.Models.SiteModel.SiteUnitOfWork'. 'unitOfWorkScope.Current' threw an exception of type 'System.InvalidCastException' SiLogicWeb.DataLayer.Models.SiteModel.SiteUnitOfWork {System.InvalidCastException} at Mindscape.LightSpeed.PerRequestUnitOfWorkScope`1.get_Current() Thanks for looking in it, Alex
|
|
|
Hi Alex, Yes - you are correct in why the casting error is occuring, PerRequestUnitOfWorkScope uses local storage to store the currently instantiated UnitOfWork so it can be re-used with a request. Because you have already accessed the Client repository the value which has been stored is of a ClientUnitOfWork type rather than SiteUnitOfWork. What you will want to do is use a custom implementation of this which correctly caters for multiple UnitOfWork instances from separate schemas. If you have a look at this thread: http://www.mindscape.co.nz/forums/Thread.aspx?PostID=5449 there is a code example of how to do this :) Let us know how you get on
Jeremy |
|
|
Thanks Jeremy. It worked out as a charm. But I still think, that it is a major flaw in the design. It shouldn't see other UnitOfWorks from different domains. Hope you will fix it soon. Many thanks, Alex |
|