Dependency injection with Ninject

0

I'm using Ninject in ASP.Net MVC and trying to implement, but I'm getting an error:

  

Error activating ISessionFactory

     

No matching bindings are available, and the type is not self-bindable.

     

Activation path:

     

3) Injection of dependency ISessionFactory into parameter session of constructor of type PersonRepository

     

2) Injection of dependency IPersonRepository into parameter personsRepository of constructor of type HomeController 1) Request for HomeController

I'm using ISessionFactory in my repository, do I need to bind on it?

I'm using Fluent NHibernate.

My repository:

public class PersonRepository : IPersonRepository
{
    private ISession openSession;
    private ISessionFactory session;

    public PersonRepository(ISessionFactory session)
    {
        this.openSession = session.OpenSession();
        this.session = session;
    }

    public void CreatePerson(Person person)
    {
        openSession = NhibernateUtilities.OpenIfClosed(session, openSession);
        openSession.SaveOrUpdate(person);
    }
}

My controller:

public class HomeController : Controller
{
    private readonly IPersonRepository personsRepository;


    public HomeController(IPersonRepository personsRepository)
    {
        this.personsRepository = personsRepository;
    }

    public ActionResult Index()
    {
        Person test = new Person()
        {
            Id = 1,
            Name = "teste",
            Surname = "teste",
            Nickname = "teste",
            Age = 25,
            Division = "teste",
            Email = "teste",
            Lane = "teste"
        };

        personsRepository.CreatePerson(test);

        return View();
    }

Global.asax:

public class MvcApplication : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<IPersonRepository>().To<PersonRepository>();
        return kernel;
    }

    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

}
    
asked by anonymous 09.09.2014 / 02:20

1 answer

1

Rodrigo,

At first you need to bind your ISessionFactory.

Here's a stackoverflow topic in English that contains bind.

link

    
15.09.2014 / 16:29