将会话数据传递到类库而不使用一堆构造函数?
我在这里得到了我的应用程序,其中实际上每个对象都有一个lastUpdatedBy属性。我在此处输入的信息是该人的用户名,该用户名是从 session("username") 变量中检索的。
如何将此数据传递到类库中的 DAL?
起初我只是将值传递到每个方法中,但我认为这很荒谬,没有理由在每次调用方法时都这样做。
然后我想,如果我只是将它放在每个 DAL 相关类的构造函数中,那就更容易了。
然而,即使在任何给定的页面上,我都有大量的 New() 声明,为此我需要将会话用户名传递为字符串。
有没有一种更有效的方法来做到这一点,以便我只能在一个地方声明它,并且所有东西都会知道它是什么,并且我可以将它传递给类库中的类?
I've got my application here where literally every object has a lastUpdatedBy property. The information I put into here is the person's username, which is retrieved from the session("username") variable.
How can I pass this data to my DAL in the class library?
At first I was just passing in the value into each method, but this is ridiculous I thought, there should be no reason to do that every time a method is called.
Then I thought well if I just put it in a constructor for each of the DAL related classes, that will make it even easier.
However, even still on any given page, I've got a plethora of New() declarations, for which every single line I need to pass in the session username casted as a string.
Is there an even still more efficient way of doing this so that I could only declare this in one place, and everything will know what it is and I can pass it to classes in a class library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几种方法可以解决这个问题。
一种是拥有一个类,为您从会话中检索值。与 Session.Current.Username 类似,Current 是返回会话实例的静态属性,而 Username 是 Session 的简单属性。
另一种是使用依赖注入。您仍然在构造函数中拥有输入(最好是一个类,而不是简单值),但您不需要在代码中显式传递它,因为它会自动注入。
There are several ways to go about it.
One is to have a class, that retrieves the value from session for you. Like Session.Current.Username, with Current being a static property that returns a session instance and Username being a simple property of Session.
Another one is to use dependency injection. You'd still have the input in the constructor (a class preferably instead of the simple value), but you wouldn't need to explicitly pass it on your code as its automatically injected.
我通常会采取措施确保 Thread.CurrentPrincipal 引用当前连接的用户。在某些情况下,这将自动完成:
使用 ASP.NET RoleManager 的 ASP.NET 应用程序
托管在 IIS 中的 WCF 服务,使用principalPermissionMode ="UseAspNetRoles"
在其他情况下,您可能必须自己执行此操作(例如,在WinForms 应用程序中启动应用程序时)。
然后,您的 DAL 可以简单地使用 Thread.CurrentPrincipal.Identity.Name 来识别当前用户。
这比使用 HttpSessionState 更清晰,后者意味着对 ASP.NET 的依赖。
I would generally take steps to ensure that
Thread.CurrentPrincipal
references the currently connected user. This will be done automatically in some cases:an ASP.NET application that uses the ASP.NET RoleManager
a WCF service hosted in IIS that uses principalPermissionMode ="UseAspNetRoles"
In other cases, you may have to do it yourself (e.g. at application startup in a WinForms application).
Then your DAL can simply use
Thread.CurrentPrincipal.Identity.Name
to identify the current user.This is cleaner than using
HttpSessionState
, which implies a dependency on ASP.NET.我将创建一个包含
string Username
属性的AbstractDALObject
抽象类。使所有 DAL 类都从此抽象类派生,并通过创建设置此属性的 DALFactory 类来实例化它们。您的DALFactory
类将包含每种 DAL 类型的CreateXXXDAL
方法,或者您可以使用反射创建一个通用方法,如下所示:I would create an
AbstractDALObject
abstract class that contains astring Username
property. Make all of your DAL classes derive from this abstract class, and instantiate them by creating aDALFactory
class that sets this property. YourDALFactory
class would either contain aCreateXXXDAL
method for each type of DAL, or you could use reflection to create a generic method like below: