Sql – linq2sql using with dependency injection

dependency-injectionioc-containerlinq-to-sqlunity-container

Does anyone know how to have dependency injection work with linq2sql. Heres my situation..

I will explain it as best i can here.

I have a base class which has a DBML (linq2sql) and classes etc .. This DBML is COMMON to more than 1 project.. Well each project has its own DBML but has all the tables etc that is in the common dbml i am using in the base class – does that make sense?!

Each of my projects creates a new class by inheriting the base class and extending it… but of course i need to REINJECT my dbml because the dbml that i use specifically in my project has all the functionality that was in COMMON and then some

I am a little lost here.. Anyone know how to achieve this.. I do hope i explained it well enough 🙂

I was hoping to use unity or something similar, the classes that are created by linq2sql don't seem to implement interfaces… is this going to be a issues with DI?

Thanks

Best Answer

It sounds like you probably don't need a DBML in every project. I would recommend having one project "MyCoolApp.Entities" that contains your Linq to SQL entities, and then reference that project in your other projects. Those other projects can extend your base entities as needed.

As for Dependency Injection, Unity can definitely resolve dependencies that don't implement interfaces so that shouldn't be a problem.