简体   繁体   English

在解决方案中的多个项目之间共享LINQ-to-SQL类/模型

[英]Sharing LINQ-to-SQL classes/models between multiple projects in a solution

I have multiple C# projects in a Visual Studio solution right now that will all use the same SQL Server database. 我现在在Visual Studio解决方案中有多个C#项目,它们都将使用相同的SQL Server数据库。

What is the proper way to share LINQ-to-SQL classes between projects? 在项目之间共享LINQ-to-SQL类的正确方法是什么?

I'm considering just copying the dmbl files into each project, but I think that may be too redundant. 我正在考虑将dmbl文件复制到每个项目中,但我认为这可能过于冗余。 Is there a better way to approach this? 有没有更好的方法来解决这个问题?

I suppose you need to share database access code between projects not linq-2-sql classes as they are. 我想你需要在项目之间共享数据库访问代码而不是linq-2-sql类。

Build Data Access Library ( DAL ) that expose operations over database that are implemented via linq-2-sql classes rather then expose classes. 构建数据访问库( DAL ),它通过linq-2-sql类实现数据库上的操作 ,而不是公开类。

I'd suggest to use Repository Pattern 我建议使用Repository Pattern

Refer DAL library from projects you want. 从您想要的项目中引用DAL库。

You could: Build it as a class library and include it. 您可以:将其构建为类库并包含它。 It is just a class, right. 这只是一个类,对吧。

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. 如果它们在同一个解决方案中,您可以在需要访问Linq类的项目中添加对Linq项目的引用。 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. 您可以通过右键单击需要访问Linq类的项目的“References”文件夹来执行此操作,选择“Projects”选项卡并选择Linq项目。

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. 如果您正在访问严格的数据访问,请遵循Andrew Florko的建议并构建数据访问层以获取所需的数据。

What you need to do is create a Class Library that contains your DBML classes. 您需要做的是创建一个包含DBML类的类库。 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. 然后做Andrew Florko建议的内容并阅读数据访问模式。 Repository is nice, though I like the DAO . 存储库很好,虽然我喜欢DAO

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. 我不仅将Model类,而且将整个数据访问层(DAL)分离到您从应用程序的其他部分引用的单独程序集中。

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. DAL将封装它与数据库交互的详细信息(在本例中使用LINQ to SQL),并将它们隐藏在对应用程序有意义的定义良好的接口之后。

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. 此外,由于从LINQ to SQL DBML文件生成的类只是普通的旧CLR对象(PO​​CO),因此我没有看到将它们作为DAL接口的一部分直接公开的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM