Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. public class ConfigurationProvider : Provider<Configuration> {
  2. public class ConfigurationProvider(string connectionString
  3. , Assembly mappings) {
  4. ConnectionString = connectionString;
  5. Mappings = mappings;
  6. }
  7.  
  8. protected override Configuration CreateInstance(IContext context) {
  9. var c = new Configuration().Configure();
  10. c.AddAssembly(Mappings);
  11. c.Properties["connection.connection_string"] = ConnectionString;
  12. return c;
  13. }
  14.  
  15. private readonly string ConnectionString;
  16. private readonly Assembly Mappings;
  17. }
  18.  
  19. public class ConfigurationService {
  20. public ConfigurationService(ICurrentUser user) { CurrentUser = user; }
  21.  
  22. public string BuildConnectionString() {
  23. return string.Format(DefaultConnectionString
  24. , CurrentUser.DatabaseInstanceName
  25. , CurrentUser.Login
  26. , CurrentUser.Password);
  27. }
  28.  
  29. private readonly ICurrentUser CurrentUser;
  30. }
  31.  
  32. public class CurrentUser : ICurrentUser {
  33. public CurrentUser(string dbinstance, string login, string password) {
  34. DatabaseInstanceName = dbinstance;
  35. Login = login;
  36. Passwword = password;
  37. }
  38.  
  39. public readonly string DatabaseInstanceName;
  40. public readonly string Login;
  41. public readonly string Password;
  42. }
  43.  
  44. string dbInstance = authenticationDialog.DatabaseInstanceName;
  45. string login = authenticationDialog.Login;
  46. string password = authenticationDialog.Password;
  47.  
  48. var nHibernateConfigurationProvider =
  49. new ConfigurationProvider(
  50. new ConfigurationService(
  51. new CurrentUser(dbInstance, login, password)).BuildConnectionString()
  52. , Assembly.GetExecutingAssembly());
  53.  
  54. // Wait until user is authenticated before continuing to bind???
  55. Bind<string>().ToMethod(ctx => ctx.Request.???.BuildConnectionString()).WhenInjectedInto<ConfigurationProvider>().InSingletonScope();
  56. Bind<Assembly>().ToConstant<Assembly>(Assembly.GetExecutingAssembly().WhenInjectedInto<ConfigurationProvider>();
  57.  
  58. // Do your stuff to collect credentials
  59.  
  60. // User now authenticated, initialize ICurrentUser instance singleton
  61. // You could have used a ICurrentUserFactory or static member, this is just a sample implementation
  62. kernel.Get<ICurrentUser>(new ConstructorArgument("dbInstance", dbInstance),
  63. new ConstructorArgument("login", login),
  64. new ConstructorArgument("password", password));
  65.  
  66. // You can now access your NHibernate configuration properly
  67. var config = kernel.Get<Configuration>();
  68.  
  69. Bind<Configuration>().ToProvider<ConfigurationProvider>();
  70. Bind<ConfigurationProvider>()
  71. .ToMethod(
  72. ctx => new ConfigurationProvider(ctx.Kernel.Get<ConfigurationService>().BuildConnectionString()));
  73.  
  74. Bind<ICurrentUser>().To<CurrentUser>().InSingletonScope();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement