A Few NHibernate Gotchas
Some things to watch out for when working with NHibernate...

If you update an NHibernate entity without detaching it first from the session, it will be updated automatically.


         [Test]        
        
public void UpdatedEntityPropertyWillSaveAutomatically()
        {
            Customer customer 
= null;

            try
            
{                
                customer 
_Repository.GetFirst();
                
customer.FirstName = null;  // First Name can not be null, so this will throw an exception
                
_Repository.Flush();  // flush happens also when the session ends.
            
}
            
catch(Exception ex)
            {
                Assert.That(ex.InnerException 
is System.Data.SqlClient.SqlException);
                
_Repository.Clear();                              
                return;
            
}

            Assert.Fail(
"We should never get here, because flushing the repository will throw the exception above.");
        
}

If you do not detach your object from its repository, NHibernate will pass you a reference to the same object.

        [Test]
        
public void IfNotDetachedTwoObjectsHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreEqual(c1.LastName, c2.LastName);
        
}

        [Test]
        
public void IfDetachedAnObjectWillNotHaveTheSameReference()
        {
            Customer c1 
_Repository.GetFirst();
            
_Repository.Evict(c1);
            
Customer c2 _Repository.GetById(c1.ID);

            
c1.LastName +"1";
            
Assert.AreNotEqual(c1.LastName, c2.LastName);
        
}
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

# re: A Few NHibernate Gotchas
Gravatar I read the article and it helped and I found NetSpell to be an excellent free spelling engine that works both in ASP.Net and Windows forms.
Left by Mosaic tables on 11/13/2009 1:50 AM
# re: A Few NHibernate Gotchas
Gravatar The service provider acts as principal, no commission is paid, but the price offered may not be the best available in the market thanks for sharing the information.
Left by forex education on 12/31/2009 4:46 AM
# re: A Few NHibernate Gotchas
Gravatar Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work... we are major supplier of used clothing, used shoes, garments and many different kinds of name brand clothing closeouts etc., many more are available here...
Left by static caravan insurance on 2/27/2010 1:13 AM
# re: A Few NHibernate Gotchas
Gravatar Firstly, You've got a lot of fantastic information here on your web site. I am very impressed! If we consider the part of Link building which is an important part of Search Engine Optimization and anyone who has website and runs the business online, consider various online marketing strategies to market the business and gain more profits.
Left by contextual links on 3/1/2010 12:03 AM
# re: A Few NHibernate Gotchas
Gravatar Resources like the one you mentioned here is very useful! I will post a link to this page on my blog. I am sure my visitors will find that very useful.
Left by SMO Dubai on 3/9/2010 4:14 AM
# re: A Few NHibernate Gotchas
Gravatar i like this Gucci new design handbags,see here
Left by GucciBags on 10/5/2010 11:02 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

 

Preview Your Comment.