Here is my suggestion on how you can achieve this.
Since you don't have the data beforehand and only when your ICurrentUser
is created via the credentials and parameters you need, it means that you need to "save it" into a globally accessible location once it has been created.
If we think about a web .NET application, that would tipically involve binding the ICurrentUser
with InRequestScope()
using a static reference to a HttpContext
.
As this is not your case, I've chosen to bind ICurrentUser
as a singleton and initialize it passing the values. When you need your other classes that depend on it, the container will pass the already-initialized instance, since it is bound with InSingletonScope()
.
I've also tried to mimic your current dependencies. For instance, instead of passing in the connectionString
to ConfigurationProvider
, my way of doing it would be declare a direct dependency on ConfigurationService
in order to obtain the information.
// Do your stuff to collect credentials
// User now authenticated, initialize ICurrentUser instance singleton
// You could have used a ICurrentUserFactory or static member, this is just a sample implementation
kernel.Get<ICurrentUser>(new ConstructorArgument("dbInstance", dbInstance),
new ConstructorArgument("login", login),
new ConstructorArgument("password", password));
// You can now access your NHibernate configuration properly
var config = kernel.Get<Configuration>();
And the bindings:
Bind<Configuration>().ToProvider<ConfigurationProvider>();
Bind<ConfigurationProvider>()
.ToMethod(
ctx => new ConfigurationProvider(ctx.Kernel.Get<ConfigurationService>().BuildConnectionString()));
Bind<ICurrentUser>().To<CurrentUser>().InSingletonScope();
Hope this helps.