0

I am havign two tables in DB

table name Questions

columns : _id, QuizId, QuestionText

and second table Answers

columns : _id , QuesId, QuizId , AnswerText , IsRight

Now i have populated such model through Data Access Layer

Class QuestionAnswers
{

   int _id {get;set;}
   string QuestionText {get;set;}
   List<Answer> AnswerList { get; set;}
}

while Answer class is as follows

class Answer
{
   int _id {get;set;}
   string AnswerText {get;set;}
   boolean IsRight{ get;set;}
}

Now since students of one group have same set of QuestionAnswers, so through my Data Access Layer I will pick the set of QA only first time and then I wish to render these existing QA to all other subsequent request.

To achieve this I have to create static List QAList. So how to manage this QAList object because I want to access this object from Web Service project as well as Web Form project as I am havign a common Models and common BusinessLogic class library project.

I simply want to know how to mange this object so that it can be accessible from multiple projects within same solution and how to manage this object so that it is accessible to all users who logged in my web app (can be achieved by making class static).

Abhi
  • 5,501
  • 17
  • 78
  • 133
  • Wouldn't this be what a databse is for? – Andrew Barber Nov 22 '11 at 07:18
  • I don't want to make repetitive connection with database to access same set of data.That is why i wish to fetch data store it in some shared model and then i can render data to each user from there itself. – Abhi Nov 22 '11 at 07:24
  • Have fun reinventing the wheel. – Andrew Barber Nov 22 '11 at 07:26
  • so u want to say that for each request i will create connection , open connection , execute stored proc , and then populate model and then my ASP.net worker process will render html. Why should i repeat these steps while no need is there. – Abhi Nov 22 '11 at 07:29
  • 3
    Welcome to how every data-driven website in the world works. Caching is important, but you seem to be talking about trying to keep a virtual database, in a single instance, accessible to multiple users/applications. That's exactly what network-aware databases are designed to do for you. – Andrew Barber Nov 22 '11 at 07:33
  • yes u are right i want to cache the data – Abhi Nov 22 '11 at 07:35
  • creating connection, opening connection. executing queries and populating the model is much easier than what you are trying to achieve. not only easier, but even safer. Even if what you are trying to do succeeded, you will face some problems with multithreading which will require locks and so on.. just use the database.. it was made to be connected to all the time! –  Nov 22 '11 at 12:36
  • although db approach was much easier , safer but not efficient. – Abhi Nov 23 '11 at 04:57

1 Answers1

3

accessible from multiple projects within same solution

Go to a project where you want to use these classes and add a Project Reference to the project which has these classes.

  • Right click on references.
  • Click on add a reference.
  • Change to the Projects tab.
  • Select your Model project.

accessible to all users who logged in my web app

No, I refuse. You aren't ready to handle the multi-threaded problems of that shared list instance. My advice is to avoid the static keyword.


Brief explanation. Here's a static property.

public static List<Animal> Zoo {get;set;}

ThreadA is looking through the list with this statement

List<Bear> funnyBears = StaticClassName.Zoo.OfType<Bear>().Where(bear => bear.IsFunny).ToList();

Meanwhile, ThreadB does this:

StaticClassName.Zoo.Add(new Zebra());

ThreadA now gets an exception about how lists can't be enumerated while they're being modified.

In addition, learn about RaceConditions (especially for calculations), and Deadlocks (what happens when you try to solve a race condition the second time).

Community
  • 1
  • 1
Amy B
  • 108,202
  • 21
  • 135
  • 185
  • can u briefly explain what type of multi-threaded problem I can face if try to achieve that . How can i made it thread safe . As I m having plenty of time so I want to do it. – Abhi Nov 22 '11 at 07:25
  • thanks david for telling me about the procedure of adding references. I know it or even any 5 day .NET developer knows it. Basically I want to know that once i add reference the .dll file of BLL and models will get copied to wen forms project and also to WCf project and I have to deploy both service project and web form project on different physical servers . so how can that static (shared) object created by one BLL can be accessible though another BLL this what i want to know for your first response . – Abhi Nov 22 '11 at 07:34
  • 1
    "how can the object be in two appdomains?" You have the object in the service, and the service provides access to the object to the clients. – Amy B Nov 22 '11 at 08:47
  • "any 5 day .NET developer knows it", I remember a time when I didn't know it. It does exactly answer what you exactly asked. – Amy B Nov 22 '11 at 08:58