How to use dependency injection outside a Controller with ninject

3

I have following code:

public class ContaController : ApiController
{
    private readonly IAppServiceUsuario _app;

    public ContaController(IAppServiceUsuario _app)
    {
        this._app = _app;
    }
}

How to inject dependency with Ninject using that same IAppServiceUsuario in another class that is not a Controller ?

This is mapped to Ninject :

kernel.Bind<IAppServiceUsuario>().To<AppServiceUsuario>();

Class where you should instantiate IAppServiceUsuario

 public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        private IAppServiceUsuario _app;

        public AuthorizationServerProvider(IAppServiceUsuario _app)
        {
            this._app = _app;
        }

        /// <summary>
        /// Método para validar o token no cache do Oauth
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            context.Validated();
            return Task.FromResult<object>(null);
        }

        public override Task TokenEndpoint(OAuthTokenEndpointContext context)
        {
            context.AdditionalResponseParameters.Add("User", "");
            return Task.FromResult<object>(null);
        }

        /// <summary>
        /// Metodo para verificar as credenciais de acesso
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {

            if (_app.Autenticar(context.UserName, context.Password) != null)
            {
                Claim claim1 = new Claim(ClaimTypes.Name, context.UserName);
                Claim[] claims = new Claim[] { claim1 };
                ClaimsIdentity claimsIdentity =
                    new ClaimsIdentity(
                       claims, OAuthDefaults.AuthenticationType);
                context.Validated(claimsIdentity);
            }
            else
            {
                context.SetError("Erro na validação", "Usuario ou senha Inválidos");
            }
            return Task.FromResult<object>(null);
        }
    }
    
asked by anonymous 20.12.2016 / 16:42

1 answer

2
When you do the kernel.Bind (). To (), Ninject already injects dependency and when you call the class it creates it for you.

Example:

public class Repository : IRepository {
 ...
}

public class Domain : IDomain {    
  public Domain(IRepository repository){}   
  ...
}
public class AppService : IAppService { 
  public AppService (IDomain domain ){} 
   ...
}

public class SomeControllerController: ApiController{ 
  public SomeControllerController(IAppService appService){} 
  ...
}


kernel.Bind<IRepository>().To<Repository>();
kernel.Bind<IDomain>().To<Domain>();
kernel.Bind<IAppService>().To<AppService>();
    
20.12.2016 / 19:53