首页 \ 问答 \ C#中非泛型接口通用成员实现的类型约束(Type constraints on implementations of generic members of non-generic interfaces in C#)

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
更新时间:2023-08-30 18:08

最满意答案

那么,检索到的数据线程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 its own first level cache. To get the updated data session can either call evict(..) and load it again or call the refresh(...) 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 the Entity you can also use session.lock(...) to detect entity changes. For ex: LockMode.READ checks the version 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 ...
  • 捕获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 ...
  • 假设您希望所有更新都是单个原子事务,则需要打开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做同样的事情。 如果更改记录的主键,则它将使用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的性能调优技巧: 避免由并行多对应的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()。 可能不会从回调中调用会话(回调也不会导致集合或代理被懒惰地初始化)。 获取 ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)