基础设施层上的 Singleton LoggedUser class?

Singleton LoggedUser class on Infrastructure Layer?

我正在开发一个支持许多应用程序 UI 使用者的多层应用程序,例如 WinForms 和 MVC。

首先,我正在开发一个 UI Windows 表单来使用它。

我有以下项目结构:

Application.DataAccess      - IUserRepository....
Application.Busines         - IUserService, ICustomerService...
Application.Infrastructure  - Logging, Exception handling... (Cross-cutting.)
Application.Domain          - Entities, User, Customer...
Application.WindowsForms     - UI

我的大多数业务方法都需要有请求的用户,例如 AddNewCustomer(Customer c, User requestingUser)...

我总是必须通过我的用户界面应用程序将登录的用户传递到业务层,或者应该创建一个单例 class 横切层并将其用于我的所有应用程序?

最好的选择是让您的 UI 应用程序(表示层)跟踪当前登录的用户,并在每次需要进行的调用时将其传递给业务层。尤其如此,因为用户登录是通过 UI 应用程序,通常也是管理活动用户会话的 UI 应用程序。

您的服务(业务层)应该是无状态的,因此应该不知道任何活动或登录用户。