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
|
Does anyone have an example of using LightSpeed with the Repository Pattern using interfaces and dependency injection? |
|
|
Here is a fairly simple example using Ninject and our FilmFestival sample model. The highly exciting Worker class has an injected ICinemaRepository which in turn uses an injected IUnitOfWork. If you want to write LINQ queries in your repository methods you could inject a typed UnitOfWork instance instead to get access to the associated IQueryable properties. Let me know if you have any questions about the sample :)
Jeremy |
|
|
If you're using Windsor, it'd be pretty easy to do. Here's what I did, in brief: 1- Created a context, configured and added it as an instance: var ctx = new LightSpeedContext<PubDomainUnitOfWork>("AppContext") 2- Registered my generic UnitOfWork (generetad by Lightspeed designer) using factory method on the context: container.Register(Component 3- Registered a generic UnitOfScopeBase with PerThread lifestyle. I'm using this on a rich client so I've used the PerThreadUnitOfWorkScope, but you might choose other available UnitOfWorkScope implementations: container.Register(Component 4- Register your repositories (deriving from RepositoryBase<>): container.Register(AllTypes.FromAssemblyContaining<MyRepository>() If you need something more concrete I can put a more elaborated solution on my blog. That's it...happy injecting!
|
|
|
I would also like to see an example of this in regards to both a rich client application and a MVC web app. This post has an example of scoping a unit of work in a web app: http://www.mindscape.co.nz/blog/index.php/2009/05/19/unit-of-work-scoping-in-lightspeed/ but it does not show how to use dependency injection or recommended usage in a rich client... Currently, we are using Unity to do something like this: in Web Global.asax.cs: Unity.Container.RegisterType<UnitOfWorkScopeBase<AppModelUnitOfWork>, PerRequestUnitOfWorkScope<AppModelUnitOfWork>>(new InjectionConstructor(Repository.CreateContext())); in rich client:
Unity.Container.RegisterType<UnitOfWorkScopeBase<AppModelUnitOfWork>, PerThreadUnitOfWorkScope<AppModelUnitOfWork>>(new InjectionConstructor(Repository.CreateContext())); where "Unity" is a wrapper for Microsoft.Practices.Unity.UnityContainer
then, we can get the current unit of work by:
public DataService(Mindscape.LightSpeed.UnitOfWorkScopeBase<AppModelUnitOfWork> scope)
{
this.Data = scope.Current;
this.Data.Context.AutoTimestampMode = Mindscape.LightSpeed.AutoTimestampMode.Utc;
}
This works in the web app, but we have had issues with doing this in the rich clients...
A Full example from Mindscape on how to use unitOfWork dependency injection would be much appreciated.
Thanks,
-Joe
|
|
|
We're just starting to go down the road of building a new app and would like to see a using LightSpeed using Microsoft Unity to use unit of work dependency injection. Has something been put together that I haven't found or does someone have a best practices sample yet?
Thanks, Brad
|
|
|
I should note that we're building a web app and would need to use the PerRequestUnitOfWorkScope. Thanks again! |
|
|
Hi Brad, Can you elaborate on what you would need to inject the UOW into and maybe a bit about how you are structuring your application? Do you want to inject an IUnitOfWork for testing purposes? With the PerRequestUnitOfWorkScope implementation you can just access .Current on this from within any scope as the UOW on the current request is held in the Items collection - if you have a look above Joe gave an example of how he has injected this around using Unity, however you could inject a UOW directly if you implemented a custom version of PerRequestUnitOfWorkScope which handled registering the active UOW with your container at the beginning of each request.
Jeremy
|
|