2013-03-27 30 views
1

這是一個新手問題,我正在設計一個將要保存用戶詳細信息的類。一個部分是我撓我的頭是「許可」用戶類的數據類型/類結構

這裏是場景。每個用戶都屬於多個城市。並在那個可以擁有各種權限的城市。

例如,約翰在紐約和DC,在紐約他有權限1,2,3和DC 3,4

我不知道如何來設計我的班能考慮到這一點。

我的表,我沒有設計)看起來像這樣(有一些樣本數據):

userID City permission 
John NY  1 
John NY  2 
John NY  3 
John DC  3 
John DC  4 
Jane DC  1 
Jane DC  2 
Jane NY  6 
在我的C#類

,我已經最初寫出來單獨像這樣:

 public class User 
     { 

      public string userid { get; set; } 
      public string name{ get; set; } //from main user table 
      public string address{ get; set; } //from main user table 
      public List<string> Citites{ get; set; } //from usercitypermission table 
      public List<string> userRole { get; set; }//from usercitypermission table 

public User(string username) 
    { 
     this.userid = username; 

     DataSet ds = new DataSet(); 
     SqlConnection conn = new SqlConnection(cCon.getConn()); 

     SqlCommand cmd = new SqlCommand("sp_getUserDetails", conn); 

     // 2. set the command object so it knows 
     // to execute a stored procedure 
     cmd.CommandType = CommandType.StoredProcedure; 

     // 3. add parameter to command, which 
     // will be passed to the stored procedure 
     cmd.Parameters.Add(
      new SqlParameter("@userName", username)); 

     try 
     { 
      // Open the connection and execute the Command 
      conn.Open(); 
      SqlDataAdapter sqlDA = new SqlDataAdapter(); 

      sqlDA.SelectCommand = cmd; 
      sqlDA.Fill(ds); 
     } 
     catch (Exception ex) 
     { 
      throw (ex); 
     } 
     finally 
     { 
      conn.Close(); 
     } 

     this.userRole = new List<string>(); 
     foreach (DataTable DT in ds.Tables) 
     { 
      foreach (DataRow dr in DT.Rows) 
      { 
       this.userRole.Add(dr["permission"].ToString()); 
      } 
     } 
    } 

當我設置這些,我只是在我的tblUserCityPermission上運行一個獨特的,所以我的城市擁有一個明確的名單Cities用戶在(我沒有發佈代碼,但它只是一個存儲過程運行,類似於上面的那個) 和userRole擁有該用戶(1-10)的所有權限(如上所示),但不考慮適用於該城市的權限。

我希望這是有道理的。 : -/

我想我的問題是,我如何設計班級,以確保我的權限與用戶所在的每個特定城市(或城市)綁定。我應該使用除List以外的其他類型嗎?我在這裏?

+0

所以用戶可以擁有多個城市,每個城市都有多個權限? – 2013-03-27 19:06:06

+0

這是正確的。所以在我的代碼中,我現在顯示用戶需要登錄的城市列表,然後我們需要使用該用戶的城市權限在表單上顯示適當的項目。那麼爲什麼約翰和簡屬於紐約,他們將能夠看到不同的東西,因爲他們在那個城市的權限。 – 2013-03-27 19:08:00

回答

2

您需要爲城市及其權限創建一個類。沿線的東西:

class CityPermission 
{ 
    public string Name; 
    public List<int> Permissions{ get; set; } 
} 

不,你有權限作爲城市的屬性。這又是用戶的屬性:

class User 
{ 
    . 
    . 
    . 
    public List<CityPermission> Citites{ get; set; } 
    . 
    . 
    . 
} 
1

作爲替代定義類型,你可以使用一個Dictionary

public Dictionary<string, List<string>> Permissions { get; set; } 

其中關鍵是城市名和值適合該城市的權限列表。

0

您可以將所有這一切在多級字典,像這樣:

Dictionary<string, Dictionary<string, Dictionary<int, int>>> UserCityPermissions = new Dictionary<string, Dictionary<string, Dictionary<int, int>>>(); 
    UserCityPermissions.Add("John", new Dictionary<string, Dictionary<int, int>>()); 
    UserCityPermissions["John"].Add("NY", new Dictionary<int,int>()); 
    UserCityPermissions["John"]["NY"].Add(1, 1); 
    UserCityPermissions["John"]["NY"].Add(2, 2); 
    UserCityPermissions["John"]["NY"].Add(3, 3); 
    UserCityPermissions["John"].Add("DC", new Dictionary<int, int>()); 
    UserCityPermissions["John"]["DC"].Add(3, 3); 
    UserCityPermissions["John"]["DC"].Add(4, 4); 
    UserCityPermissions.Add("Jane", new Dictionary<string, Dictionary<int, int>>()); 
    UserCityPermissions["Jane"].Add("DC", new Dictionary<int, int>()); 
    UserCityPermissions["Jane"]["DC"].Add(1, 1); 
    UserCityPermissions["Jane"]["DC"].Add(2, 2); 
    UserCityPermissions["Jane"].Add("NY", new Dictionary<int, int>()); 
    UserCityPermissions["Jane"]["NY"].Add(6, 6);