|
UpperStringUserType
Howto use the user type `UpperString`
IntroductionSome times, especially for customers requeriments, we need a way to save the strings against the database in upper case way. One maner to do this it's letting to the business objects the job, at the getters/setters methods. But other coolest way it's let to NHibernate take care of this. UpperStringThis is the class that do the job: uNHAddIns.UserTypes.UpperString. Let's look this example: The mapping fileAs you see the property LastName it's configurated to support upper case string. <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NH" namespace="NH"> <class name="Customer"> <id name="Id" type="int"> <generator class="assigned"/> </id> <property name="FirstName" /> <property name="LastName" type="uNHAddIns.UserTypes.UpperString, uNHAddIns" /> </class> </hibernate-mapping> The entitynamespace NH
{
public class Customer
{
private int id;
public virtual int Id
{
get { return id; }
set { id = value; }
}
private string firstName;
public virtual string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public virtual string LastName
{
get { return lastName; }
set { lastName = value; }
}
}
}The runCustomer c = new Customer(); c.Id=1; c.FirstName = "Astor"; c.LastName = "Piazzolla"; session.Save(c); session.Flush(); session.Evict(c); Customer customer = session.Get<Customer>(1); Debug.Assert(customer.FirstName == "Astor"); Debug.Assert(customer.LastName == "PIAZZOLLA"); Before Save, the property LastName was set to Piazzolla, then we Save. After of this, we do a Get and the resultant string property was PIAZZOLLA Cool uh ? Advantages of this approach
Dario Quintana |
Sign in to add a comment