views:

97

answers:

2

I have done some searching around but have not been able to figure out how to bind LinqToSql data context's with specified connection strings into different repositories.

This binding is performed in global.ajax when routes are registered. I'm using a fairly standard repository pattern to decouple the LinqToSql infrastructure from my application so that I can change the technology infrastructure in the future (NHibernate, EntityFramework etc.). Additionally, make my code easily unit testable and mockable.

I have something like this.

string desktopConnectionString = ConfigurationManager.ConnectionStrings["Desktop"].ConnectionString;
string messagingConnectionString = ConfigurationManager.ConnectionStrings["MessageQueue"].ConnectionString;
string usersConnectionString = ConfigurationManager.ConnectionStrings["Users"].ConnectionString;

// Map linqToSql entity types to interfaces
TableMapper typeFinder = new TableMapper();
typeFinder.Define<EzsDashboard>().As<IDashboard>();
typeFinder.Define<EzsDashboardGadget>().As<IGadget>();
typeFinder.Define<EzsDashboardGadgetAssignment>().As<IGadgetAssignment>();
...

IDesktopRepository dr = new DesktopRepository(new LinqToSqlDataSource(new DataContext(desktopConnectionString), typeFinder)));
Bind<IDesktopRepository>().ToConstant(dr).InRequestScope();

IUserRepository ur = new UserRepository(new LinqToSqlDataSource(new DataContext(usersConnectionString), typeFinder)));
Bind<IUserRepository>().ToConstant(ur).InRequestScope();

IMessageRepository mr = new MessageRepository(new LinqToSqlDataSource(new DataContext(messagingConnectionString), typeFinder)));
Bind<IMessageRepository>().ToConstant(mr).InRequestScope();

Whilst this physically works. I find the data in my asp.net MVC app often comes back stale. For example, I'll add a gadget to the dashboard it displays and if I check the database, it was saved correctly into the database. I reload the page and the dashboard doesn't show anything. Clicking around the application will sometimes cause it to 'refresh' and the data that was saved shows up.

Another way this is visible is if I make a changed to my web.config and trigger an application re-load. When I hit the dashboard everything shows up exactly as it should.

Can somebody please provide some help with binding connection string->DataContext->LinqToSqlDataSource->DomainRepository

A: 
Ruben Bartelink
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
@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
+1  A: 

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<IDesktopRepository>().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.

John Bledsoe
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