0
votes

This is my first project using Ninject and MVC, I`m trying to implement.

But I'm getting this 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

Estou utilizando ISessionFactory no meu repositório, eu preciso dar bind nele?

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);
    }

}

I'm using Fluent Nhibernate.

1
Where are you setting up your binding for ISession (or is that automatic?) also it doesn't look like you're calling CreateKernel. - Kritner
So, ISessionFactory have a repository to bind? It`s from Fluent . I found this Create Kernel in a tutorial, i need to call it somewhere? - rodmba
@RodrigoMagalhães Estou utilizando ISessionFactory no meu repositório, eu preciso dar bind nele? Translate this line to English. - RajeshKdev

1 Answers

2
votes

Ref : Need help understanding how Ninject is getting a Nhibernate SessionFactory instance into a UnitOfWork?

You need to bind the session factory

Bind<ISession>().ToProvider<SessionProvider>().InRequestScope();

And use ISession in the repo constructor

public class PersonRepository : IPersonRepository
{
    private ISession session;

    public PersonRepository(ISession session)
    {
        this.session = session;
    }

...