Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach when creating data access objects?

Tags:

c#

.net

.net-3.5

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);
    }
}

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

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?

like image 723
cdub Avatar asked Dec 06 '12 01:12

cdub


People also ask

What is a Data Access Objects Java?

The Data Access Object (or DAO) pattern: separates a data resource's client interface from its data access mechanisms. adapts a specific data resource's access API to a generic client interface.

What are the benefits of using the Data Access Object pattern?

Access is transparent because the implementation details are hidden inside the DAO. Enables Easier Migration A layer of DAOs makes it easier for an application to migrate to a different database implementation. The business objects have no knowledge of the underlying data implementation.

What is data access object in Visual Basic?

Data Access Objects (DAO) enable you to manipulate the structure of your database and the data it contains from Visual Basic. Many DAO objects correspond to objects that you see in your database—for example, a TableDef object corresponds to a Microsoft Access table.

What does a data access object do?

In software, a data access object (DAO) is a pattern that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, the DAO provides some specific data operations without exposing details of the database.


1 Answers

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

And these are really good books:

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.

like image 65
Cyrus Avatar answered Sep 22 '22 23:09

Cyrus