2

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
       }   
   }
}
user841683
  • 335
  • 6
  • 17

3 Answers3

2

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
       }   
   }
davecoulter
  • 1,806
  • 13
  • 15
  • Thanks for your reply, Do you think it uses more memory when u instantiate and assign it from session? – user841683 Aug 03 '11 at 23:26
  • 3
    Well, instantiating an object definitely uses memory-- and I am making the assumption that you return null if you don't find a Teacher. If on the other hand, you return a newly minted Teacher object, you should stay with what you've got. In any case Session state is fine to use as long as it is alright for the memory to be a little volatile, and if figure you wont have scaling issues when the application gets larger. – davecoulter Aug 03 '11 at 23:34
  • @user841683: Any time you say `new ...`, it uses memory. This just allows you to avoid creating a new instance that you just end up overwriting in the next line anyway. – StriplingWarrior Aug 03 '11 at 23:35
2

Checking for null is perfectly valid. Using Session is also valid.

    public static Teacher GetClassTeacher()
    {
        Teacher teacher = HttpContext.Current.Session["Teacher"] as Teacher;
        if (teacher == null)
        {
            //Get Teacher Info from Database
            teacher = GetTeacherFromDB();
            HttpContext.Current.Session["Teacher"] = teacher;                
        }
        return teacher;
    }
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
0

Here's a sample of singleton: http://msdn.microsoft.com/en-us/library/ff650316.aspx. And also, some investigation into different implementations: http://www.yoda.arachsys.com/csharp/singleton.html.

Otherwise, I'm not sure how to say if it's "best" to use a singleton (or not to use one). Your situation will help determine it. Personally, I don't find much need for it in what I do in asp.net.

Chains
  • 12,541
  • 8
  • 45
  • 62
  • In Asp.Net in what scenario i will use singleton as most of the job can be done with Session / Cache / Static by checking whether it is null nor not – user841683 Aug 03 '11 at 23:34
  • 1
    If I understand singletons...big if...they are intended to prevent you from creating more than one instance of a class. But they can be a big problem if they are in a position to interrupt code execution. As long as you're careful to always check for null before you instantiate, then the effect is the same, whether you use a singleton or not. So why use it? I'd say don't. There's a more in-depth discussion about that here: http://stackoverflow.com/questions/228164/on-design-patterns-when-to-use-the-singleton – Chains Aug 03 '11 at 23:43