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
|
Do you guys have an ASP.NET MVC Sample that uses an IoC Container? I don't care what tool you use. I am more interested in how it might change the way you code your current MVC Sample. In the current sample you chose to have a default constructor on ProductsController and UnitOfWorkScope Property on ControllerBase and use that value to inject into your CatalogRepository Class. The ControllerBase Class also disposes of the UoW as well. How can this be rewritten if we want the IoC Container to inject the dependencies into the ProductsController and CatalogRepository Classes and have the UoW disposed of properly without having to depend on a default constructor and that UnitOfWorkScope Propery? Thanks, Dave |
|
|
Hi David, We do not currently have a sample that uses an IoC container using the new API we introduced in LightSpeed 2.0 however it would be an interesting example to show. I'll spend some time this weekend building up a small sample of how you could manage this scenario. I hope that you're otherwise enjoying using LightSpeed? Kind regards, John-Daniel Trask |
|
|
Hi David, Rather than creating the repository in the constructor, add the repository to your container when you setup your container and remove the constructor setup (or fetch it from the container in the constructor and assign it privately). Call your container on the EndResult and close the unit of work. If you're more interested in fetching a new repository each time you wish to work with it, simply wrap your use of the repository in a Using statement as this will give you the complete unit of work lifecycle for the block of work you're undertaking. I hope that helps and please let me know if I've missed something that you're looking for in particular. Kind regards, John-Daniel Trask |
|
|
My concern is the Repository's dependency on the PerRequestUnitOfWorkScope hidden behind the UnitOfWorkScope Property on the ControllerBase Class. I can't use that if the Repository is injected so I am looking for the best practice on how the scope and repository is registered with the IoC Container so it can inject the scope into the repository and inject the repository into the controller. And then, of course, how this all gets disposed of properly at the end of the request.
I have some ideas, but ideally I was looking for an example of the best practice using any IoC Container and MVC Preview 5. Seeing an example will also flush out other answers, like best practices on lifetime management of those repositories: singleton, per request, etc. I also wonder if we really need to use the PreRequestUnitOfWorkScope since it sounds like it is mainly doing lifetime management of a resource that could be done by the IoC Container itself.
I hope this makes sense.
Regards,
Dave
|
|
|
Hi
I wondered if you have an example that demonstrates LightSpeed usage in conjunction with an IoC container. It would be interesting to see some kind of best practice with LS and IoCs in an web application that employs repositories and services.
Furthermore, it would be beneficial for you to demonstrate how one can accomplish this with usage of UnitOfWorkScope and PerRequestUnitOfWork (and alternatives that provide more fine-grained control), for instance. The examples in the express version are too simple and don't cut it.
Not being able to see how LS is used with this somewhat a show-stopper.
Regards,
A.H.Johannessen
|
|
|
Hi A.H.Johannessen |
|
|
Very timely post as I just did this with StructureMap with the MVC Framework. Check this thread as I used something very similar: http://www.mindscape.co.nz/forums/Thread.aspx?ThreadID=1678 We definitely need some guidance from Mindscape on how to handle dependency injection of the LightSpeedContext, UnitOfWorkScope, and Repositories using a dependency injection container. In most cases you want the DI to handle lifetime management of these objects and not have them hooked onto an HttpApplication, Controller, or some other spot shown in the Lightspeed Tutorials. Hopefully John-Daniel and Ivan can put together some real-world samples on how to handle the registration and lifetime management of these objects using DI for both ASP.NET and the MVC Framework. I particularly am interested in samples with StructureMap as well! Regards, Dave
|
|
|
Hi guys, I'll put together a sample using an IoC container (I'll use SM since that's what you're discussing here) over the next few days and update this thread once it's ready. We're planning to improve upon all the samples as part of LightSpeed 3.0 so having your feedback on this early on will help ensure we're hitting the right marks for the IoC web sample. Cheers, John-Daniel Trask |
|
|
Excellent, looking forward to see the sample.
|
|
|
Fantastic! Regards, Dave |
|
|
Hi guys, I have attached a first cut example of using LightSpeed with StructureMap to this forum post. I have borrowed some of the ideas from the post by A.H.Johannessen but have altered things a bit. This sample includes:
A couple of things to note:
The sample, as stated, is a first cut. It effectively allows you to view, edit and create Product's in a simple SQLite database. I've deliberately kept some aspects light (e.g. no validation error messages, no invariant checking etc) to ensure that we're only demonstrating IoC usage at this stage. We are planning to ship a larger sample with LightSpeed 3.0 which covers delivering a larger scale solution using LightSpeed on the Web. I have also attempted to comment all the key areas of the sample - if you think there could be improved comments somewhere then let me know :-) We like working with the community on things like this so I appreciate any feedback you have - we're fairly new to StructureMap so there may be better ways of doing things (Ninject is our preference generally). Thanks, and hope this helps, John-Daniel |
|
|
I see I will need to spend some time with LightSpeedModelBinder. Not exactly sure what is all going on there. Have a few seconds to explain it briefly? I could use more comments there The only question ( feedback ) I have at the moment on the DI piece is that I noticed you are not disposing of the UOW at the end of each request. Is this not necessary? Some containers, like Autofac, are fanstastic at creating a RequestContainer and disposing of all per-request objects that implement IDisposable. Not sure if Ninject does that, but I know StructureMap and Unity do not. StructureMap has a new convenience method that you can call at the end of the request to clean up HttpContext and call IDisposable when appropriate on the disposable items: HttpContextBuildPolicy.DisposeAndClearAll(); Still, I am wondering where you guys stand with the necessity of calling dispose on your UOW? As a proven practice I always like to do it, but I notice you are not in your sample which makes me think it really isn't necessary? Regards, Dave |
|
|
Hi David :) re: Dispose - yes, this should be called on the UnitOfWork to clear up the associated connection resources. Thanks for pointing out that StructureMap needs the additional call, JD will add that in to the sample - we normally use Ninject so were having fun working out the syntax translation between the two :) re: LightSpeedModelBinder - this class just allows for entities to be automatically unbound from a form so that you can take an entity as an argument to one of your action methods. If you have a look at what it is doing, basically it walks the internal type model filling in the value fields as possible from the submitted parameters. We actually have a simpler version of this in our MVC extensions library, however that is trusted by the Framework and the sample is not, so we had to use a bit of reflection hacking in this version of the sample :)
Feel free to fire out any more q's you have on this.
Jeremy |
|
|
Jeremy, Is your MVC Extensions Library something that comes with Lightspeed? I am not at my developer machine to look for it, but this is the first I have heard of it. The MVC Framework will probably be out any day now and I would love to see how you guys are working with it. Thanks, Dave |
|
|
Hi David, No - its not something we have currently released but we will likely have something out in the near future :) I am currently putting up some posts on my blog about our experiences with the MVC framework and LightSpeed, so in the mean time you could check those out :) Are you currently working on some MVC implementations?
Jeremy |
|
|
Hi Jeremy, My team is only focusing on the MVC Framework right now and have been doing so for quite some time. Up to this point we have been using NHibernate and LINQ To SQL, but I am ready to go full steam with Lightspeed as a replacement for LINQ To SQL for sure. I am hoping for some ASP.NET MVC bits from you guys soon than later as the MVC Framework is probably being released this week at MIX. As we speak I am converting your StructureMap IoC Example for use with Autofac on one of my websites ;) Regards, Dave
|
|
|
No problem - we are equally keen :) Since we seem to have quite a few people who are already working with LightSpeed and ASP.NET MVC we are looking to set up a community project for extensions and contributions shortly - any bits you have will be most appreciated :)
Jeremy |
|
|
A quick follow up to this - we have set up a community project for MVC extensions to LightSpeed (see: http://www.mindscape.co.nz/blog/ for details) so if you have some to share we would love to include then :)
Jeremy |
|