[英]ASP.NET Storing Cross Request Data
In my application I have this entity:在我的应用程序中,我有这个实体:
public class Franchise
{
public Guid Id {get;set;}
public string Name {get;set;}
}
When each of my users log in, they are associated with a franchise.当我的每个用户登录时,他们都与一个特许经营权相关联。
There is another entity:还有一个实体:
public class Client
{
public Guid FranchiseId {get;set;}
public virtual Franchise Franchise {get;set;}
public string Name {get;set;}
/* other useful client information */
}
Depending on my user franchise association will determine which clients they shall see (or are allowed to see)根据我的用户特许经营协会将决定他们将看到(或允许看到)哪些客户
So equivalent of doing所以相当于做
dbContext.Set<Client>().Where(x => x.FranchiseId.Equals(associatedFranchiseId));
I was wondering what the options where of storing that associatedFranchiseId
so each request for data can use that Id
to select the appropriate dataset.我想知道存储该
associatedFranchiseId
FranchiseId 的选项是什么,以便每个数据请求都可以使用该Id
来选择适当的数据集。
APPROACH 1方法一
I created a service that gets these associations and returns the correct dataset.我创建了一个获取这些关联并返回正确数据集的服务。 I thought I could use this in each controller where I need to get the information.
我想我可以在需要获取信息的每个控制器中使用它。 But I thought this maybe costly in database lookup terms.
但我认为这在数据库查找方面可能代价高昂。 It would have to based on the User, so getting that out of the request.
它必须基于用户,因此从请求中获取。
I just am not sure how I would go about doing this.我只是不确定我将如何去做。
The process is:过程是:
I've used something similar and use the session
and application
spaces for the objects.我使用了类似的东西,并为对象使用了
session
和application
空间。
So, when the application fires up load all the franchise objects into application
:因此,当应用程序启动时,将所有特许经营对象加载到
application
:
Application["Franchise"] = MethodToLoadFranchiseInfo();
You can then access this at anytime via Franchise f = Application["Franchise"];
然后您可以随时通过
Franchise f = Application["Franchise"];
访问它Franchise f = Application["Franchise"];
Similarly, for the clients, when they login, load the client info into Session
in a similar fashion.同样,对于客户端,当他们登录时,以类似的方式将客户端信息加载到
Session
中。
The only caveat if that you'll need to refresh the Application
object when there's an update, and same for the session, or require a log out and log back in.唯一需要注意的是,如果您需要在更新时刷新
Application
对象,会话也是如此,或者需要注销并重新登录。
This way you only have one hit to the database and in memory accessible objects!这样你就只有一次命中数据库和内存中可访问的对象!
* Edit to Add * * 编辑添加 *
Just had some more thoughts on this, and probably something I'll look to implement myself.只是对此有更多想法,可能还有一些我会自己实现的想法。 If you put a timestamp in both the
application
and session
objects, if the session object is older than the application
object (which will be updated centrally being application wide), then hit the database and refresh the session
object.如果在
application
和session
对象中都放置了时间戳,如果会话对象比application
对象旧(它将在应用程序范围内集中更新),则访问数据库并刷新session
对象。
That way you do not get the log out / log back in situation when something is changed by the backend / admin.这样,当后端/管理员更改某些内容时,您不会退出/重新登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.