StructureMap can inject the necessary dependencies seamlessly in your WebAPI controllers Credit: IDG Dependency injection enables you to inject dependencies in your application and in doing so, you can separate the implementation from the interface. In essence, dependency injection enables you to change the dependencies (the implementations) without having to change the types that need those dependencies. Here the term dependency implies a dependent object that needs one or more objects or dependencies for it to operate. In an earlier post here, I presented a discussion on how we can implement dependency injection using NInject. This time, we will explore another dependency injection framework named StructureMap. It should be noted that StructureMap is one of the popular and widely used Dependency Injection (DI) frameworks around. This article presents a discussion on how we can work with StructureMap to inject dependencies in WebAPI. Getting started using StructureMap To get started using StructureMap in WebAPI, you should first create a WebAPI application. To do this, follow these steps outlined below. Open Visual Studio IDE (I’m using VS.NET 2017, but you can also use any earlier version as well) Create a new WebAPI project Now install the necessary StructureMap package via NuGet Package Manager Once StructureMap has been successfully installed in your WebAPI project, you would observe that two files, namely Structuremap.Mvc.cs and Structuremap.WebApi.cs has beed addedd to the App_Start solution folder. And, another solution folder named DependencyResolution will be added with a list of files added to it. Implementing dependency injection In this section we will implement a simple application to demonstrate how we can work with StructureMap. Consider the following types — the IProductRepository interface and the ProductRepository class. The ProductRepository class implements the IProductRepository interface. Note that I have provided the structure of the class — you can write the necessary implementation as needed. public interface IProductRepository { IEnumerable<Product> GetAll(); Product Get(int id); } public class ProductRepository : IProductRepository { public IEnumerable<Product> GetAll() { //Write your implementation here return null; } public Product Get(int id) { //Write your implementation here return null; } } The ProductsController uses the ProductRepository class to retrieve data as shown below. public class ProductsController : ApiController { private IProductRepository _repository; public ProductsController(IProductRepository repository) { _repository = repository; } public IHttpActionResult GetAll() { var products = _repository.GetAll(); return Ok(products); } public IHttpActionResult GetByID(int id) { var product = _repository.Get(id); if (product == null) { return NotFound(); } return Ok(product); } } As you can see in the ProductController class given above, a reference to the repository interface, i.e., the IProductRepository interface is available. This would ensure that if the implementation of the ProductRepository changes, the types that depend on the implementation, i.e., the ProductsController would not be affected. The next thing that you should do is register the ProductRepository with the Dependency Injection container. To do this, you should specify the following statement in the DefaultRegistry.cs file present inside the DependencyResolution folder. For<IProductRepository>().Use<ProductRepository>(); The DefaultRegistry class would now look like this. public class DefaultRegistry : Registry { #region Constructors and Destructors public DefaultRegistry() { Scan( scan => { scan.TheCallingAssembly(); scan.WithDefaultConventions(); }); For<IProductRepository>().Use<ProductRepository>(); } Lastly, you should write the necessary code to start the DI container. To do this, specify the following statement in the Register method of the WebApiConfig.cs file. StructuremapWebApi.Start(); And that’s all you have to do. You can now test your application and setup breakpoints to see where and how the dependency is being injected at run time. Dependency injection helps you to build pluggable implementations in your application — it eliminates the hard-coded dependencies between the types and makes your types easier to build, test, and maintain over time. I will write more on DI containers in my future posts here. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos