1

I've develop a multi tier project with MVC5. And i use ASP.NET Identity 2.0.. And i use Ninject for depency injection. But i can't figure out how can i set call service layer in my controller. I use call directly call my manager class.

    public class UserController : Controller
    {

    private IUserService _userService; //i can't use this property at the moment. Because i can't iject it

    private UserManager _userManager;
    public UserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<UserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }


    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new User { UserName = model.Email, Email = model.Email };
            //i need create usermanager from my service layer.
            UserManager userManager = this.UserManager;

            var result = userManager.Add(user, model.Password);

            if (result.Succeeded)
            {
                return RedirectToRoute("Home");
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error);
                }                    
            }
        }

        return View(model);
    }
 }
Yargicx
  • 1,704
  • 3
  • 16
  • 36
  • see https://github.com/ninject/ninject.web.mvc and http://stackoverflow.com/questions/16382982/configuring-ninject-with-asp-net-mvc-web-api for correct configuration of ninject. Ctor-injection is recommended, not property injection. If you want to do property injection, you have to put an `[Inject]`-Attribute on the property. – BatteryBackupUnit Oct 14 '14 at 05:05
  • I wrote a post answering this question exactly except that it was for simple injector. But I suppose you'll have little work to redo this for ninject. You can find the answer here: [link](https://simpleinjector.codeplex.com/discussions/564822) – Ric .Net Oct 14 '14 at 09:17

0 Answers0