Reputation: 1337
I am still new to dependency injection, and it seems like I'm missing something.
I used Autofac as my container to resolve every repositories dependency to NHibernate session.
The session receive a dependency of SystemSetting, which pretty much only consisted of MySql connection string.
At the beginning of application I configure all registered dependency, including the initialization of SessionFactory.
The case is, if the connection string is wrong, the application throws a compile time exception that it cannot connect to MySql with provided connection string. What if I wanted to allow this misconfiguration so the user can then set own his own the correct, let's say, ip address?
My problem is, I cannot get the application to the Login Form, since Login Form received User Repository as a dependency which in turn receive an, supposedly, active session, therefor throwing an exception.
Should I exclude the creation of NHibernate session out of Autofac configuration?
Upvotes: 0
Views: 504
Reputation: 14318
I'm fairly sure you'd be better off when injecting singleton lifetime-scoped ISessionFactory
instead of ISession
.
It means that there's one instance of ISessionFactory
injected to each component, and each of those components creates its own ISession (or multiple ISessions
if needed).
As for the keywords check, by default NHibernate connects briefly to the database when building the ISessionFactory
to check for reserved keywords (that's for schema/DDL creation). You can disable that, so the first connection to the database will be made on the first ISession
use.
Upvotes: 1