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 Guys, IOC/DI is all new to me, and to help me with understanding it all, are there any complete and simple LightSpeed examples based on MVC3 and Ninject? Thanks for any help you can offer! Cheers |
|
|
There will be a couple of MVC3 based samples provided as part of the LightSpeed 4 sample set and these are not too far off being in the LightSpeed 4 nightlies. They do not include any IoC container use however this isnt really something which is specific to LightSpeed. My suggestion would be to get familar with Ninject by walking through the tutorial on the Wiki (https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand) and then look at what it is you are wanting to inject. Can you elaborate on what you are looking to achieve? We might be able to help with some more suggestions :)
Jeremy |
|
|
Thanks Jeremy, I have looked at this, and I am unsure if this is actually what I need :) I also understand this is somewhat outside of the scope of lightspeed support, but I'd like to understand some best practices here... Lets say I have a project with a lightspeed model which contains two entities for Users and Widgets, and a user has multiple Widgets. There is a partial class for User with a ValidateUser method which accepts a unitOfWork, username and password I have a MVC3 web app which uses the LSmodel project, and a custom MembershipProvider. in global.asax I declare... public static readonly LightSpeedContext<modelUnitOfWork> DbContext = new LightSpeedContext<modelUnitOfWork>("Default"); And I have a default controler which the others inherit from, so they have easy access to UOW using System.Web.Routing; using System.Web.Mvc; using Mindscape.LightSpeed; using Mindscape.LightSpeed.Logging; using appModel; public class DefaultController : Controller { private UnitOfWorkScopeBase unitOfWorkScope; private UnitOfWorkScopeBase UnitOfWorkScope { get { #if DEBUG MvcApplication.DbContext.Logger = new TraceLogger(); #endif if (this.unitOfWorkScope == null && System.Web.HttpContext.Current != null) this.unitOfWorkScope = System.Web.HttpContext.Current.Items["UOW"] as PerRequestUnitOfWorkScope; if (this.unitOfWorkScope == null) { this.unitOfWorkScope = new PerRequestUnitOfWorkScope(MvcApplication.DbContext); if (System.Web.HttpContext.Current != null) System.Web.HttpContext.Current.Items["UOW"] = this.unitOfWorkScope; } return this.unitOfWorkScope; } } protected modelUnitOfWork UoW { get { return UnitOfWorkScope.Current; } } protected override void OnResultExecuted(ResultExecutedContext filterContext) { if (this.unitOfWorkScope != null) this.unitOfWorkScope.Dispose(); base.OnResultExecuted(filterContext); } public new void Dispose() { if (this.unitOfWorkScope != null) this.unitOfWorkScope.Dispose(); base.Dispose(); } } Then the custom membership code looks like this... using System.Web.Security; using Mindscape.LightSpeed; using Mindscape.LightSpeed.Logging; using appModel; class CustomMembershipProvider : MembershipProvider { private static readonly LightSpeedContext Context = new LightSpeedContext("Default"); private readonly modelUnitOfWork uow; public CustomMembershipProvider() { Context.Logger = new TraceLogger(); uow = Context.CreateUnitOfWork(); } ~~~~~~~~ 8< snip >8 ~~~~~~~~ public override bool ValidateUser(string username, string password) { return User.ValidateUser(uow, username, password); } ~~~~~~~~ 8< snip >8 ~~~~~~~~ } Can I improve what I am doing here, as in, is there a better way to make the UOW available to my view controllers. In the customer membership controller I have to create a UOW in there, which I pass over to the model for use in the ValidateUser method - Should I be doing this at all, or should I create a new UOW inside the model to handle the function (this is what I have done in the past)? Thanks for any advice you can offer :) Cheers |
|