代理客户关系

Agent Client relationship

我正在尝试开发一个应用程序,其中一个用户(称为代理)在网站注册后可以创建许多客户端。我正在使用 MVC5 和 ASP.NET 身份。我想知道的是,当 Agent 与特定客户端一起工作时,我是否需要在会话中存储客户端 ID,或者每次我在查询字符串或类似的东西中发送 id 以识别代理所在的当前客户端工作。如果有人能指导我正确的方向,我将不胜感激。

样本类是这样的

public class Agent
{
  public int UserId{get; set;}
  public ICollection<Client> Clients{get; set;}

  public virtual ApplicationUser User{get; set;}
}

public class Client
{
  public int ClientId{get; set;}
  public string Name{get; set}
  public Address Address{get; set;}
  public Something1 Something1{get; set;}
  public Something2 Something2{get; set;}
  so on....

  public int UserId{get; set;}
  public virtual Agent Agent{get; set;}
}

public class Address
{
  public int ClientId{get; set;}
  public string Line1{get; set}
  public string Line2{get; set}
  public string PostCode{get; set}


  public virtual Client Client{get; set;}
}

根据 REST 指南,客户端是一种资源,应由 URL 标识。例如/clients/{clientId}查看详情,/clients/{clientId}/edit修改,/clients/{clientId}/delete删除等

您的操作会将 clientId 作为参数,然后使用它来查找合适的客户端。对于用户级权限,您只需将拥有客户端对象的用户集成到查询中。例如:

var userId = User.Identity.GetUserId();
var client = db.Clients.SingleOrDefault(m => m.UserId == userId && m.ClientId == clientId);
if (client == null)
{
    return new HttpNotFoundResult();
}

// whatever