【发布时间】:2013-03-18 00:42:58
【问题描述】:
这是一个新手问题,我正在设计一个保存用户详细信息的类。让我摸不着头脑的一部分是“许可”
这里是场景。每个用户属于多个城市。并且在那个可以拥有各种权限的城市中。
例如,John 在 NY 和 DC,他在 NY 的权限为 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 拥有用户所在的 Cities 的不同列表(我没有为此发布代码,但它只是一个存储的运行的程序,与上述类似)
userRole 拥有该用户 (1-10) 的所有权限(如上所示),但它不考虑该权限适用于哪个城市。
我希望这是有道理的。 :-/
我想我的问题是,我如何设计课程以确保我的权限与用户所在的每个特定城市(或多个城市)相关联。我应该使用List 以外的类型吗?我离这儿很远吗?
【问题讨论】:
-
那么一个用户可以有多个城市,每个城市有多个权限?
-
这是正确的。所以在我的代码中,我现在显示用户所属的城市列表,在他们选择一个之后,我需要使用该用户的城市权限在表单上显示适当的项目。那么为什么 John 和 Jane 都属于 NY,由于他们在那个城市的权限,他们将能够看到不同的东西。