I'm learning NHibernate and have created some unit tests and it's going quite well. My main app only has two layers:
- Presentation (WPF app)
- Domain (has business logic and data access)
and of course I have my Unit test project (Test).
My unit tests call my SessionManager class to build a SessionFactory during [TestFixtureSetup]. Each [Test] then gets a Session to perform the data access unit test. I'm wondering how I should do this in my WPF project.
I have created a simple dialog box to see if I can wire up my WPF app to my database. It feels wrong to have a reference to NHibernate in my Presentation layer, am I right in this feeling? If so, how should I get a session to store an object when a user clicks Add on my form to store something? Here's some of my code:
public class SessionManager
{
private ISessionFactory _sessionFactory;
public SessionManager()
{
_sessionFactory = GetSessionFactory();
}
public ISession GetSession()
{
return _sessionFactory.OpenSession();
}
private ISessionFactory GetSessionFactory()
{
return (new Configuration()).Configure().BuildSessionFactory();
}
}
In my Test project I have this kind of thing:
[TestFixtureSetUp]
public void TestFixtureSetup()
{
log4net.Config.XmlConfigurator.Configure();
DatabaseFixtureSetUp();
_sessionManager = new SessionManager();
}
[SetUp]
public void Setup()
{
DatabaseSetUp();
_session = _sessionManager.GetSession();
}
I'm not sure how to structure my Presentation layer code to store an object. I have this on my form:
private void btnAddBroker_Click(object sender, RoutedEventArgs e)
{
var broker = new Broker
{
Name = txtBrokerName.Text,
IsActive = (bool)chkIsActive.IsChecked,
IsDefault = (bool)chkIsDefault.IsChecked
};
// save the broker object to the database
// is it ok to have NHibernate code here? feels wrong
}
How would you do it?