【问题标题】:3 entities execute the same lambda predicate. How creating one lambda expression generic for these entities3 个实体执行相同的 lambda 谓词。如何为这些实体创建一个通用的 lambda 表达式
【发布时间】:2023-03-12 19:30:01
【问题描述】:

我有 3 个实体。
产品
语言标识
产品名称

类别
语言标识
猫名

产品类型
语言标识
类型名称

如您所见,它们每个都有 LangID 属性。 我希望能够创建一个通用存储库,它只包含一个返回Func<T, bool> GetLmbLang()的函数

   public interface IBaseRepository<T> where T : class
   {
        Func<T, bool> GetLmbLang();
   }

   public class BaseRepository<T> : IBaseRepository<T> where T : class
   {

      public Func<T, bool> GetLmbLang()
      {
          //ERROR HERE
          //That dosen't work here
          return (p => p.LangID == 1);
      }
   }

有人有想法???。

【问题讨论】:

    标签: asp.net-mvc-2 entity-framework-4 lambda repository-pattern


    【解决方案1】:

    实现此目的的一种方法是使用 LangID 属性创建一个接口,并让您的每个类都实现它。

    public interface IHasLangID
    {
      string LangID { get; set; }
    }
    
    public class Product : IHasLangID
    ...
    public class Category : IHasLangID
    ...
    public class ProductType : IHasLangID
    ...
    
    public interface IBaseRepository<T> where T : IHasLangID
    ...
    public class BaseRepository<T> : IBaseRepository<T> where T : IHasLangID
    

    【讨论】:

    • @Jean 我猜想的地方和你说的一样,在 BaseRepository 里面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多