automatically truncate strings

179 views
Skip to first unread message

Robert Allen

unread,
Dec 30, 2009, 2:02:43 PM12/30/09
to Fluent NHibernate
Is there a way to automatically truncate strings with fluent
mappings? I would prefer not to do this in the entity as it seems
that this would be leaking the db structure into the model.

Thanks

Hudson Akridge

unread,
Dec 30, 2009, 2:17:20 PM12/30/09
to fluent-n...@googlegroups.com
Could you elaborate a bit more? Do you just want to chop off text after n characters? That's not something Fluent NHibernate specifically would be able to help you with. It's no different than NH mappings. You can specify a .Length(n) if you'd like. There is a caveat. NHibernate won't let you save a property to a column where truncation is involved, an exception will be raised.

If you want to truncate text before it gets persisted, then I'd say that's a rule of the model, not the DB structure. The DB is there only to validate your business rules. In this case, one of your business rules is that a specific string field cannot have more than n characters. That's a model rule, not a db rule.

Let's say NH didn't throw an exception when truncating and just did it automatically. You then saved your object, and immediately retrieved it back from NH (hitting the database), you'd have two different objects. One with the non-truncated string fields, and one with truncation. It's not the same object. Therefore you have not properly persisted your model. That's a breech of contract between your model and the persistence layer.


--

You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.





--
- Hudson
http://www.bestguesstheory.com
http://twitter.com/HudsonAkridge

robsosno

unread,
Dec 31, 2009, 1:03:42 PM12/31/09
to fluent-n...@googlegroups.com
I have a database (Informix) which pads strings with spaces up to the
declared field length while retrieving data from database. This is ANSI
compliant behavior for Char fields (for Varchars there is no padding).
I'm not sure if you have similar things in mind.
If so solution is to use custom type like in the following example:
Map(x => x.LastName, "lastname").CustomType<TString>();
You need to implement TString custom user type. Mine implementation is
here: http://nhjira.koah.net/secure/attachment/12614/tstring.cs
There are also other similar implementations.

W dniu 2009-12-30 20:02, Robert Allen pisze:

Reply all
Reply to author
Forward
0 new messages