0

我正在尝试设计一个模块来支持我的应用程序(C#)中的用户身份验证和管理。该模块应作为用于基础设施活动的 DLL 的一部分交付,以便其他开发人员需要将其用作“第三方”组件。这里的要点是我不负责 UI(这取决于用户权限和会话),而只提供要使用的基础架构。

所以我创建了一个UserManager单例类,它允许登录、注销等操作以及更多功能和事件。UserManager 类拥有一个名为的属性CurrentUser,其类型为ApplicationUser

ApplicationUser是一个使用用户详细信息(例如全名、用户名等)创建的类,并且还提供了一个IsEligibleFor(Permission)函数。

我希望用户获取UserManager实例,并通过该CurrentUser属性与当前登录的用户进行通信。理想的方法是在调用注销操作时取消,并在每次用户登录时CurrentUser创建一个新操作。ApplicationUser但这是有问题的,因为这些类的用户能够缓存 的实例ApplicationUser,而不会注意到更改。

这就是为什么我正在考虑转移到一个解决方案,在该解决方案中,我在 ApplicationUser 对象中有一个标志,指示该对象是否仍然有效。但这需要检查 ApplicationUser 的每个方法的有效性,我不喜欢。

我想知道这里是否缺少某种解决方案或模式。

4

1 回答 1

0

听起来像观察者模式是你所追求的。班级将观察UserManager班级,然后在有变化时收到通知。

They could listen for specific changes, so in your scenario, they could listen for when the ApplicationUser object changes and they can then discard the old copy and cache the new one.

In terms of implementation, with .NET you can achieve it pretty easily using events.

于 2013-05-08T12:38:44.020 回答