How to organize Data Access Layer (DAL) in ASP.NET

百般思念 提交于 2019-12-07 07:47:34

问题


I have an ASP.NET Web Forms application developed in C#.

I would like to give structure my application by separating the DAL tasks from the code behind.

I created a class in App_Code, DBUtilities that takes care of the communication with the database, in order not to have duplicated code all along the application. The class has methods to get datatables, scalar values, etc... and they accept as parameters the connection string name and the query command as string.

The problem is that I have still all the queries' commands in my code behind. Many of them are duplicated all around the pages and this cause maintanance problems.

I was wondering if it is a good practice to create a (static) class QueryRepository that contains many string properties and associate to each of them a specific query command. Everytime I want to execute the query command MyCommand I pass to the DBUtilities class the QueryRepository.MyCommand property instaed of the string. Morevoer if I need to change a query command I do it just on the QueryRepository class.

Is it a good way to organize my DAL?


回答1:


For ASP.NET web forms implementing Model-View-Presenter (MVP) pattern can be one good approach to separate your logic and database queries from the UI code behind. You have to write a Presenter class that has a generic reference to a view interface and has a model in it which has database queries, logic etc. The presenter will call functions of the generic view interface in all its functions. Then you can write the actual ASP.net view. In the view you instantiates and reference this presenter and while instantiating the presenter you inject the self object i.e the ASP view itself (using "this" keyword) to the Presenter. You can design proper inheritance for your presenter classes based on your need so that they are reusabe and can be unit tested.

Addition in response to CiccioMiami's query:

Here are couple of links to start

http://www.codeproject.com/KB/aspnet/AspNet_MVP.aspx

http://wiki.asp.net/page.aspx/340/mvp-pattern/

The difference in the MVC and MVP pattern is explained here: http://www.codeproject.com/KB/architecture/DotNetMVPFramework_Part1.aspx

To add to this, for ASP.net Web form architecture, the Requests are tightly coupled with the page life cycle. You have series of page life cycle events and developers write code in each of these events. Thus the business logic becomes tightly coupled to the UI view. Over the period of time this is not a good situation for code maintainability and unit testing. Unit testing is difficult in ASP.net web forms as it is difficult to simulate the requests and page life cycle events. In MVC the requests first comes to a controller. The controller uses the model for business logic and passes the model to view. The view renders using the model data and is returned back as the response to the user. The controller has greater control on the work-flow. You can test the model, DTO transfers etc. as you could do in a standalone app. With web forms there is no controller the request directly comes to ASP.net page which is a view. There is nothing much user can do. Good that Microsoft realized this problem and we have ASP.net MVC. The MVP pattern for ASP.net webforms will solve the code separation problem to some extent. The best option is to use ASP.net MVC if not then you can use MVP with Webforms.




回答2:


Long term answer: I can really recommend you read Professional Enterprise .NET

The ASP.NET website has a good example of the repository pattern which is worth looking at.

I'm no expert, but if your DAL can conform to a best-practise patterns it's more likely to be a good way for it to be organised.

I'm struggling to follow your DAL design without a concrete example, so not sure I can help there.




回答3:


Few steps to follow:

It is good practice to separate the DAL, BLL code from presentation(UI) layer, however to go accordingly below steps would be helpful.

  1. Create DTO (Data Transfer Object) or Entity
  2. Fill the DTO/Entity from presentation layer
  3. Pass it to a public method to your BLL layer and validate Business Logic
  4. Then pass the DTO/Entity to DAL layer (at DAL layer, create a method which return Command, then put your CommandText, CommandType and then Set value, data type and size to all the parameters, also create execute method which get Command and return results).
  5. Finally, execute your desired execute method ( created at DAL layer)



回答4:


namespace DAL
{
    public class DBAccess
    {
        private IDbCommand cmd = new SqlCommand();
        private string strConnectionString = "";
        private bool handleErrors = false;
        private string strLastError = "";
        public DBAccess()
        {
            ConnectionStringSettings objConnectionStringSettings = ConfigurationManager.ConnectionStrings["connectionString"];
            strConnectionString = objConnectionStringSettings.ConnectionString;
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = strConnectionString;
            cmd.Connection = cnn;
            cmd.CommandType = CommandType.StoredProcedure;
        }

