這是一個新手問題,我正在設計一個將要保存用戶詳細信息的類。一個部分是我撓我的頭是「許可」用戶類的數據類型/類結構
這裏是場景。每個用戶都屬於多個城市。並在那個可以擁有各種權限的城市。
例如,約翰在紐約和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
以外的其他類型嗎?我在這裏?
所以用戶可以擁有多個城市,每個城市都有多個權限? – 2013-03-27 19:06:06
這是正確的。所以在我的代碼中,我現在顯示用戶需要登錄的城市列表,然後我們需要使用該用戶的城市權限在表單上顯示適當的項目。那麼爲什麼約翰和簡屬於紐約,他們將能夠看到不同的東西,因爲他們在那個城市的權限。 – 2013-03-27 19:08:00