我有一个类似下面的DAL CLASS
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类
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());
}
}
}
}
我的问题是
将BAL中的所有函数写为静态是一个好/坏的想法吗?我将致电BAL进行所有操作。
public static int getUserID(string user_name)
答案 0 :(得分:0)
在我看来,您目前的设计没有问题。使用static
类时应该开始担心的那一刻是你希望保持变量存活的时候。
ASP.NET中的静态变量通过会话保留。因此,当您将用户ID保存在静态变量中时,可能会跨越不同的用户。会话。
答案 1 :(得分:0)
设计BAL有两种方法 1.Stateless 2.Statefull
将方法设置为静态成员是无状态设计之一,例如,您可以在类中设置一个变量,以便在一个方法中设置并在另一个方法中使用另一个方法(当然,您也可以将其设置为静态但是它的价值是全球性的,不建议在网络应用程序中使用)
此外,如果您想要一些横切(例如记录,交易,...)代码,您必须拥有非静态成员和类。
顺便说一下,如果你正在开发一个小应用程序,那就没关系,如果你正在开发一个企业应用程序,那就重新考虑一下