C# 堆栈溢出异常(基本架构缺陷)
我正在使用一个懒惰的单身追随者 此对象的目的是提供对应用程序中所有其他方法的引用 例如,C# 堆栈溢出异常(基本架构缺陷),c#,design-patterns,singleton,stack-overflow,lazy-evaluation,C#,Design Patterns,Singleton,Stack Overflow,Lazy Evaluation,我正在使用一个懒惰的单身追随者 此对象的目的是提供对应用程序中所有其他方法的引用 例如,GetTheme(Context.Current.User.Id)获取当前用户的主题,以及大量其他方法 我遇到的问题是,当对象已经实例化时,如何处理状态的变化 也就是说,如果一个用户来到该网站并且没有登录,则在创建新用户的过程中使用上下文对象 但是,登录后,用户对象为空,因为它已实例化 我试着用下面的方法来处理这个问题,使公共属性引用成为检查null引用的私有方法,并尝试确定它是否应该是null 不幸的是,这
GetTheme(Context.Current.User.Id)
获取当前用户的主题,以及大量其他方法
我遇到的问题是,当对象已经实例化时,如何处理状态的变化
也就是说,如果一个用户来到该网站并且没有登录,则在创建新用户的过程中使用上下文
对象
但是,登录后,用户
对象为空,因为它已实例化
我试着用下面的方法来处理这个问题,使公共属性引用成为检查null
引用的私有方法,并尝试确定它是否应该是null
不幸的是,这会变成一个无限循环,每次都会崩溃
我以前尝试过让用户对象本身变懒,但出于某种奇怪的原因,它在调用时没有实例化,并且保持null
我要寻找的是,如何使我的惰性单例的User
属性在被调用时对自身进行求值,并在其null
但能够被填充时对自身进行实例化
条件是,MVC全局User
对象属性User.Identity.Name
不为空,并在加载时传递到会话中以在模型中拉入,并且数据库中存在一个使用用户名作为键的用户
public sealed class Context
{
public static Context Current { get { return lazy.Value; } }
private static readonly Lazy<Context> lazy =
new Lazy<Context>(() => new Context());
public UserMeta User { get { return _meta(); } }
private Context()
{
Deployment = GetCurrentDeploymentType();
Device = (Device)HttpContext.Current.Session["CurrentDevice"];
}
private UserMeta _meta()
{
//If the current object is null,
//but the user has been authenticated, populate the object
if (Current.User == null &&
!string.IsNullOrEmpty((string)HttpContext.Current.Session["UserEmail"]))
{
//check that the user is in the database first
var _userTry =
Sql.Read.UserByEmail((string)HttpContext.Current.Session["UserEmail"]);
if (_userTry == null)
{
return new UserMeta(
new UserMeta((string)HttpContext.Current.Session["UserEmail"]));
}
return null;
}
//If the Current Instance already has a populated User Object,
//just use that
else if (Current.User != null)
return Current.User;
else
return null;
}
}
公共密封类上下文
{
公共静态上下文当前{get{return lazy.Value;}}
私有静态只读惰性=
新的惰性(()=>newcontext());
公共用户meta用户{get{return _meta();}}
私有上下文()
{
部署=GetCurrentDeploymentType();
设备=(设备)HttpContext.Current.Session[“CurrentDevice”];
}
私有UserMeta_meta()
{
//如果当前对象为空,
//但用户已通过身份验证,请填充该对象
if(Current.User==null&&
!string.IsNullOrEmpty((string)HttpContext.Current.Session[“UserEmail”])
{
//首先检查用户是否在数据库中
var\u userTry=
Sql.Read.UserByEmail((字符串)HttpContext.Current.Session[“UserEmail]”);
如果(_userTry==null)
{
返回新的UserMeta(
新的UserMeta((字符串)HttpContext.Current.Session[“UserEmail”]);
}
返回null;
}
//如果当前实例已填充用户对象,
//就用这个吧
else if(Current.User!=null)
返回当前用户;
其他的
返回null;
}
}
如果您使用私有字段,它将解决问题
public sealed class Context
{
private UserMeta _user = null;
public static Context Current { get { return lazy.Value; } }
private static readonly Lazy<Context> lazy = new Lazy<Context>(() => new Context());
public UserMeta User
{
get
{
if (_user == null) _user =_meta();
return _user;
}
}
private Context()
{
private UserMeta _meta()
{
if (!string.IsNullOrEmpty((string)HttpContext.Current.Session["UserEmail"]))
{
//check that the user is in the database first
var _userTry =
Sql.Read.UserByEmail((string)HttpContext.Current.Session["UserEmail"]);
if (_userTry == null)
{
return new UserMeta(
new UserMeta((string)HttpContext.Current.Session["UserEmail"]));
}
return null;
}
}
公共密封类上下文
{
private UserMeta\u user=null;
公共静态上下文当前{get{return lazy.Value;}}
private static readonly Lazy Lazy=new Lazy(()=>newcontext());
公共用户元用户
{
得到
{
如果(_user==null)_user=_meta();
返回用户;
}
}
私有上下文()
{
私有UserMeta_meta()
{
如果(!string.IsNullOrEmpty((string)HttpContext.Current.Session[“UserEmail”]))
{
//首先检查用户是否在数据库中
var\u userTry=
Sql.Read.UserByEmail((字符串)HttpContext.Current.Session[“UserEmail]”);
如果(_userTry==null)
{
返回新的UserMeta(
新的UserMeta((字符串)HttpContext.Current.Session[“UserEmail”]);
}
返回null;
}
}