Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ to SQL:检查通用存储库中是否存在通用实体_C#_Linq To Sql_Generics_Domain Driven Design_Repository Pattern - Fatal编程技术网

C# LINQ to SQL:检查通用存储库中是否存在通用实体

C# LINQ to SQL:检查通用存储库中是否存在通用实体,c#,linq-to-sql,generics,domain-driven-design,repository-pattern,C#,Linq To Sql,Generics,Domain Driven Design,Repository Pattern,我有一个这样的通用存储库 public class Repository<T> : IRepository<T> where T: class { DataContext _db; public Repository() { _db = new DataContext("connection string"); } System.Data.Linq.Table<T> GetTable {

我有一个这样的通用存储库

public class Repository<T> : IRepository<T> where T: class
{
    DataContext _db;
    public Repository()
    {
        _db = new DataContext("connection string");
    }
    System.Data.Linq.Table<T> GetTable
    {
        get { return _db.GetTable<T>(); }
    }
    public T GetBy(Func<T, bool> exp)
    {
        return GetTable.SingleOrDefault(exp);
    }
    ....
}
在任何存储库中编写都很容易

_productRepository.GetBy(p => p.Id == 5 // or whatever);
其中productRepository如下所示:

public class ProductRepository : Repository<Product>
{
    public ProductRepository()
        : base()
    {
    }
}
公共类产品存储库:存储库
{
公共产品存储库()
:base()
{
}
}

我之所以这样做是因为我总是想检查实体是否存在,所以我不需要在所有存储库中编写相同的方法。

如果所有实体都有属性Guid Id,例如,您可以为实体创建以下接口:

public interface IEntity
{
    Guid Id { get; set; }
}
并将您的存储库类限制为:

public class Repository<T> : IRepository<T>
    where T : class, IEntity
{
   ....
}

例如,如果所有实体都具有属性Guid Id,则可以为实体创建以下接口:

public interface IEntity
{
    Guid Id { get; set; }
}
并将您的存储库类限制为:

public class Repository<T> : IRepository<T>
    where T : class, IEntity
{
   ....
}


正如旁注:
IfExisted
是一个错误的选择-它是过去式,所以检查
IfExisted(…)
实际上意味着:它确实存在-在过去的某个时候-但不再存在。你想要的是
IfExists(…)
-这就是它现在存在的地方。@marc\s为什么这种方法会是过去时?我只想写一些类似于
\u productRepository.IsExisted(someproduct)
的东西,这是否意味着这个通用存储库中的所有通用方法都是过去时??对不起,我搞不懂,这就是英语的存在方式-
我存在
是过去时-它存在于过去一段时间-但不再存在了。如果这对您有效-很好-但是如果其他开发人员查看此代码,他可能会根据方法的名称得到错误的印象。对于现在存在的东西,你应该使用
.DoesExist(…)
或者只是简单地使用
.exists()
ok@marc_s,那么什么是正确的名称呢,因为我不是这个意思。我只会使用
.exists(…)
-然后当你签入你的代码时,它看起来像
if(repository.exists(…)
这非常清楚,就像旁注一样:
IfExisted
是一个错误的选择-它是过去时,所以检查
IfExisted(…)
实际上意味着:它确实存在-在过去的某个时候-但不再存在。你想要的是
IfExists(…)
-这就是它现在存在的地方。@marc\s为什么这种方法会是过去时?我只想写一些类似于
\u productRepository.IsExisted(someproduct)
的东西,这是否意味着这个通用存储库中的所有通用方法都是过去时??对不起,我搞不懂,这就是英语的存在方式-
我存在
是过去时-它存在于过去一段时间-但不再存在了。如果这对您有效-很好-但是如果其他开发人员查看此代码,他可能会根据方法的名称得到错误的印象。对于现在存在的东西,你应该使用
.DoesExist(…)
或者只是简单地使用
.exists()
ok@marc_s,那么什么是正确的名称呢,因为我不是这个意思。我只会使用
.exists(…)
-然后当你签入你的代码时,它看起来像
if(repository.exists(…)
这非常清晰,不幸的是,这种方法在LINQ to SQL中不起作用,因为它不知道如何将对
IEntity.Id
属性的调用转换为SQL。@StriplingWarrior但它工作得很好,因为我让所有实体都实现了
IEntity
@MGA:虽然我现在无法测试以验证,我相当肯定这行不通。它可以很好地编译,但是当您实际对数据库运行它时,您会得到一个异常。@StriplingWarrior,实际上我对数据库进行了测试,它工作正常,但是您怎么知道它不知道如何将cll转换为
IEntity.Id
with
where t:class,企业
,所有实体都在实施企业??感谢您的考虑,我让它在Linq to Entities的生产代码中运行,它工作得非常好。不幸的是,这种方法在Linq to SQL中不起作用,因为它不知道如何将对
IEntity.Id
属性的调用转换为SQL。@StriplingWarrior,但它工作得很好,因为我让所有实体都实现了它
IEntity
@MGA:虽然我现在无法通过测试来验证,但我相当肯定它不会工作。它可以很好地编译,但是当您实际对数据库运行它时,您会得到一个异常。@StriplingWarrior,实际上我对数据库进行了测试,它工作正常,但是您怎么知道它不知道如何将cll转换为
IEntity.Id
with
where t:class,企业
,所有实体都在实施企业??感谢您的考虑,我在Linq to实体的生产代码中运行了它,它工作得非常完美。