DataAccess Layer 和 class,我应该把函数放在哪里?
DataAccess Layer and class, where do I put my functions?
我正在使用 Dapper 通过数据访问 class 填充我的 class。在这个 class 我做了我所有的 CRUD 操作。
public class Product
{
public int id { get; set; }
public string description {get;set;}
public string value {get;set;}
}
public class ProductDa
{
//CRUD operations using Dapper
public List<Product> GetAllElements();
}
现在我需要创建一个函数来计算 return 具有特定值的元素的数量。
Public int ReturnCountValue(int val)
在这个函数中,我将调用我的 GetAllElements
,然后使用 LINQ 来 return 所需的值。
我想知道的是,我应该把 ReturnCountValue
函数放在哪里:ProductDa
(数据访问层)或 Product
(基础 class) ?
不要编写任何 ReturnCountValue
方法,使用 LINQ 代替您的任何方法作为 Count。我强烈推荐 Repository 而不是 DAL。 here
鉴于您使用的模式,我认为适合您的心智模型如下:
Product
代表数据库中的一行——它是一个普通的旧 C# 对象 (POCO),没有什么特别智能的地方。它应该只是自动属性的集合,就像您在示例中一样。
ProductDa
应该是您访问数据的地方。要弄清楚您是否正在访问数据,您应该问自己 "will I be querying the database to implement this method?" 或 "will I be using Dapper to implement this method?" 如果答案是肯定的,那么与数据库的交互就在 class 及其方法中。
至于您是应该使用 LINQ 的 Count()
方法还是捆绑您自己的方法:在数据访问级别,您应该避免为 Count()
捆绑您自己的方法。数据访问层应该抽象出你如何查询数据库的细节;消费者对数据访问层的进一步操作(Count
、Any
、First
等)是可以的。另外,由于您已经返回 List<Product>
,因此您可以访问 List.Count
属性,因此调用者可以轻松访问。
考虑到所有这些,我将修改您的 ProductDa
class 如下
public class ProductDa
{
//CRUD operations using Dapper
public List<Product> GetAllElements();
//query database using Dapper to get all the elements where Value == value
public List<Product> GetAllElementsWithValue(int value);
}
并按如下方式使用 class 以获得返回的 Product
的数量:
var dataAccess = new ProductDa();
var threeValuedItems = dataAccess.GetAllElementsWithValue(3);
int numberOfItems = threeValuedItems.Count;
或者,更简洁地说:
var dataAccess = new ProductDa();
var numberOfItemsWithValueThree = dataAccess.GetAllElementsWithValue(3).Count;
我正在使用 Dapper 通过数据访问 class 填充我的 class。在这个 class 我做了我所有的 CRUD 操作。
public class Product
{
public int id { get; set; }
public string description {get;set;}
public string value {get;set;}
}
public class ProductDa
{
//CRUD operations using Dapper
public List<Product> GetAllElements();
}
现在我需要创建一个函数来计算 return 具有特定值的元素的数量。
Public int ReturnCountValue(int val)
在这个函数中,我将调用我的 GetAllElements
,然后使用 LINQ 来 return 所需的值。
我想知道的是,我应该把 ReturnCountValue
函数放在哪里:ProductDa
(数据访问层)或 Product
(基础 class) ?
不要编写任何 ReturnCountValue
方法,使用 LINQ 代替您的任何方法作为 Count。我强烈推荐 Repository 而不是 DAL。 here
鉴于您使用的模式,我认为适合您的心智模型如下:
Product
代表数据库中的一行——它是一个普通的旧 C# 对象 (POCO),没有什么特别智能的地方。它应该只是自动属性的集合,就像您在示例中一样。
ProductDa
应该是您访问数据的地方。要弄清楚您是否正在访问数据,您应该问自己 "will I be querying the database to implement this method?" 或 "will I be using Dapper to implement this method?" 如果答案是肯定的,那么与数据库的交互就在 class 及其方法中。
至于您是应该使用 LINQ 的 Count()
方法还是捆绑您自己的方法:在数据访问级别,您应该避免为 Count()
捆绑您自己的方法。数据访问层应该抽象出你如何查询数据库的细节;消费者对数据访问层的进一步操作(Count
、Any
、First
等)是可以的。另外,由于您已经返回 List<Product>
,因此您可以访问 List.Count
属性,因此调用者可以轻松访问。
考虑到所有这些,我将修改您的 ProductDa
class 如下
public class ProductDa
{
//CRUD operations using Dapper
public List<Product> GetAllElements();
//query database using Dapper to get all the elements where Value == value
public List<Product> GetAllElementsWithValue(int value);
}
并按如下方式使用 class 以获得返回的 Product
的数量:
var dataAccess = new ProductDa();
var threeValuedItems = dataAccess.GetAllElementsWithValue(3);
int numberOfItems = threeValuedItems.Count;
或者,更简洁地说:
var dataAccess = new ProductDa();
var numberOfItemsWithValueThree = dataAccess.GetAllElementsWithValue(3).Count;