Reputation: 1352
In my application i m using fluent nhibernate with MVC 3. i have separate business layer , DAL , and repository layer, now i have to ask where to create the database connection , weather in the DAL or in the MVC (user interface layer)
Note: i want to define the connection string in web.config file.
Does anyone know ?
Thanks
Upvotes: 1
Views: 1856
Reputation: 60564
Since the application running your code will be the ASP.NET MVC project, the configuration settings in Web.config will be available in any class library. Thus, you should be able to use the configuration code Rippo suggested in any project (however, it won't be reusable from other applications if they don't define a connection string with the same key).
I suggest you let the configuration code for NHIbernate reside in the DAL project, along with other NHibernate related things.
Upvotes: 1
Reputation: 22424
I do something like this inside my MVC project:-
<connectionStrings>
<add name="db"
connectionString="Server=.;Database=NHCookbook;Trusted_Connection=SSPI;"/>
</connectionStrings>
Then in the mvc project I build my session factory like:-
var nhConfig = Fluently
.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("db"))
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AMODELCLASS>()
)
.BuildConfiguration();
sessionFactory = nhConfig.BuildSessionFactory();
Upvotes: 2