Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net中的静态方法在这种情况下是个坏主意吗?_C#_Asp.net - Fatal编程技术网

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

C# asp.net中的静态方法在这种情况下是个坏主意吗?,c#,asp.net,C#,Asp.net,我有一个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.C

我有一个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;
            }

        }

    }
}
我的问题是

将BAL中的所有函数都写成静态函数是一个好主意还是一个坏主意?我会打电话给BAL做所有的操作


在我看来,你目前的设计没有问题。在使用静态类时,您应该开始担心的时刻是希望保持变量的活动状态


ASP.NET中的静态变量在会话中保持不变。因此,当您在静态变量中保存用户id时,可能会跨不同用户的会话。

设计BAL有两种方法 1.无国籍 2.全州

将方法作为静态成员是一种无状态设计,例如,您不能在类中为每个调用在一个方法中设置变量并在另一个方法中使用。当然,您也可以将其设置为静态的,但它的值是全局的,在web应用程序中不建议这样做

另外,如果你想有一些横切,如日志,事务。。。代码必须具有非静态成员和类


顺便说一句,如果您正在开发一个小型应用程序,这是可以的,如果您正在开发一个企业应用程序,请重新考虑一下

假设我正在开发一个企业应用程序。给我一些改进的方向看看Castle.Proxy,以改进您的横切和添加拦截器,并编写类似ServiceFactory的东西,将您的UI与BizLayer分离。还尝试为DAL和Biz编写一些基类,当然是generic当您需要静态变量时,给我一些改进方向,将它们包装在属性中,并在内部使用HttpContext.Session来存储变量。需要更多信息/想法吗?做得好?如果你想给我更多。我随时准备学习。
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());
            }
        }

    }
}
public static int getUserID(string user_name)