简体   繁体   English

添加项目参考与添加 DLL 参考

[英]Add Project Reference vs Add DLL Reference

I am newbie in .net.Today I have created a sample.我是 .net 的新手。今天我创建了一个示例。 In that sample, I have stored data in database using N Tier architecture.在该示例中,我使用 N 层架构将数据存储在数据库中。 If I want to use to Use BL or DAL method in other project(I mean BL method in UI or DAL method in BL), I found two ways.如果我想在其他项目中使用 BL 或 DAL 方法(我的意思是 UI 中的 BL 方法或 BL 中的 DAL 方法),我找到了两种方法。

First one is - Right click on project << Add Reference << Select Project in Project tab第一个是- 在项目选项卡中右键单击项目 << 添加参考 << Select 项目

Second one is - Right click on project << Add Reference << Select DLL in Browse tab第二个是- 右键单击项目 << 添加参考 << Select DLL 在浏览选项卡

Could anyone tell me that is there any difference between both of them as both works same.Is DLL way better then Project Reference.If yes, then what is the benefits?谁能告诉我它们两者之间有什么区别,因为它们的工作原理相同。DLL 是否比项目参考更好。如果是,那么有什么好处?

Thanks in advance.提前致谢。

Regards, Mohit Kumar.问候,莫希特库马尔。

The correct way to do it is to add a project reference.正确的做法是添加项目引用。
one of the most important differences is that a project reference is updated automatically when you change the referenced project.最重要的区别之一是当您更改引用的项目时,项目引用会自动更新。
for example- If you change your DAL method from GetEmployees() to GetAllEmployees() then you can use GetAllEmployees() immediately in your BL class, without compiling your DAL first.例如,如果您将 DAL 方法从GetEmployees()更改为GetAllEmployees() ,那么您可以立即在 BL class 中使用GetAllEmployees() ,而无需先编译 DAL。

You typically use the project reference when the reference is part of your solution.当引用是您的解决方案的一部分时,您通常会使用项目引用。 In this way the relevant latest code is always used.这样,始终使用相关的最新代码。

You will reference an assembly when it is a third party component or not part of your solution.当程序集是第三方组件或不是您的解决方案的一部分时,您将引用它。 This is somewhat more static as you will be using the code at the version represented by the assembly.这有点更多 static 因为您将使用程序集表示的版本的代码。 So any changes to the assembly will require you to physically overwrite the referenced assembly with the updated dll.因此,对程序集的任何更改都需要您使用更新的 dll 物理覆盖引用的程序集。

HTH高温高压

I have came across a very interesting difference between the two approaches, but in the context of having 2 projects, one shared among multiple developers and one private to each developer.我发现这两种方法之间有一个非常有趣的区别,但是在有 2 个项目的情况下,一个在多个开发人员之间共享,一个对每个开发人员私有。

Let us say that the shared VS project is called projectS, and the private project is called projectP让我们说共享VS项目称为projectS,私有项目称为projectP

Now if the objective is to have a centralized development, and the developer needs to access source definitions from both projectP and projectS so that pressing "F12" or "Go To Definition" in VS would bring complete definition, then we have to use the Project Reference and not the DLL reference, otherwise pressing F12 would bring the definition from the compiled "metadata" excluding all developer comments and other relevant data.现在如果目标是集中开发,开发者需要同时访问 projectP 和 projectS 的源定义,以便在 VS 中按“F12”或“Go To Definition”会带来完整的定义,那么我们必须使用 Project参考而不是 DLL 参考,否则按 F12 将带来编译后的“元数据”中的定义,不包括所有开发人员评论和其他相关数据。

When adding a Project Reference to projectS from within projectP, VS will resolve references to the files included in projectS and referred to from projectP using the source definition in projectS, and not from "metadata" of the DLL associated with the referenced project (ProjectS.dll).从 projectP 中添加对 projectS 的项目引用时,VS 将解析对 projectS 中包含的文件的引用,并使用 projectS 中的源定义从 projectP 引用,而不是来自与引用的项目(ProjectS. dll)。 Still, the projectS.dll will be included in the References folder and Development time, Execution time will be OK.尽管如此,项目S.dll 将包含在References 文件夹和开发时间中,执行时间就可以了。

When adding a DLL reference, VS will resolve references from "metadata" stored in ProjectS.dll, even if the Referenced Project was added to the Solution of projectP as an "Existing Project".添加 DLL 引用时,VS 将解析存储在 ProjectS.dll 中的“元数据”中的引用,即使引用的项目已作为“现有项目”添加到 projectP 的解决方案中。 This would allow Execution time to be OK, however, Developer will not be able to press F12 and go to Source definition in projectS, he/she will have to do this manually from the solution search area.这将允许执行时间正常,但是,开发人员将无法按 F12 和 go 到 projectS 中的源定义,他/她必须从解决方案搜索区域手动执行此操作。

The differences between the 2 have already been answered, however just to add to that, I think the only difference between the 2 in VS is that if you add project reference it still adds a normal dll reference, except the reference is to the Debug location of the projects binaries (ie bin\debug\project.dll), so in essence you could probably just Add Reference in the same way and point straight to the latest compiled dll.已经回答了 2 之间的差异,但是补充一点,我认为 VS 中 2 之间的唯一区别是,如果您添加项目引用,它仍然会添加正常的 dll 引用,除了引用是调试位置项目二进制文件(即 bin\debug\project.dll),所以本质上你可能只是以相同的方式添加引用并直接指向最新编译的 dll。

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

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