NHibernate 和 Sharp 架构中的一个位置用于指定数据库和验证的字段长度
我正在使用 NHibernate、S#arp 架构构建一个新系统。我是 S#arp 的新手,但对 NHibernate 有经验。我真正希望找到的一个功能是一种在一个地方指定文本字段长度的好方法,该长度将被传递到需要知道长度的每一层。
换句话说,我使用 Fluent 映射来指定 Whatsis 对象的 Name 字段为 50 个字符。 (我关闭了自动映射,因为我仍在为大多数(如果不是全部)类编写映射重写。我决定自己使用 ClassMap 编写完整的映射。我希望映射中有足够的细节来生成完整的映射然后,当我使用 hbm2ddl 生成数据库模式时,我得到一个 50 个字符的字段长度。那太棒了。但我也希望 MVC 验证器自动执行它。我是如此贪婪,我什至希望 NHibernate 为 Whatsis 生成的代理对象了解其设置器中的长度,所以如果在代码中我应该分配一个太长的值,我会得到一个异常。
我是否有希望在不使我的项目过于复杂的情况下实现这一目标?这是我第一次使用 Fluent 映射。我以为这是 Fluent 的一个功能,但看起来毕竟不是这样的。
I'm building a new system with NHibernate, using S#arp Architecture. I'm new to S#arp, but experienced with NHibernate. One feature I was really hoping to find was a nice way to specify in one place text field lengths that would be carried through to every layer that needs to know the length.
In other words, I use Fluent mapping to specify that the Name field of a Whatsis object is 50 characters. (I turned off automapping because I was still writing mapping overrides for most, if not all classes. I decided just to write the full mapping myself with ClassMap
. I want enough detail in the mapping to generate the complete DB schema from it.) Then when I generate the database schema using hbm2ddl, I get a 50-character field length. That's great. But I also want the MVC validators to enforce it automatically. And I'm so greedy I even want the proxy object the NHibernate generates for Whatsis to know about that length in its setter, so if in code I should assign a too-long value, I'll get an exception.
Do I have any hope of achieving this without really overcomplicating my project? This is my first time using Fluent mapping. I thought this was a feature of Fluent, but it looks like it doesn't work that way after all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 NHibernate Validator是 NHibernate hbm2ddl 目前支持的唯一验证器框架。不幸的是,似乎没有人将 NHibernate Validator 和 MVC3 验证连接在一起。这是其他人遇到的问题:
MVC 3,NHIbernate 验证器和消息插值器
应该是可能的,因为我已经使用了 xVal (MVC3 验证器的前身)过去使用 NHibernate Validator 属性进行客户端验证、服务器端验证和模式生成。有人只需要投入一些 TLC 即可让 NHibernate Validator 与 MVC3 验证器很好地配合。
You need to use NHibernate Validator as that is the only validator framework that NHibernate hbm2ddl currently supports. Unfortunately it doesn't look like anyone has wired together NHibernate Validator and MVC3 validations. Here is someone else having problems with it:
MVC 3, NHIbernate Validators & Message Interpolator
It should be possible as I've used xVal (predecessor to MVC3 validators) in the past with NHibernate Validator attributes for client-side validation, server-side validation, and schema generation. Someone just needs to invest some TLC to get NHibernate Validator playing nicely with MVC3 validators.