1

这是一个新手问题,我正在设计一个包含用户详细信息的类。让我摸不着头脑的一部分是“许可”

这是场景。每个用户属于多个城市。在那个可以拥有各种权限的城市。

例如,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吗?我离这儿很远吗?

4

3 回答 3

2

您还需要为城市及其权限创建一个类。类似于以下内容:

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

不,您拥有作为城市财产的权限。这又是用户的属性:

class User
{
    .
    .
    .
    public List<CityPermission> Citites{ get; set; }
    .
    .
    .
}
于 2013-03-27T19:10:30.647 回答
1

作为定义类型的替代方法,您可以使用Dictionary

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

其中键是城市名称,值是适用于该城市的权限列表。

于 2013-03-27T19:11:15.167 回答
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);
于 2013-03-27T19:16:41.973 回答