---
uid: module-building.templates-csharp.how-to-add-nuget-dependencies-csharp
description: "How to declare NuGet dependencies in a C# template using AddNugetDependency so the Software Factory automatically adds the required PackageReference."
---
# How to add NuGet dependencies (C#)

Often source code generated by Templates will require that certain NuGet packages are installed. Templates allow you to define such dependencies by using the `AddNugetDependency` method.

First, create a static class (if not already created) that will contain the NuGet package references. By having the NuGet package references defined in a central class, multiple Templates can make use of the references and the versions can later be updated in a central location.

```csharp
using Intent.Modules.Common.VisualStudio;

namespace Intent.Modules.Application.AutoMapper;

public static class NugetPackages
{
    public static INugetPackageInfo AutoMapper = new NugetPackageInfo("AutoMapper", "12.0.0");
}
```

Then in your Template you can use the `AddNugetDependency` method:

```csharp
[IntentManaged(Mode.Merge, Signature = Mode.Fully)]
partial class MappingProfileTemplate : CSharpTemplateBase<object>
{
    [IntentManaged(Mode.Fully)]
    public const string TemplateId = "Intent.Application.AutoMapper.MappingProfile";

    [IntentManaged(Mode.Merge, Signature = Mode.Fully)]
    public MappingProfileTemplate(IOutputTarget outputTarget, object model = null) : base(TemplateId, outputTarget, model)
    {
        AddNugetDependency(NugetPackages.AutoMapper);
    }
...
```

> [!IMPORTANT]
> The `AddNugetDependency` method call must be invoked in either the `Constructor` or in the overridden `BeforeTemplateExecution` method as the updates to the Visual Studio Project files may not reflect the when the Software Factory execution occurs.

Once the Template executes in the Software Factory, it will introduce a `PackageReference` in the corresponding Visual Studio Project file:

```xml
<ItemGroup>
    <PackageReference Include="AutoMapper" Version="12.0.0" />
    ...
</ItemGroup>
```

> [!NOTE]
> Actual package restoration is still handled by your IDE (`Visual Studio`, `Rider`, etc.) / `dotnet` commands for package restoration. Refer to the NuGet documentation on how to manage package sources.
