我有一个模型,它有一个参考数据的下拉列表。参考数据基于当前用户。因此,用户 A 可以看到分配给他的记录,而用户 B 可以看到不同的记录。关键是,参考数据是基于userId的。
userId 在 Session 中。模型有没有办法访问会话变量?SelectionList 的创建被内置到模型中。因此,我可以将 UserId 作为参数放在构造函数中 - 但我需要在模型的所有构造函数中使用它。似乎是重复工作。我希望模型能够说,“啊,当前用户是 User1”,但它是自我。
可能的?还是我有设计缺陷?
我有一个模型,它有一个参考数据的下拉列表。参考数据基于当前用户。因此,用户 A 可以看到分配给他的记录,而用户 B 可以看到不同的记录。关键是,参考数据是基于userId的。
userId 在 Session 中。模型有没有办法访问会话变量?SelectionList 的创建被内置到模型中。因此,我可以将 UserId 作为参数放在构造函数中 - 但我需要在模型的所有构造函数中使用它。似乎是重复工作。我希望模型能够说,“啊,当前用户是 User1”,但它是自我。
可能的?还是我有设计缺陷?
这样的事情怎么样
public class WibbleModelBuilder
{
private int _userId;
private WibbleRepository _repo;
public WibbleModelBuilder(WibbleRepository wibbleRepository, int userId)
{
_repo=wibbleRepository;
_userId=userId;
}
public WibbleModel Build()
{
var model = new WibbleModel();
model.LookupList = _repo.GetLookupForUser(_userId);
return model;
}
}
现在您可以在控制器中创建 WibbleModelBuilder 并将您的存储库和用户 ID 传递给构造函数。您的模型现在只是一个非常简单的数据对象
public class WibbleModel
{
public IList<ReferenceData> LookupList { get; set;}
}