views:

142

answers:

5

I have multiple C# projects in a Visual Studio solution right now that will all use the same SQL Server database.

What is the proper way to share LINQ-to-SQL classes between projects?

I'm considering just copying the dmbl files into each project, but I think that may be too redundant. Is there a better way to approach this?

+1  A: 

You could: Build it as a class library and include it. It is just a class, right.

Curtis White
+3  A: 

I suppose you need to share database access code between projects not linq-2-sql classes as they are.

Build Data Access Library (DAL) that expose operations over database that are implemented via linq-2-sql classes rather then expose classes.

I'd suggest to use Repository Pattern

Refer DAL library from projects you want.

Andrew Florko
A: 

If they are in the same solution you could add references to the Linq-project in the projects that needs to access the Linq-classes. Or am I misunderstanding your question? You do this by right-clicking the "References" folder of the project that needs to access the Linq-classes, select the "Projects" tab and the select the Linq-project.

If it is strict data access you are after, follow Andrew Florko's advice and build a Data Access Layer to get the data you need.

daft
A: 

What you need to do is create a Class Library that contains your DBML classes. Then you add that as a reference to your other projects. Make sure there's nothing in that class library except for database-related code so it's easy to add to future projects.

Then do what Andrew Florko suggested and read up on data access patterns. Repository is nice, though I like the DAO.

roufamatic
A: 

I would separate not only the Model classes but the entire Data Access Layer (DAL) into a separate assembly that you reference from other parts of your application.

The DAL will encapsulate the details of how it interacts with the database (in this case the usage of LINQ to SQL) and hide them behind a well-defined interface that is meaningful for the application.

Also, since the classes generated from the LINQ to SQL DBML file are just Plain Old CLR Objects (POCO), I don't see a problem in exposing them directly as part of your DAL's interface.

Enrico Campidoglio