繁体   English   中英

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

[英]What is the best-practice way to use LINQ to SQL in a C# application? [Design-pattern]

好吧,我总是试图改进我编码的方式,因为这是我的热情。 我有一个.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功能几乎相同。

如果我需要更具体的内容,例如“塞尔吉奥,我需要列出所有在x和y之间出生的人”; 然后我只是将方法添加到PersonRepository类。

public List<Person> GetPeopleFromDOB(DateTime x, DateTime y)
{
    // Do the logic here.
}

我的另一个想法是创建一个DataAccess.cs类,并在其中包含所有这些方法(我们将讨论现有的每个表4-5个方法)并按区域划分它们。

什么是知识渊博的程序员正在做什么,你会为一个热切的年轻程序员(我20岁)提供什么建议?

以下是您正在做的事情的问题:

在使用IQueryable时使用List

为什么使用AsQueryable()而不是List()?

基本上,在您需要之前,您永远不应该获取数据。 使用您的方法,您总是从数据库获取数据,随后的LINQ查询将对所有数据起作用。 为什么不只是构建查询,让LINQ将事情细化到只有你需要的东西,并且只在你需要的时候呢?

扩展方法完善时使用方法

您正在使用GetPeopleFromDOB之类的东西创建一个实用程序类。 为什么不把它作为一个扩展方法? 如果你让所有这些都返回IQueryable,你也可以连续使用它们。 例如GetPeople().StatusEnabled().BornInJuly().AgeIsGreaterThan( 57 ); 此外,如果必须这样做,至少应考虑在部分类或静态实用程序类中执行此操作。

考虑使用ActiveRecord / Repository Pattern作为根

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

我坚持单一责任原则并坚持使用您的存储库类。 如果您的数据库随着时间的推移而增长,请想象DataAccess类有多大。 你会在一段时间内侥幸成功,但它会增长并发展到你在一堂课中有数千行代码的地步(我之前在这样的课程中已经看到了15000多行)并且你被卡住了!

您将希望使用存储库使用L2S直接与DBML连接。

然后,您将需要创建与存储库对话的服务

最后,您将在代码隐藏或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#内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM