Using dependency injection in Web API applications using Unity
Step 1: Create a new ASP.NET Web application Step 2: Install Unity through Nuget At the current time of writing the version used in the Package Manager Console was as follows: [code language="text"] PM> Install-Package Unity -Version 5.7.3 [/code] Step 3: Create a new repository We add this to the Model folder. When creating the controller in the constructor, We like to specify that we would like an interface to the repository – eg when we come to specify tests at a later stage, when don’t have to specify a specific repository, we can be much more de-coupled, but mainly to abstract it out for testing. In this example my 'repository' is just a property that returns a list of strings. IRepository.cs [code language="csharp"] using System.Collections.Generic; namespace WebApiDepInject.Models { public interface IRepository { IEnumerable<string> MyValues { get; set; } } } [/code] Repository.cs [code languag...