JDalri
JDalri

Reputation: 388

C# Dependency Injection with 3 projects on same Solution

I have a solution with separate projects. The structure is like this: 1 - the web project 2 - the Aplicacao, that is called by the controllers of 1 to do the logic 3 - the Dominio, that has the classes of my solution 4 - the Dados, that has the entity framework stuff...

So, when I have a POST to register a new user, the UsuarioController (UC) is called. The UC has to instanciate a UsuarioAplicacao (UA) passing a usuario. The UA has to instanciate a UsuarioRepositorio (UR) passing a usuario as parameter and here it will be salved to the database.

I'm used to instanciate the class I need inside the constructor of the class, like so:

public ActionResult Registrar (Usuario usuario)
    {
        if (ModelState.IsValid)
        {
            var _usuarioApp = new UsuarioAplicacao();
            _usuarioApp.Salvar(usuario);
            rest of the code...
        }
    }

But, while studing, I've learned that this isn't quite right. I should use the dependency injection (DI)...

So, using DI with the constructor, I can't figure out how to do it...

I've tried like this in my controller:

private UsuarioAplicacao _usuarioAplicacao;

    public UsuarioController (UsuarioAplicacao usuarioAplicacao)
    {
        this._usuarioAplicacao = usuarioAplicacao;
    }

then, on my UsuarioAplicacao (class that do the logics and call UsuarioRepositorio to save the object into the DB):

private readonly UsuarioRepositorio _usuarioRepositorio;
public UsuarioAplicacao (UsuarioRepositorio usuarioRepositorio)
{
    this._usuarioRepositorio = usuarioRepositorio;
}

and finnaly, inside my UsuarioRepositorio (class responsable for saving tha data into the DB via Entity Framework):

private readonly Contexto _contexto;
public UsuarioRepositorio(Contexto contexto)
{
    this._contexto = contexto;
}

(_contexto is my EF context class)

These are my onstructors. But I'm getting Null Reference Exceptions...

Can you guys help me with dependency injection?

Ninject I couldn't understand how to use either...

thanks in advance

Upvotes: 0

Views: 455

Answers (1)

Stormhashe
Stormhashe

Reputation: 704

you are getting null references because you didn't configure any dependency injection. Simply passing a parameter on the controller constructor is not enough, you have to actually tell the depedency injector how to construct your dependencies.

Using Unity, you do something like this on your WebApiConfig (or global.asax if on mvc)

public static void UnityContext_OnRegisterComponents(Microsoft.Practices.Unity.UnityContainer container)
{
     container.RegisterType<ICarRepository, CarRepository>(new HierarchicalLifetimeManager());                
}

Now, for the concept of dependency injection, it is used to keep low coupling among projects. Your controller must not know your Business rules, neither how to create objects that belong to another assembly.

Example: When you create your repositories and isntantiate your entities directly on the controller, you are creating a dependency between those assemblies, making it really hard to test your code later, also making it a lot more complex.

In terms of architecture, I use something like this:

Web - Front end

Business - Where you do your business Rules

Contracts - Where you declare your data transfer objects

Data - Where you declare your entities, and entity framework do low level stuff, like opening connections, saving things to the database and etc, and where you declare your repositories

In that architeture:

Web access Contracts and Business

Business access Contracts and Data

Data Doesnt Access Anything

Business will ask Data for entities, Data will answer with the entity models from your database, and any manipulation you need to do, will be done in the business, returning a data transfer object (of the Contracts assembly), which will them be used in your front end, either as it is returned, or defining a new model to fit the transfer object into your front end.

In this scenario, keeping dependency injection in mind, it would create a coupling if the business layer knew how to create Data Layer objects, so instead of doing this, you configura a dependency injection container, and that object will be responsible for instantiating everything that every layer needs, keeping all your projects decoupled.

Upvotes: 2

Related Questions