C#中非泛型接口通用成员实现的类型约束(Type constraints on implementations of generic members of non-generic interfaces in C#)
假设我有这样的界面:
interface IAwesome { T DoSomething<T>(); }
有没有什么办法来实现类型约束的DoSomething方法? 显然,这不起作用:
class IncrediblyAwesome<T> : IAwesome where T : PonyFactoryFactoryFacade { public T DoSomething() { throw new NotImplementedException(); } }
这显然不会工作,因为这个DoSomething()不会完全满足IAwesome的合同 - 它只适用于类型参数T的所有可能值的子集。有没有什么办法可以使这个工作不够“铸造黑魔法“(如果答案是否定的,我将如何作为最后的手段)?
老实说,我不认为这是可能的,但我想知道你们的想法。
编辑 :有问题的接口是System.Linq.IQueryProvider,所以我不能修改接口本身。
Let's say I have an interface like that:
interface IAwesome { T DoSomething<T>(); }
Is there any way to implement the DoSomething method with type constraint? Obviously, this won't work:
class IncrediblyAwesome<T> : IAwesome where T : PonyFactoryFactoryFacade { public T DoSomething() { throw new NotImplementedException(); } }
This obviously won't work because this DoSomething() won't fully satisfy the contract of IAwesome - it only work for a subset of all possible values of the type parameter T. Is there any way to get this work short of some "casting black magic" (which is what I'm going to do as last resort if the answer is no)?
Honestly, I don't think it's possible but I wonder what you guys think.
EDIT: The interface in question is System.Linq.IQueryProvider so I can't modify the interface itself.
原文:https://stackoverflow.com/questions/2259114
最满意答案
那么,检索到的数据线程A会发生什么,是线程B修改之前的数据还是线程B修改后的数据?
线程A仍然只有旧数据。 因为,
each session
都有own first level cache
。 要获取更新的数据session
可以调用evict(..)
并再次load
它,或者调用refresh(...)
方法,假设线程B已将数据保存到DB。那么如果将数据保存在二级缓存中会发生什么,这会在数据集成上产生差异吗?
行为与上述相同。 除此之外,如果您在
Entity
设置了version
属性,您还可以使用session.lock(...)
来检测实体更改。 例如:LockMode.READ
检查数据库的version
属性以检测对它的任何更改,即通过二级缓存。Then, what will happen to the data Thread A retrieved, is it the data before Thread B modified or the data after Thread B modified?
Thread A will still have only the old data. Because,
each session
has itsown first level cache
. To get the updated datasession
can either callevict(..)
andload
it again or call therefresh(...)
method assuming thread B has saved the data to the DB.Then what will happen if data is saved in second level cache, will that make differences on data integration?
The behavior is same as above. Apart from that, if you are having
version
attribute set in theEntity
you can also usesession.lock(...)
to detect entity changes. For ex:LockMode.READ
checks theversion
attribute against the database to detect any changes to it i.e., by pass second level cache as well.
相关问答
更多-
那么,检索到的数据线程A会发生什么,是线程B修改之前的数据还是线程B修改后的数据? 线程A仍然只有旧数据。 因为, each session都有own first level cache 。 要获取更新的数据session可以调用evict(..)并再次load它,或者调用refresh(...)方法,假设线程B已将数据保存到DB。 那么如果将数据保存在二级缓存中会发生什么,这会在数据集成上产生差异吗? 行为与上述相同。 除此之外,如果您在Entity设置了version属性,您还可以使用session.l ...
-
我相信Hibernate默认为延迟初始化。 因此,当您在会话中加载Foos列表时,没有任何关联的酒吧会加载,直到尝试使用它们。 那时候,你已经关闭了会话,这会导致错误。 一些潜在解决方案 启用Foo-Bar关联的预先获取 “加入”取回(实际上是一个渴望获取) 尝试访问会话中的酒吧(这将工作,但你调用Foo.getBar()只是为了获得关联的Bar对象加载) 评论更新: 会话/交易管理的成语: Session sess = factory.openSession(); Transaction tx; tr ...
-
休眠和并发(Hibernate and Concurrency)[2022-08-28]
捕获StaleObjectException:s并增加应该更快的线程的优先级。 StaleObjectException:s应该很少见。 看看悲观锁定,如果这不适合你。 Catch the StaleObjectException:s and increase the priority of the thread which should be faster. StaleObjectException:s should be rare. Look into pessimistic locking if th ... -
JavaFx中的Hibernate与NetBeans 7.4版本兼容。 NetBeans 7.3或更低版本不适用于JavaFx。 而不是使用逆向工程向导。 您可以轻松使用POJO和映射文件。 Hibernate in JavaFx is compatible with NetBeans 7.4 version. NetBeans version 7.3 or below are not suitable for JavaFx. Instead of making use of reverse enginee ...
-
如何使用hibernate会话?(How to use hibernate sessions?)[2022-03-11]
假设您希望所有更新都是单个原子事务,则需要打开Session并在循环之前开始事务。 然后,在循环之后,您将要提交事务并关闭会话。 如果每个更新都应该是它自己的原子事务,那么您仍然应该只打开一个Session,然后对循环的每次迭代使用一个新事务。 Assuming you want all your updates to be a single atomic transaction, you'll need to open the Session and begin a transaction before ... -
JOOQ Vs Hibernate行为(JOOQ Vs Hibernate behavior)[2022-07-10]
jOOQ做同样的事情。 如果更改记录的主键,则它将使用INSERT ,否则,它将使用UPDATE 。 实际上,当您从数据库中读取记录时,调用store()将触发UPDATE如您所料。 如果您创建一个新记录,那么它将被INSERT编辑。 使用2.6,克隆一条记录然后让jOOQ更新它有点困难(因为克隆会在新实例中设置主键,因此将其标记为“new” - > insert)。 jOOQ does the same. If you change the primary key of a record, then it ... -
Hibernate性能调整(Hibernate Performance Tweaks)[2024-03-21]
一些特定于Hibernate的性能调优技巧: 避免由并行多对应的fetch-joins引起的连接重复(因此避免重复的对象实例化) 使用延迟加载和fetch =“subselect”(防止N + 1选择问题) 在巨大的只读结果集上,不要获取映射对象,而是获取平面DTO(使用Projections和AliasToBean-ResultTransformer) 应用HQL批量更新,批量删除和按插入选择 在适当的地方使用FlushMode.Never 摘自http://arnosoftwaredev.blogspo ... -
您应该保留相同的SessionFactory而不重新创建一个。 所以你需要让它静止。 示例: public class HibernateUtils{ private static SessionFactory session; private static void createSession(){ sessionFactory = new Configuration().configure().buildSessionFactory(); } publi ...
-
在你的代码中新的DBConnection()是指什么。 我们需要从org.hibernate.SessionFactory.SessionFactory中获取/打开Hibernate中的会话。 所以请检查您是否使用了sessionfactory。 The log4j is the cause of this issue. I removed the Log4j and added log4j-1.2.16.jar. It gets fixed. Thanks!
-
似乎没有通过ohInterceptor通过ohEmptyInterceptor获取会话对象的“官方”方式。 此外, Interceptor的Javadoc说: SessionFactory可能只有一个Interceptor实例,或者可能为每个Session指定一个新实例。 无论使用哪种方法,如果Session是可序列化的,则拦截器必须是可序列化的。 这意味着SessionFactory范围的拦截器应该实现readResolve()。 可能不会从回调中调用会话(回调也不会导致集合或代理被懒惰地初始化)。 获取 ...