创建数据访问对象时最好的方法是什么? [英] What is the best approach when creating data access objects?

查看:75
本文介绍了创建数据访问对象时最好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MyWebpage.aspx.cs中有一个方法,如下所示:

I have a method in MyWebpage.aspx.cs lik so:

public partial class MyWebpage : PageBase
{
    private readonly DataAccessLayer dataAccessLayer;

    protected string GetMyTitle(string myVar, string myId)
    {
        if (string.IsNullOrEmpty(myVar))
        {
            return string.Empty;
        }

        return dataAccessLayer.GetMyTitle(Convert.ToInt32(myId), myVar);
    }
}

在DataAccessLayer类中,我有一个方法

In the DataAccessLayer class, I have a methoud that talks to the DB and does the DAL stuff and returns the title.

从MyWebPage.aspx.cs类访问DAL的最佳实践是什么(因为我需要每次创建一个新的DataAccessLayer()对象吗?应该在我的PageBase类中还是在每次我在后面的代码中调用它的地方?

What's the best practice on accessing the DAL from MyWebPage.aspx.cs class (as in do I need to create a new DataAccessLayer() object each time? Where should I create it in my PageBase class or everytime I call it in a code behind?

推荐答案

首先要从代码后面或表示层访问DAL通常不是一个好习惯,因为在这种情况下,您需要将业务逻辑代码放在代码(表示层)后面,这会引起关注冲突,耦合,重复和许多其他问题。因此,如果您要寻找最佳实践,建议您查看以下链接:

First thing is accessing DAL from your code behind or presentation layer generally is not a good practice. Because in this case you need to put your Business logic code in your code behind(Presentation Layer) which causes conflicting of concerns, high coupling, duplication and many other issues. So, if you're look for the best practices, I suggest to have a look at the following links:

  • Data Layer Guidelines
  • Layered Application Guidelines
  • Object-Relational Metadata Mapping Patterns (especially Repository Pattern) and Domain Logic Patterns

这些都是好书:

还具有用于调用DAL的静态功能。如您所知,静态函数容易受到多线程的攻击,因此,如果您使用DAL函数中共享的任何内容(有时就是这种情况,例如共享连接,命令等),它将破坏您的代码,所以我认为最好避免在此层使用静态函数。

Also about having static function for calling the DAL. As you know static functions are vulnerable to the multi-threading, so if you are using anything shared in the DAL functions (which its the case sometimes, like shared connection, command etc) it will break your code, so I think it's better to avoid static functions in this layer.

这篇关于创建数据访问对象时最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