如何配置 Fluent NHibernate 以通过 IoC 构造函数注入(非空构造函数)使用 IUserType 实现
从版本 2.1 开始,NHibernate 支持通过 IoC 容器(通过 ByteCodeProvider 配置设置提供)对 IUserTypes 进行构造函数注入。
但是,当我尝试通过 Fluent NHibernate 配置(不是自动映射)来实现此功能时,当我构建配置时,我收到一个异常,抱怨我的一个对象(在本例中是自定义 ICompositeUserType)没有空的默认值构造函数。
我将此异常追溯到 FluentNHibernate.Mapping.PropertyPart.AddColumnsFromCompositeUserType(Type compositeUserType) 中对 Activator.CreateInstance() 的调用。
NHibernate 的 IoC 支持功能的全部要点是用对提供的自定义 ByteCodeProvider 的调用来替换对 Activator.CreateInstance 的调用。
所以我想我的问题是:是否可以使用 Fluent NHibernate 来实现此功能,或者我是否必须放弃我的 Fluent NH 配置并返回到 NHibernate 的内置编程配置?
有关此功能的博客文章链接:
更新:看起来像问题在于 FluentNHibernate 对 ICompositeUserType 自定义实现的处理。我将复合用户类型更改为 IUserType(这可能更适合类的实际情况),而且看起来 Fluent NHibernate 现在正在合作。
更新#2:添加一个空构造函数确实解决了问题 - 我将其标记为已回答,但建议是在评论中而不是答案中出现的,所以我对这些评论表示赞同(直到添加正式答案)。
As of version 2.1, NHibernate supports constructor injection for IUserTypes via IoC container (supplied through the ByteCodeProvider config setting).
However, when I try to get this working through Fluent NHibernate configuration (not auto mapping), when I build my configuration, I get an exception complaining that one of my objects (a custom ICompositeUserType, in this case) does not have an empty default constructor.
I traced this exception down to a call to Activator.CreateInstance() in FluentNHibernate.Mapping.PropertyPart.AddColumnsFromCompositeUserType(Type compositeUserType).
The whole point of NHibernate's IoC support feature was to replace calls to Activator.CreateInstance with calls to the provided custom ByteCodeProvider.
So I guess my question is this: is it possible to get this working with Fluent NHibernate, or do I have to scrap my Fluent NH configuration and go back to NHibernate's built-in programmatic configuration?
Links to blog posts about this feature:
UPDATE: it looks like the problem is with FluentNHibernate's handling of custom implementations of ICompositeUserType. I changed my Composite User Types to IUserType (which is probably better suited for what the classes actually were), and it looks like Fluent NHibernate is cooperating now.
UPDATE #2: Adding an empty constructor does solve the problem - I'd mark this as answered, but the suggestion came in a comment instead of an answer, so I'm upvoting those comments (until an official answer is added).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,Fluent NHibernate 所做的一切都是创建与使用 NHibernate XML 相同的配置对象结构。您如何尝试通过 Fluent NHibernate 设置 ByteCodeProvider?
Keep in mind that all Fluent NHibernate does is create the same configuration object structure that utilziing NHibernate XML would. How are you trying to set the ByteCodeProvider via Fluent NHibernate?