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?
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.