简体   繁体   English

在这种情况下,asp.net中的静态方法是一个坏主意吗?

[英]Static Method in asp.net on this scenario is a bad idea?

i have a DAL CLASS which is like Below 我有一个DAL类,如下

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace SomeNameSpace
{
    public class DAL
    {
        public  string _ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["xClassConnectionString"].ConnectionString;

        public static DataSet GetDataSet(string sql)
        {
            try
            {
                using (SqlConnection connection2 = new SqlConnection(Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["xClassConnectionString"].ConnectionString)))
                {
                    SqlCommand cmd = new SqlCommand(sql, connection2);
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);
                    //   Connection.Close();
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    return ds;
                }

            }
            catch (SqlException err)
            {
                // Replace the error with something less specific.
                // You could also log the error now.
                throw new ApplicationException("Data error. " + err.Message.ToString());
            }

        }

        public static DataSet GetDataSet(string sql, Dictionary<string, dynamic> dictionary)
        {
            try
            {
                using (SqlConnection connection2 = new SqlConnection(Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["xClassConnectionString"].ConnectionString)))
                {
                    SqlCommand cmd = new SqlCommand(sql, connection2);
                    cmd.CommandType = CommandType.Text;
                    //Dictionary<string, dynamic> dictionary = new Dictionary<string, dynamic>();
                    foreach (KeyValuePair<string, dynamic> pair in dictionary)
                    {
                        cmd.Parameters.AddWithValue(pair.Key, pair.Value);
                    }

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataSet ds = new DataSet();
                    adp.Fill(ds);

                    return ds;
                }

            }
            catch (SqlException err)
            {
                // Replace the error with something less specific.
                // You could also log the error now.
                throw new ApplicationException("Data error. " + err.Message.ToString());
            }

        }


        public static DataTable GetDataTable(string sql)
        {
            DataSet ds = GetDataSet(sql);

            if (ds.Tables.Count > 0)
                return ds.Tables[0];
            return null;
        }


        public static int ExecuteSQL(string sql)
        {
            try
            {
                using (SqlConnection connection2 = new SqlConnection(Convert.ToString(System.Configuration.ConfigurationManager.ConnectionStrings["xClassConnectionString"].ConnectionString)))
                {
                    string BegSql = "BEGIN TRY BEGIN TRANSACTION ";
                    string EndSql = " COMMIT TRANSACTION END TRY BEGIN CATCH  ROLLBACK TRANSACTION END CATCH";
                    string NewSql = BegSql + sql + EndSql;
                    sql = NewSql;
                    SqlCommand cmd = new SqlCommand(sql, connection2);
                    connection2.Open();
                    return cmd.ExecuteNonQuery();
                }

            }
            catch (System.Exception ex)
            {
                return -1;
            }

        }

    }
}

i also have a BAL class which holds all the function 我也有一个BAL类,可以容纳所有功能

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;

namespace SomeNameSpace
{
    public class BAL
    {

        public static int getUserID(string user_name)
        {
            try
            {
                //string sql = "select user_id from CI_Users where user_name=@user_name";
                string sql = "select user_id from CI_Users where user_name=1";
                return Convert.ToInt32(DAL.GetDataTable(sql).Rows[0][0]);
            }
            catch (System.Exception ex)
            {
                throw new ApplicationException("Data error." + ex.Message.ToString());
            }
        }

    }
}

my question is 我的问题是

Is this a good/bad idea to write all functions in BAL as static ? 将BAL中的所有函数编写为静态函数是一个好主意吗? i will call BAL to do all operations. 我将致电BAL进行所有操作。

public static int getUserID(string user_name)

In my opinion, your current design is no problem. 我认为,您当前的设计没有问题。 The moment you should start worrying when using static classes is when you want to keep variables alive. 使用static类时,您应该开始担心的时候就是要使变量保持活动状态。

Static variables in ASP.NET are kept over sessions. ASP.NET中的静态变量通过会话保留。 So when you save a user id in a static variable, that might cross different users' sessions. 因此,当您将用户ID保存在静态变量中时,这可能会跨越不同用户的会话。

There are 2 approaches in designing BALs 1.Stateless 2.Statefull 设计BAL有两种方法:1.无状态2.有状态

Makeing methods as a static member is one of Stateless designs for example you can't have a variable in your class to set in one method and use in another for every call (of course you acn have it static too but it's value is global and is not advised in web application) 使方法成为静态成员是无状态设计的一种,例如,您不能在类中为每个调用设置变量,而在每次调用时都将其用于另一种方法(当然,您也可以将其设为静态,但是它的值是全局的,在Web应用程序中不建议使用)

Also if you want to have some cross cutting (such as logging, transaction, ...) codes you have to have non static members and classes. 同样,如果您想使用一些交叉代码(例如日志记录,事务等),则必须具有非静态成员和类。

By the way if you are developing a small application it is OK and if you are developing an enterprise application rethink about that 顺便说一句,如果您正在开发一个小型应用程序就可以了,如果您正在开发一个企业应用程序,请对此重新考虑

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

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