2014-04-13 1 views
1

내가이 시나리오에서 asp.net의 정적 메서드는 좋지 않은 생각입니까?

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 클래스를 가지고있는 것처럼하는 DAL 클래스가

내 질문

이 좋은입니다

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 [email protected]_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

는, 현재의 설계는 문제가되지 않습니다. static 클래스를 사용할 때 걱정할 필요가있는 순간은 변수를 유지하려고 할 때입니다.

ASP.NET의 정적 변수는 세션 이상으로 유지됩니다. 그래서 정적 변수에 사용자 ID를 저장하면 다른 사용자의 세션을 교차 할 수 있습니다.

+0

에 대한 엔터프라이즈 응용 프로그램을 재고를 개발하는 경우, 나에게 당신이 정적 변수를 필요로 할 때 –

+0

을 즉석에서 몇 가지 방향을 제시 특성을 감싸고 내부에 작은 응용 프로그램을 개발하는 경우 그런데

변수를 저장하기 위해'HttpContext.Session'을 사용하십시오. –

+0

더 많은 정보/생각이 필요하십니까? –

0

는 정적 멤버로 방법을 Makeing 1.Stateless 2.Statefull

당신이 하나를 설정하는 클래스의 변수를 가질 수 없습니다 예를 들어 비 저장 디자인 중 하나입니다 설계 발스 2 개 방법이있다 방법 및 모든 호출에 대해 다른 사용 (당연히 acn도 정적이지만 값은 전역이며 웹 응용 프로그램에서는 권장되지 않습니다)

또한 로깅, 트랜잭션, ...) 코드에는 정적 멤버 및 클래스가 아닌 멤버가 있어야합니다. 당신이 확인하고 그

+0

은 엔터프라이즈 애플리케이션을 개발하고 있습니다. 나에게 개선 할 방향을 제시하십시오. –

+0

Castle.Proxy에서 크로스 커팅을 개선하고 인터셉터를 추가하고 ServiceFactory와 같은 것을 작성하여 BizLayer에서 UI를 분리하십시오. 또한 DAL과 Biz (물론 Generic)를위한 기본 클래스를 작성해보십시오. – RezaRahmati

관련 문제