- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个项目使用了 EntityFramework 无法使用的旧数据库。
所以我们已经开始构建一个穷人的 EntityFramework,它有一个基类 CustomBaseTable
从哪个实体类派生。另一个类(class),CustomQueryBuilder
, 具有构建 SQL 查询的方法:BuildSelectQuery(CustomBaseTable p_objEntity)
等等。
我们的第一个版本很好地构建了查询,但是我们使用了相当粗糙的对象,这些对象不是很灵活(为了节省空间而保留细节)。
我最近意识到使用 Expression
对象将更加高效和灵活。
所以我想给 CustomBaseTable
添加一个方法,这或多或少会像 Where()
一样工作:
EntityTable z_objEntity = new EntityTable();
z_objEntity.CustomWhere(t => t.Field1 == Value1);
CustomQueryBuilder z_objBuilder = new CustomQueryBuilder(DBTypeEnum.DataBaseType);
string z_strQuery = z_objBuilder.BuildSelectQuery(z_objEntity);
现在,我在声明
CustomWhere()
时遇到了障碍。 .我尝试了几种方法:
public class CustomBaseTable
{
public void CustomWhere1<T>(Expression<Func<T, bool>> p_expWhereClause) where T : CustomBaseTable
public void CustomWhere2<T>(this T z_objTable, Expression<Func<T, bool>> p_expWhereClause) where T : CustomBaseTable
}
public static class CustomBaseTableExtension
{
public static void CustomWhere3<T>(this T z_objTable, Expression<Func<T, bool>> p_expWhereClause) where T : CustomBaseTable
}
但是,就我而言,每个人都有一个缺陷:
CustomWhere1
需要指定 <EntityTable>
每次调用都会占用空间并且是多余的,因为调用该方法的对象具有相同的类型:z_objEntity.CustomWhere<EntityTable>(t => t.Field1 == Value1);
CustomWhere2
需要传递它所调用的对象,也占用空间并且也是多余的:z_objEntity.CustomWhere(z_objEntity, t => t.Field1 == Value1);
CustomWhere3
巧妙地避免了这两个缺陷,但显然需要创建一个单独的扩展类。如果需要,我会使用它,但我不明白为什么需要它。 最佳答案
是的。您可以使用 curiously recurring template pattern , 定义 EntityTable : CustomBaseTable<EntityTable>
,因此,有 EntityTable
可用作通用参数。这是一个最小的例子( fiddle ):
using System;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
var Value1 = "value1";
var z_objEntity = new EntityTable();
z_objEntity.CustomWhere1(t => t.Field1 == Value1); // compiles!
}
public class CustomBaseTable<T>
{
public void CustomWhere1(Expression<Func<T, bool>> p_expWhereClause)
{
throw new NotImplementedException();
}
}
public class EntityTable : CustomBaseTable<EntityTable>
{
public string Field1 { get; set; }
}
}
话虽如此,我确实相信扩展类是解决您问题的最简单方法。
CustomWhere3 neatly avoid both flaws, but apparently requires creating a separate extension class. I'll go with it if I need to, but I fail to see why it's needed.
关于c# - 有没有办法声明一个类似 Linq 的 CustomWhere() 方法并在没有冗余项的情况下调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66461781/
我有下面提供的“示例代码”,我觉得这很多余,并且想请您与我一起解释是否有任何更少冗余的方法来处理以下代码。 public interface Bars { FOO1 FOO1 = (FOO1)
我们刚刚测试了一个由 2 个服务器组成的 AppFabric 集群,我们在其中删除了“领导”服务器。第二个服务器对它的任何请求超时并出现错误: Microsoft.ApplicationServer.
我正在设计一个关系数据库 - 底层是 MySQL - DBDesigner 4 . 我有 3 个表:module、page 和 lang。每个模块属于一个页面,每个页面都有特定的语言: 外键 pag
我的 Fraction 程序运行流畅,但 NetBeans IDE 告诉我以下 if 是多余的: public boolean equals(Object other) { Fraction bo
下面的代码显然是多余的,但根据我的经验,我经常使用这种模式。有没有更好的方法在 python 中执行此操作? if re.search("at (\d{1,2}):\d{2}", p): a=
我有一个用具体示例说明的一般性问题。当所有组件对象都已测试时,您建议对复合对象进行多少测试? 作为具体示例,请考虑下面的 NullTerminatedStringReader。它从字节缓冲区中读取一个
if ( a > b) { return true; } return false; 对于上面的代码,Netbeans 给出了 “Redundant if statement” 警告并建议将其更改
当我添加一些约束时,例如: create table Test( IDTest int primary key, Credit int not null constraint Credit
此代码适用于 Microchip 的 PIC32MX 微处理器。他们的编译器本质上是 GCC 3.4。 我倾向于使用 GCC 的 __packed__ attribute将位域打包到一个 union
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在为我的应用程序使用apns通知,为此,我将apns设备 token 存储在我的sql数据库中。 问题在于,每次用户删除或安装该应用程序时,它都会生成一个新的设备 token ,并将其存储在数据库
Action Keyname Type Unique Packed Column Cardinality Collation Null Comment Edit Dro
我正在开发一个使用 ARM Cortex-M0 处理器的项目。在这个项目中,我需要提供计时器支持(CMSDK (SSE-200)计时器)。 因此,在 vector 表中,在 TIMER0_IRQn 表
有没有写的理由 corsFilter.setAllowedOrigins(new HashSet(Arrays.asList("*"))); 其中allowedOrigins在ReSTLet框架中的定
我正在创建一个包含 4 个链接的 HTML/CSS 页面, Home.html Details.html ContactMe.html AboutUs.html 我想在所有关联的 HTML 页面中将其
我试图理解并使用其他人编写的代码,但由于我对 typedef 经验不多。 , 我有时会感到困惑。 有两个不同的头文件,一个继承另一个,并且在两个文件上声明相同的typedef。 为什么会出现冗余,如何
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个用于改造的 POJO 类。 public class AppData(){ String a; String b; String c;
我想知道: 编写这段代码: DataRow[] g = new DataRow[1] ; var t=new StringBuilder().AppendFormat("{0}", g[0]["a
只是一个性能问题...... 假设我有 5 个类,每个类都引用了 System.Data 和一个自己开发的库。这 5 个类是类库的一部分,最终将被构建并发布到一些 Web 应用程序作为引用。 通过将引
我是一名优秀的程序员,十分优秀!