        public SqlConnection OpenSqlConnection()
        {
            try {
                SqlConnection Conn = new SqlConnection(strConnectionString);
                Conn.Open();
                return Conn;
            } catch (SqlException e) {
                throw e;
            } catch (Exception ex) {
                throw ex;
            }
        }

        public IDataReader ExecuteReader()
        {
            IDataReader reader = null;
            try {
                this.Open();
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            } catch (Exception ex) {
                if (handleErrors) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return reader;
        }

        public IDataReader ExecuteReader(string commandtext)
        {
            IDataReader reader = null;
            try {
                cmd.CommandText = commandtext;
                reader = this.ExecuteReader();
            } catch (Exception ex) {
                if ((handleErrors)) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return reader;
        }
        public object ExecuteScalar()
        {
            object obj = null;
            try {
                this.Open();
                obj = cmd.ExecuteScalar();
                this.Close();
            } catch (Exception ex) {
                if (handleErrors) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return obj;
        }
        public object ExecuteScalar(string commandtext)
        {
            object obj = null;
            try {
                cmd.CommandText = commandtext;
                obj = this.ExecuteScalar();
            } catch (Exception ex) {
                if ((handleErrors)) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return obj;
        }
        public int ExecuteNonQuery(SqlConnection DBConnection, SqlTransaction DBTransaction, bool IsTransRequired)
        {
            int i = -1;

            try {
                if ((DBTransaction != null)) {
                    cmd.Transaction = DBTransaction;
                }
                i = cmd.ExecuteNonQuery();

            } catch (Exception ex) {
                if (handleErrors) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return i;
        }
        public int ExecuteNonQuery(string commandtext, bool IsTransRequired)
        {
            SqlConnection DBConnection = null;
            SqlTransaction DBTransaction = null;
            int i = -1;
            try {
                cmd.CommandText = commandtext;
                if (((DBConnection == null))) {
                    this.Open();
                    DBConnection = (SqlConnection)this.cmd.Connection;
                    if (IsTransRequired == true) {
                        if (((DBTransaction == null))) {
                            DBTransaction = DBConnection.BeginTransaction();
                        }
                    }
                    i = this.ExecuteNonQuery(DBConnection, DBTransaction, IsTransRequired);
                    if ((DBTransaction != null)) {
                        DBTransaction.Commit();
                    }
                }

            } catch (Exception ex) {
                if ((DBTransaction != null)) {
                    DBTransaction.Rollback();
                }
                if (handleErrors) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            } finally {
                this.Close();
            }
            return i;
        }
        public DataSet ExecuteDataSet()
        {
            SqlDataAdapter da = null;
            DataSet ds = null;
            try {
                da = new SqlDataAdapter();
                da.SelectCommand = (SqlCommand)cmd;
                ds = new DataSet();
                da.Fill(ds);
            } catch (Exception ex) {
                if ((handleErrors)) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return ds;
        }
        public DataSet ExecuteDataSet(string commandtext)
        {
            DataSet ds = null;
            try {
                cmd.CommandText = commandtext;
                ds = this.ExecuteDataSet();
            } catch (Exception ex) {
                if (handleErrors) {
                    strLastError = ex.Message;
                } else {
                    throw;
                }
            }
            return ds;
        }
        public string CommandText{
            get {
                return cmd.CommandText;
            }
            set {
                cmd.CommandText = value;
                cmd.Parameters.Clear();
            }
        }

        public IDataParameterCollection Parameters{
            get {return cmd.Parameters;}
        }
        public void AddParameter(string paramname, object paramvalue)
        {
            SqlParameter param = new SqlParameter(paramname, paramvalue);
            cmd.Parameters.Add(param);
        }

        public void AddParameter(IDataParameter param)
        {
            cmd.Parameters.Add(param);
        }
        public string ConnectionString {
            get { return strConnectionString; }
            set { strConnectionString = value; }
        }
        private void Open()
        {
            cmd.Connection.Open();
        }

        private void Close()
        {
            cmd.Connection.Close();
        }
        public bool HandleExceptions{
            get {return handleErrors;}
            set {handleErrors = value;}
        }
        public string LastError{
            get {return strLastError;}
        }
        public void Dispose()
        {
            cmd.Dispose();
        }
    }
}


来源:https://stackoverflow.com/questions/8256138/how-to-organize-data-access-layer-dal-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!