简体   繁体   English

NHibernate多对一“运行中”

[英]NHibernate many-to-one “on the fly”

I'd like to know if the next problem can be solved in a different way in NHibernate. 我想知道下一个问题是否可以在NHibernate中以其他方式解决。

Let's say we've this domain: 假设我们拥有以下域:

public class A 
{
     public virtual B LastAssociationWithB { get; set; }
     public virtual ICollection<B> CollectionAssociationOfB { get; set; }
}

public class B
{
     public virtual DateTime DateAdded { get; set; }
}

The LastAssociationWithB property represents one of the B persistent objects associated in the CollectionAssociationOfB collection property. LastAssociationWithB属性表示CollectionAssociationOfB集合属性中关联的B持久对象之一。

Actually, LastAssociationWithB represents the last B persistent object added by date. 实际上, LastAssociationWithB代表按日期添加的最后一个B持久对象。

So, in the domain, when a new B is added to CollectionAssociationOfB , it's also assigned to LastAssociationWithB . 因此,在域中,当将新的B添加到CollectionAssociationOfB ,它也被分配给LastAssociationWithB

This is a good way of later turning code into less complex LINQ queries. 这是以后将代码转换为不太复杂的LINQ查询的好方法。

Anyway, my question is: do you know any other approach to this? 无论如何,我的问题是: 您知道其他方法吗? For example, some kind of many-to-one association that produces a SQL join under the hoods so you wouldn't need to have an explicit 1:n relation in the A table but it would maintain the class property? 例如,某种多对一的关联在幕后产生了SQL 连接 ,因此您不需要在A表中具有显式的1:n关系,但是它将维护类属性?

Or is my current approach the recommended way of solving this scenario? 还是我当前的方法是解决此情况的推荐方法?

Side note: in the real-world scenario that CollectionAssociationOfB is an ordered list as ordering is specified in the NHibernate mapping configuration. 旁注:在实际情况下, CollectionAssociationOfB是有序列表,因为在NHibernate映射配置中指定了排序。

You could specify the relationship using a formula: 您可以使用公式指定关系:

whether this is better or not is debatable.. it depends on your circumstances - one the one hand it ensures consistency, but on the other hand it will probably have a performance penalty when querying - so it really depends on your own specific case. 是否更好还是值得商。的..这取决于您的情况-一方面可以确保一致性,另一方面,在查询时可能会降低性能-因此,它实际上取决于您自己的具体情况。

Another alternative is to use a trigger on insert into B to update the column in A. This has the downside of moving logic into the database, but it would ensure consistency without the potential performance penalty. 另一种选择是使用插入到B的触发器来更新A中的列。这样做有将逻辑移到数据库中的缺点,但可以确保一致性而不会造成性能损失。

You could also achieve the equivalent of a trigger by using an NHibernate event to intercept saving B and then updating A - with the benefit of the logic remaining in your code, but the downside that any direct updates to the database could introduce inconsistency. 您还可以通过使用NHibernate事件来拦截保存的B然后更新A来实现与触发器相当的效果-受益于代码中保留的逻辑,但是不利的是,对数据库的任何直接更新都可能导致不一致。

Of course both trigger options obfuscate the logic somewhat, as opposed to having a method on A or B that does the logic. 当然,与在A或B上执行逻辑的方法相反,这两个触发选项都会使逻辑有些模糊。 I would personally probably put a method in A to add a new B and update the association, but then you would need to ensure that no-one updates the B collection directly and bypasses your method. 我个人可能会在A中添加一个方法来添加新的B并更新关联,但是您将需要确保没有人直接更新B集合并绕过您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM