How to configure Ninject for ASP.NET MVC using LinqToSQL and Repository Pattern?

It looks to me like your repositories are outlasting the scope that you expect. Am I correct to assume that the code in your question is in a Ninject module or global application start up? If so, I would change each of your bindings to something like this: Bind().

ToMethod(context => new DesktopRepository(new LinqToSqlDataSource(new DataContext(desktopConnectionString), typeFinder)))).InRequestScope() I believe that you're getting the same repository across requests, because you've bound it to a constant. I would want a new repository per request, which the above code will provide.

It looks to me like your repositories are outlasting the scope that you expect. Am I correct to assume that the code in your question is in a Ninject module or global application start up? If so, I would change each of your bindings to something like this: Bind().

ToMethod(context => new DesktopRepository(new LinqToSqlDataSource(new DataContext(desktopConnectionString), typeFinder)))).InRequestScope(); I believe that you're getting the same repository across requests, because you've bound it to a constant. I would want a new repository per request, which the above code will provide.

Perfect. Your reasoning behind the repository being carried across requests due to BindToConstant sounds reasonable. I did have InRequestScope at the end of the binding but it must not have been working properly.

Your solution shaved off a few extra lines of code and fixed the stale data problem I was having. Many thanks. Btw, yes this was performed in application start; – Joshua Hayes Jul 19 '10 at 13:51.

Do you feel this is an ASP. NET issue or a DI issue? Are you cahcing stuff in a session etc.? You need to surface stuff like this (assuming I've inferred correctly that you want to dynamically be able to change your application's storage mechanism on the fly - you've stated what you are doing, not why you are doing it) as application logic.

Trying to abuse a DI container to do this for you is bad news - it doesn't retain a tree of objects it give the old configuration to, etc.Your application needs to manage this. Think of the case where you switch storage from A to B. What if something in your app is contacting A in the middle of your operation and is about to retry?

You don't want the container (or anyone else) handing it a B instead. That's not to say that there are no legitimate use cases for such things. It's just that they're definitely not a common case or something that DI containers are typically used to manage.... Or I may be missing something - in which case, it'd be interesting to hear more about your scheme and how you are attempting to map things to scopes etc.

I have updated the original question with a bit more context. I have already stated that the problem is what I believe to be stale data being returned. As I was asking specifically about ninject bindings, this is where I believe the problem to be.

– Joshua Hayes Jul 19 '10 at 13:12 @Joshua: Blad you're sorted. I'll leave my 'answer' here for completeness (unless you feel the planet would be better off without the general advice in which case I'll happily delete it) – Ruben Bartelink Jul 19 '10 at 16:02.

Ninject for asp is configured (without quotes):. Configure Ninject for ASP.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions