代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

在C应用程序中使用LINQ to SQL的最佳实践方法是什么?[设计模式]

  •  4
  • Sergio Tapia  · 技术社区  · 14 年前

    好吧,我一直在努力改进我的编码方式,因为这是我的激情所在。我有一个.dbml文件( LINQ to SQL ,我用它访问我的 SQL Server 数据库。

    想象一下,如果愿意的话,您的数据库中有一个Person表,并且您希望提供一种删除、添加和修改Person记录的方法。

    我目前处理事情的方式是创建名为PersonRepository、Carrepository、DocumentRepository等的类。对于数据库中的每个表,我都创建一个Repository类。

    这些存储库类通常由类似的内容组成:

    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岁),你有什么建议?

    3 回复  |  直到 8 年前
        1
  •  6
  •   Community CDub    8 年前

    以下是您在上面所做工作的问题:

    在应该使用iQuery时使用列表

    Why use AsQueryable() instead of List()?

    基本上,在需要数据之前,您不应该获取数据。使用这种方法,您总是从数据库中获取数据,随后的LINQ查询将作用于所有数据。为什么不建立查询,让Linq把事情细化到只得到你需要的东西的地方,并且只在你需要的时候?

    当扩展方法是完美的时候使用方法

    您正在使用getPeopleFromDob之类的东西创建一个方法的实用程序类。为什么不把它作为扩展方法呢?如果你让他们都可以退货,你也可以连续使用他们。例如。 GetPeople().StatusEnabled().BornInJuly().AgeIsGreaterThan( 57 ); 另外,如果必须这样做,至少考虑在分部类或静态实用程序类中这样做。

    考虑使用ActiveRecord/Repository模式作为根

    http://compiledexperience.com/blog/posts/Implementing-an-ActiveRecord-pattern-in-Linq-to-SQL

    现在您正在创建多个硬编码存储库,但您应该将它们建立在存储库之外?

    为什么不使用验证器?

    如果您使用的是ASP.NET MVC,那么验证是不可或缺的,但是对于WebForms,您也可以使用数据注释验证程序。

    http://adventuresdotnet.blogspot.com/2009/08/aspnet-webforms-validation-with-data.html

        2
  •  2
  •   Peter Mortensen icecrime    8 年前

    我会坚持 single responsibility principle 坚持使用你的知识库类。如果数据库随着时间的推移而增长,请想象一下DataAccess类可以变得多大。你会摆脱它一段时间,但它会增长并增长到你在一个类中有数千行代码的程度(我以前在这样的类中见过15000多行代码),你被卡住了!

        3
  •  2
  •   Peter Mortensen icecrime    8 年前

    您将要使用 知识库 使用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材料。