I'm working on Employee Management System. Each employee belong to one department, when employee logged in to system I store UserId and DepartmentId in my class as below
internal static class MyStaticData
{
internal static Guid UserId { get; set; }
internal static Guid DepartmentId { get; set; }
}
When user go to create leave application page and hit save button, I need to send both UserId and DepartmentId to store in database, so what is the proper way to send UserId and DepartmentId to this page between
// Create instance property inside the class and assign it when create new object.
public class LeavePage
{
public Guid UserId { get; set; }
public Guid DepartmentId { get; set; }
}
var leavePage = new LeavePage { UserId = MyStaticData.UserId, DepartmentId = MyStaticData.DepartmentId };
or
// Don't have instance property inside the class but use MyStaticData directly
public class LeavePage
{
public void Save()
{
Db.Save(data, ..., My开发者_JAVA百科SataicData.UserId, MyStaticData.DepartmentId);
}
}
I'm not sure which one is better (less coupling) or does it has better way to do this?
You might want to try using a static instance of some more general class - something like this:
public class User
{
public Guid Id { get; set; }
public Guid DepartmentId { get; set; }
}
public static class SessionContext
{
public static User CurrentUser { get; set; }
}
Then use SessionContext.CurrentUser when saving.
Of course, in this case using a static class is probably inappropriate, and you should look into a session-specific instance.
If there is no need to include instances of your values in your class, do not do it.
精彩评论