好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

c# – 确保只获取属于该用户的行

这对我来说很难解释,但我尽力做到最好.

我有一个具有登录权限的多个用户的应用程序.这些用户可以附加一些员工.我希望避免用户查看/编辑不属于他们的其他员工.

我有一个像这样的User类:

public class User
{
    public int ID { get; set; }
    public string Name { get; set; }

    public User()
    {
    }

    public User(int userid)
    {
        //
        // Gets the user from the database and fills the properties
        //
    }
}

像这样的Employee类:

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Employee()
    {
    }

    public Employee(int employeeid)
    {
        //
        // Gets the employee from the database and fills the properties
        //
    }
}

这个问题就像我编辑员工时应用程序中的查询字符串一样:

id是员工的ID.通过快速编辑此ID,我很幸运能够获取不属于当前登录用户的员工.

虽然这可以通过拥有一个来解决

public Employee GetEmployee(int id) 
{
    // Gets the employee (using this.ID as UserID) from the database
}

User对象上的方法,它为User.ID属性提供存储过程并检查:

SELECT * FROM EmployeeTable WHERE EmployeeID = @EmployeeID AND UserID = @UserID

但有了这个,我总是必须创建一个User对象的实例来获得一个员工.

这使得Employee对象上的Employee(int id)已过时.

没有别的办法吗?

对此的问题是,我处于这样的情况:我不想创建User对象的实例来获得员工,因为我100%确定我拥有正确的员工ID. (避免过多的数据库调用).

我是否真的必须在Employee对象上保留Employee(int id)并创建一个不检查UserID的新存储过程?

这个例子是虚构的.解释它的最好方法莫过于粘贴数百行代码和对象.也许我太过于表现怪胎了.但我只想改进我做多个用户网站的方式.

我真的希望这能解释得很好,我尽我所能.

查看更多关于c# – 确保只获取属于该用户的行的详细内容...

  阅读:48次