Sunday, April 30, 2017

Dependency- External DLLs Management in #Sitecore with NuGet



Dependency, Dependency, Dependency!!


There is the number of Library/framework/External DLLs reference uses in the most large .Net/Sitecore Projects. 

These dependencies have their own dependencies and those dependencies further depend on some other library and so on. So it’s may be more complex
Dependencies >> Dependencies >> Dependencies >> Dependencies 
The problem of dependency management is neither new nor original, it exists in all development platforms, and .NET is no different.

A very big question: Where to store external DLL files in your Sitecore based solution?

The first thing came into the mind is Lib folder in the solution, create the Lib folder and put all the required DLLs into this folder and check-in the folder in the source control.

Keeping dependencies in the source control that’s a very popular solution, and for a reason. The benefits are obvious like:

  • No setup. You already have your source control in place (hm, I hope you do!). Add \bin directory, and you are fine.
  • No learning curve. Developers are used to work with source control.
  • Shared. The whole team gets changes and updates from the server as they occur.

·        
Sounds good, doesn’t it? So, what’s wrong with it? Source control software is designed to control sources code well. As such they aren’t so great in controlling binaries. It’s not a good practice to store third party DLLs (binaries) into the source control.

The biggest problem with this approach is version upgrade, manually we need to upgrade the project reference version, let’s take one example of the popular IoC framework Ninject.

Between Jan 1, 2011 and Apr 5, 2011, there have been four different versions (2.0.1, 2.1.0.76, 2.2.0.0, 2.2.1.0). Not too many, but multiply that against a project with six or seven dependencies, and then add in the cross dependencies, and it has a nightmare.

No worries now NuGet is here.

“NuGet is a package management system for .NET. The goal of NuGet is to make the process of incorporating third party libraries into your solutions as simple as possible.”

Let’s look the great features of NuGet which makes the developer’s addicted to using this.

  • Manages packages, not dlls.
  • Provides NuGet Gallery – almost 70K packages are at your disposal for all your development needs.
  • Supports binary versioning.
  • Integrates with Visual Studio.
  • Integrates with your build.
  • Integrates with your build server

And the good thing is Sitecore also supports the NuGet package management system for distributing Sitecore package, that’s really icing on the cake for us.
Now you can manage all your third party dependencies from Nuget Package Along with Sitecore DLLs as well.


So it’s always advisable to add the Nuget package from the solution and then select the projects which required a specific package.

On a large project, you often wind up adding new projects over time, often weeks or months apart. If you are always installing packages one project at a time, and a package author happens to release an update in between your two installs, you may wind up with two projects in your solution referencing different versions of a package.

That is one of the reasons I personally preferred NuGet package from the solution only.

How to Add Third party DLLs Reference in your solution using NuGet

  1. Select the solution in solution explorer
  2. Right Click and select Manage NuGet Packages for solution…
  3. Select the  packages
  4. Now simply locate your new projects and tick the checkbox next
  5. Click Install, sit back and relax


Let’s take the example of Newtonsoft.Json Dlls that need to be included in the project reference:

Open the solution:

  1.          Right click on the solution
  2.          Select the Manage Nuget Packages for Solution Option > Click
 





Once you install this> check the output >






The newly added package has been installed and also reference has been added in the project as well.

Now we can add this as a namespace as below:



How to use Sitecore Assembly Reference in your solution using NuGet

Now we can also add the Sitecore dependency DLLs by Nuget,
Below are the steps:

Open the solution:
  1. Right click on the solution
  2. Select the Manage Nuget Packages for Solution Option > Click




Go to the package source

Click on property
Add the new Source by click on Add Icon in green as highlighted



Enter the name and Source Url:
Name: Sitecore



I added the new package source called Sitecore as below:



Now, I am going to add the reference of Sitecore. Kernel DLL in my project:
  1.           Select the Sitecore as source
  2.           Search the Kernel DLL
  3.           Select the Sitecore.Kernel DLL
  4.           Select the project, on which you need to add the reference
  5.           Click on install







Oops, There is Error for framework compatibility

“Could not install package 'Sitecore.Kernel 8.2.170407'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.”

Make sure your project framework and package framework must be compatible, check the dependency of the package before installing.





And check my project framework version:


I just added another Sitecore reference > Sitecore.Analytics.Sql.NoReferences.
This DLL has no dependency, so I can quickly add this just for the demo.



Sitecore reference has been added in the project,



In the same way, we can also add the Sitecore reference which has dependencies for example Sitecore.Kernel



 Now check the refrence in the code.


  





I hope this article will help you for Sitecore DLLs references management using nuget

If you have any questions or concerns, please get in touch with me on twitter @sitecore_ashish or on Slack).

Happy Sitecoring