.NET Core and IoC

If you are concerned about decoupling your code, then you know that IoC can help you with this.

Maybe you had used castle before, or even ninject, but did you know that .NET core comes with a basic implementation of IoC?

Yes! You don’t need to use external classes or make hard configurations, because decoupling in dotnet core is as easy as call an extension method!

It’s not my intention here to explain what is IoC or whether you have to use, but I suppose that you have some experience with IoC (not so much) and want to have a quick start with .net core.

So, let’s get started!

Quickly, what I must do to start using Dependency Injection?

You just need to tell .Net Core where is the implementation for a specific interface and if you want to do a singleton, scoped or transient instance.

Transient

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.

Scoped

Scoped lifetime services are created once per request.

Singleton

Singleton lifetime services are created the first time they are requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behaviour, allowing the services container to manage the service’s lifetime is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in the class yourself.

source

You can do that at Startup.cs file on project’s root folder.

Here an example:

public void ConfigureServices(IServiceCollection services)
{
    // Add own services
    services.AddSingleton<IMyService, MyService>();

    // Add framework services.
    services.AddMvc();
}

I did not forget any step. You do not need to do anything else.

Now you can inject MyService class by constructor like this:

using System;
using Microsoft.AspNetCore.Mvc;
using IocExample.Services;

namespace IocExample.Api.Controllers
{
    [Route("api/[controller]")]
    public class GreetingController : Controller
    {
        private readonly IMyService _service;
        public GreetingController(IMyService service)
        {
            _service = service;
        }

        [HttpGet]
        public string Get()
        {
            return _service.DoHelloWorld();
        }
    }
}

And if I have more than one layer in multiple projects?

I created a mechanism to construct these mappings where each layer is responsible to their own mapping.
You can see this accessing this project on GitHub.
Take a look into this classes:
  • IocExample.Domain.Services.DI.DiMapping
  • IocExample.Api.Startup (lines 36-38)
  • All service and repositories files, interfaces and impl
  • IocExample.Api.Controllers.GreetingController

Tip: Go to implementation in VSCode: Ctrl + F12 😉

And let me know if you like it and if it was useful to you! =)

See you!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s