Skip to content

An example container for implementing dependency injection does not MVC5

License

Notifications You must be signed in to change notification settings

jefferson/mvc5-container-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A simple implementation os IoC Container for AspNet. MVC5

An example of inversion of control through Dependency Injection(DI)

How to use

First create your services, example:

//code omitted for simplicity
 public interface IService
 {
      string TagIsOnline(string tag);
 }
//code omitted for simplicity
public class MyService : IService
{
    public string Myservice(string tag) => $"{tag} is online";
}

Now create your container and register services

//code omitted for simplicity
namespace Containers.Container
{
    public static class Startup
    {
        public static void Configure(IContainer container)
        {

            #region register controllers
            //Is not necessary register Controllers
            //container.Register<HomeController, HomeController>(LifeCycle.Transient);
            #endregion

            container.Register<IService, Service>();

        }
    }
}

And initiate and register the container in your Globa.asax

//code omitted for simplicity
var container = new IocContainer();
Startup.Configure(container);
ControllerBuilder.Current.SetControllerFactory(new ControllerFactory(container));

In the last step, now you can use your service into Controller

//code omitted for simplicity
private IService service;

public HomeController(IService service) => this.service = service;
  • It is important to note that Control Class, Example HomeController, are automatically register by the containers.
  • The purpose of this code is only for study.
  • This code is based on this article, published in 2010 by Tim Ross: Creating a Simple IoC Container