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