2013-03-27 33 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);