开发者

Way to use NHibernate to get field length limits easily

开发者 https://www.devze.com 2023-01-24 05:13 出处:网络
I need to limit the input length of some of开发者_运维技巧 my text boxes. I\'d like to link them to whatever the database length limit is in case the DBA decides to increase the max length directly in

I need to limit the input length of some of开发者_运维技巧 my text boxes. I'd like to link them to whatever the database length limit is in case the DBA decides to increase the max length directly in the database.

Anyone have any clue on how this is done?


I'm not sure if this is possible using NHibernate. As far as I'm aware, NHibernate can be used to create the database, but it can't read table properties from the database. In other words, it will assume that the tables are set up the way you declared them in the mappings, and then complain if something went wrong.

As Pieter has already said, you can use NHibernate Validator to enforce constraints on your model. You would still need to manually change the constraints if the underlying DB changes though.


Use a T4 template to read the length from table schema and automatically inject into the mapping or an NHibernate Validator.

It is a little bit of work, but if you're worried about the DBA changing the schema often, this is the way to go.

Alternatively, if that's too much work, it is possible to write unit tests that compare the length in the schema to the length on the mapping. These will break if the DBA updates the schema but the mapping is not updated properly. Then, when your unit tests break you know to change the code.


Have a look at NHibernate.Validator.

This system allows you to add restrictions to your entities, via attributes or XML. This information can then later on retrieved and used in your interface.

Some reading material:

  • http://www.pnpguidance.net/post/nhibernatevalidatortutorialvalidatebusinessobjectsmvc.aspx
  • http://nhibernate.info/doc/validator/nhibernate-validator-1-0-0-documentation.html
  • http://nhibernate.info/blog/2009/05/17/nhibernate-validator-quickstart.html
0

精彩评论

暂无评论...
验证码 换一张
取 消