views:

65

answers:

2

I use NHibernate to persist my objects.. However, I cannot seem to find any information about whether I need to encode inputs?

For example:

string firstName = TextboxFirstName.Text;
string lastName = TextboxLastName.Text;

using(ISession session = sessionFactory.OpenSession())
{
    Customer customer = new Customer(firstName, lastName);
    session.SaveOrUpdate(customer);         
}

Do I need to encode firstName and lastName (specifically single qoutes), or does NHibernate do this for me?

+3  A: 

Hibernate will take care of all that for you. In this case the properties just need to be strings.

ryber
A: 

A Customer instance is simply an object within your domain model. That is all it is. NHibernate is simply there behind the scenes - it is a window through which you may access your domain model.

NHibernate makes sure that your domain model is persisted correctly. It does this without you needing to do much of anything, such as encoding the string properties on your objects.

Additionally, if you are creating a new Customer instance, and you wish to inform NHibernate of the new instance, then you should use the API method ISession.Save, rather than ISession.SaveOrUpdate. The API method ISession.Save will save the new instance into the domain model (and, transparently, into the database).

Justice
SaveOrUpdate should work just fine. I'm guessing that Customer has an auto-assigned Id mapped with something like <generator class="native" />. In that case, NHibernate will be able to figure out on its own whether this is a new Customer or not.
Daniel Schilling
Daniel Schilling - You are correct, though I am using <generator class="guid.comb" /> but still the same reasoning should apply.
Morten Jacobsen