加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:48:27 所属栏目:百科 来源:网络整理
导读:这对我来说很难解释,但我尽力做到最好. 我有一个具有登录权限的多个用户的应用程序.这些用户可以附加一些员工.我希望避免用户查看/编辑不属于他们的其他员工. 我有一个像这样的User类: public class User{ public int ID { get; set; } public string Name
这对我来说很难解释,但我尽力做到最好.

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

我有一个像这样的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
        //
    }
}

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

~/EditEmployee.aspx?id=1

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的新存储过程?

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读