好吧,我一直在努力改进我的编码方式,因为这是我的激情所在。我有一个.dbml文件(LINQ to SQL),我用它来访问我的SQL Server数据库。
假设您的数据库中有一个Person表,并且希望提供一种删除、添加和修改Person记录的方法。
我目前的处理方式是创建名为PersonRepository、CarRepository、DocumentRepository等的类。对于数据库中的每个表,我都会创建一个存储库类。
这些存储库类通常由类似于以下内容的内容组成:
MyDatabaseContext db = new MyDatabaseContext();
public Person GetPersonByID(int id)
{
return db.Person.Where(p => p.ID == id);
}每个表的基本CRUD功能基本相同。
如果我需要更具体的东西,例如"Sergio,我需要x和y之间出生的所有人的列表“;那么我只需将该方法添加到PersonRepository类中。
public List<Person> GetPeopleFromDOB(DateTime x, DateTime y)
{
// Do the logic here.
}我的另一个想法是创建一个DataAccess.cs类,并在其中包含所有这些方法(我们谈论的是现有的每个表大约4-5个方法),并将它们按区域划分。
知识更丰富的程序员在做什么,你会为一个热心的年轻程序员(我今年20岁)提供什么建议?
发布于 2010-06-22 03:20:32
以下是您在上面所做的操作的问题:
应该使用IQueryable时使用List的
Why use AsQueryable() instead of List()?
基本上,除非你需要数据,否则你永远不应该得到数据。使用您的方法,您总是从数据库获取数据,随后的LINQ查询将作用于所有数据。为什么不干脆建立查询,让LINQ精细化到只在你需要的时候才能得到你需要的东西呢?
当扩展方法是完美的时使用方法的
您正在使用GetPeopleFromDOB之类的东西创建方法的实用程序类。为什么不把它作为一个扩展方法呢?如果你让所有的函数都返回IQueryable,你也可以连续使用它们。例如,GetPeople().StatusEnabled().BornInJuly().AgeIsGreaterThan( 57 );,如果你必须这样做,至少可以考虑在分部类或静态实用类中这样做。
考虑使用ActiveRecord/Repository模式作为根
http://compiledexperience.com/blog/posts/Implementing-an-ActiveRecord-pattern-in-Linq-to-SQL
现在你正在创建几个硬编码的仓库,但是你应该把它们建立在仓库的基础上吗?
为什么不使用验证器?
如果你使用的是ASP.NET MVC,那么验证是必不可少的,但是对于webforms,你也可以使用Data Annotation验证器。
http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html
发布于 2010-06-22 03:20:57
我会坚持使用single responsibility principle,并坚持使用您的存储库类。如果您的数据库随着时间的推移而增长,想象一下DataAccess类会变得有多大。您将在一段时间内摆脱它,但是它会越来越多地增长到一个类中有成千上万行代码(我以前在这样的类中看到过15000+行),您就卡住了!
发布于 2010-06-22 03:21:23
您将希望使用存储库通过L2S直接连接到您的DBML。
然后,您将需要创建一个与存储库对话的服务。
最后,您将在code-behind或MVC控制器等应用程序中使用该服务。
用户存储库
Namespace Data
#Region "Interface"
Public Interface IUserRepository
Function GetAllUsers() As IList(Of User)
Function GetUserByID(ByVal id As Integer) As User
End Interface
#End Region
#Region "Repository"
Public Class UserRepository : Implements IUserRepository
Private dc As MyDataContext
Public Sub New()
dc = New MyDataContext
End Sub
Public Function GetAllUsers() As System.Collections.Generic.IList(Of User) Implements IUserRepository.GetAllUsers
Dim users = From u In dc.Users
Select u
Return users.ToList
End Function
Public Function GetUserByID(ByVal id As Integer) As User Implements IUserRepository.GetUserByID
Dim viewUser = (From u In dc.Users
Where u.ID = id
Select u).FirstOrDefault
Return viewUser
End Function
End Class
#End Region
End Namespace用户服务
Namespace Data
#Region "Interface"
Public Interface IUserService
Function GetAllUsers() As IList(Of User)
Function GetUserByID(ByVal id As Integer) As User
End Interface
#End Region
#Region "Service"
Public Class UserService : Implements IUserService
Private _ValidationDictionary As IValidationDictionary
Private _UserRepository As IUserRepository
Public Sub New(ByVal validationDictionary As IValidationDictionary, ByVal UserRepository As IUserRepository)
_ValidationDictionary = validationDictionary
_UserRepository = UserRepository
End Sub
Public Function GetAllUsers() As System.Collections.Generic.IList(Of User) Implements IUserService.GetAllUsers
Return _UserRepository.GetAllUsers
End Function
Public Function GetUserByID(ByVal id As Integer) As User Implements IUserService.GetUserByID
Return _UserRepository.GetUserByID(id)
End Function
End Class
#End Region
End Namespace现在只要确保像你上面提到的那样使用CRUD即可。
另外,请原谅我的VB。您可能需要通过http://converter.telerik.com代码转换器运行它来获取C#内容。
https://stackoverflow.com/questions/3087682
复制相似问题