Project Reference Vs Dll Reference in Visual Studio

While developing any application we need to separate the application into different projects. In many cases we need to use the functionality from one project into the other. This article explains available options to achieve this.

For this I have created a solution having two projects in Visual Studio:

  1. ArthimaticClassLibrary: I have added Add method that will do the sum of two integers.
  2. SampleMvcApplication: This just calls the Add method from the ArithmaticClassLibrary and show the sum in view.

SampleMvcApplication

ArthimaticClassLibrary project has an add method. The code of the same is shown below.

add method

So here Add method takes two integers and returns the sum. I have used this Add method inside the HomeController of SampleMvcApplication as:

Code

In order to use Add method (which is in ArthimaticClassLibrary project) as above, we must add reference of ArthimaticClassLibrary to SampleMvcApplication.

There are two options to do it:

  1. File reference or dll reference
  2. Project-to-Project Reference

File Reference or dll reference

In order to add reference of ArthimaticClassLibrary to SampleMvcApplication, build the ArthimaticClassLibrary project so that it will generate the ArthimaticClassLibrary.dll file in the bin folder.

Right click on SampleMvcApplication and select the Add Reference option.

Add reference

Now click on the browse button in the dialog and locate the ArthimaticClassLibrary.dll which will be in the \bin\debug path of ArthimaticClassLibrary project.

Project-to-Project References

For adding the reference of ArthimaticClassLibrary to the SampleMvcApplication by project reference approach, right Click on SampleMvcApplication and select the Add Reference option. In the dialog, expand the Solution option in left pane, click on projects and select the ArthimaticClassLibrary project and click on Ok as shown in the below snap.

click on Ok

Now the question is when to use the File reference or dll reference option and Project-to-Project Reference?

If the project or source code is already there in the solution (like in our case), we should always go with Project-to-Project Reference approach. Following is an advantage of this approach.

Project dependency between the projects will be automatically created by Visual Studio. To see the created dependency with Project To Project Reference option (If you have added the reference of ArthimaticClassLibrary to SampleMVCApplication with Project To Project Reference ), right click on Solution and select properties, a dialog will appear as show in the snap.

ArthimaticClassLibrary

So due to these automatically created build dependencies (where SampleMvcApplication depends on ArthimaticClassLibrary) the dependent project (ArthimaticClassLibrary) will be built if it has changed since the last time the referencing project (SampleMvcApplication) was built.

A file reference does not create a build dependency, so it is possible to build the referencing project without building the dependent project, and the reference can become obsolete. (That is, the project can reference a previously built version of the project.)

So when should we use dll reference option? We need to use File reference or dll reference when the corresponding project or source code is not available in the solution. For example, for referring the third part libraries we can use this approach.

Read more articles on Visual Studio:


Similar Articles