Singleton模式/检查空 - 当使用Asp.Net与会议 [英] Singleton Pattern / Check for Null - While using Asp.Net with Session
问题描述
在低于code我想在Asp.net应用程序每个会话只执行一次的GetClassTeacher的方法,我已经使用会话检查调用数据库之前,对象是否为null。
我的问题是,这是实现该方法的最佳方法还是可以使用Singleton模式来实现这一点,如果是如何执行每个会话。
公共类学校
{
公开名单<的Student GT; GetAllStudents(){} 公开名单<教师与GT; GetAllTeachers(){} // Singleton模式或检查NULL
公共老师GetClassTeacher()
{
老师老师=新教师();
教师=会话[教师作为教师
如果(教师== NULL)
{
//从数据库中获取信息老师
}
}
}
我认为使用会话fine--,但你可以通过不实例化一个教师目标减少一些开销,如果你没有为:
公共教师GetClassTeacher()
{
VAR教师=会话[教师作为教师
如果(教师== NULL)
{
//从数据库中获取信息老师
}
}
In the below code i want the "GetClassTeacher" method to be executed only once per session in Asp.net application, I have used session to check whether the object is null before calling the database.
My question is, Is this the best way to implement this method or can i use singleton pattern to accomplish this, If so how to implement it per session.
public class School
{
public List<Student> GetAllStudents() {}
public List<Teacher> GetAllTeachers() {}
//Singleton pattern or Check for Null
public Teacher GetClassTeacher()
{
Teacher teacher = new Teacher();
teacher = Session["Teacher"] as Teacher
if (teacher == null)
{
//Get Teacher Info from Database
}
}
}
I think using session is fine-- but you can cut down on some of the overhead by not instantiating a teacher object if you don't have to:
public Teacher GetClassTeacher()
{
var teacher = Session["Teacher"] as Teacher
if (teacher == null)
{
//Get Teacher Info from Database
}
}
这篇关于Singleton模式/检查空 - 当使用Asp.Net与会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!