我在Global.asax中创建了一个类:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
//SHOULD I PUT THE LIST DESCRIBLED BELOW IN HERE?
}
public sealed class security
{
private static readonly Lazy<security> lazy = new Lazy<security>(() => new security());
public static security Instance { get { return lazy.Value; } }
private security()
{
}
//OR SHOULD I PUT THE LIST DESCRIBLED BELOW IN HERE?
}
问题:
public static List<permissionTemp> userPermissionSet { get; set; }
,但我不知道该放置该行的位置,以便在应用程序启动时立即创建。我怀疑是否需要使用单例模式,因为每次我需要在列表中添加项目时我都需要获取此类的实例..我猜是的!
答案 0 :(得分:1)
我会创建一个包含权限集的Singleton类,并在应用程序中使用它。
public class PermissionManager{
private PermissionManager() { UserPermissionSet = new List<permissionTemp>(); }
private PermissionManager _instance;
public PermissionManager Instance{
get{
if (_instance==null) _instance=new PermissionManager();
return _instance;
}
}
public List<permissionTemp> UserPermissionSet { get; private set; }
}
注意:只是一个简单的实现......你需要实现一个线程安全的构造函数
然后在您使用的代码中
PermissionManager.Instance.UserPermissionSet