I have a Windowsphone client who calls a business layer class(C# Class) through a webservice.
In my business layer class wich is a C# class, not silverlightclass i have List<int> numbers = new List<int>()
And from a method in the business layer class the list is filled with numbers. After this method have filled the list with numbers, the client layers is called again and a new method in my client is called and we are back in the business layer again. Here is the problem, i want to keep the List<int> numbers populated between calls from client layer to business layer. Now every time the businesslayer is called the List<int> numbers gets empty.
In a silverlight class I could use isolated storage to keep data alive on application level. In asp.net I could use application state, session or whatever.
How do I store this List in a C# class to save the data in the List between calls?
1st call
Client -> webservice -> businesslayer = List<int> numbers gets values
2nd call ( The problem)
Client -> webservice -> businesslayer = List<int> numbers I want the list to keep values from first call but instead it is empty because I can’t save the list to something like ApplicationState or Isolated Storage.
Here is my code regarding this problem
public class TimereportDataAccess
{
TimereportDBEntities1 context = new TimereportDBEntities1();
List<int> dayId = new List<int>(); // Used in sendDays() and sendWeeks()
bool submitStatus = true; // Used in sendDays() and sendWeeks()
public List<int> sendDays(List<Common.Day> days, bool status)
{
TimereportMappers mapper = new TimereportMappers();
foreach(var item in mapper.dayMap(days))
{
context.Days.AddObject(item);
context.SaveChanges();
dayId.Add(item.Id);
}
if (status == true)
{
submitStatus = true;
}
else
{
submitStatus = false;
}
return dayId;
}
public void sendWeeks(List<Common.Week> weeks)
{
TimereportMappers mapper = new TimereportMappers();
foreach (var item in mapper.weekMap(weeks))
{
context.Weeks.AddObject(item);
}
context.SaveChanges();
int firstday;
firstday = dayId.FirstOrDefault();
if (submitStatus == true )
{
int reportId = (from u in context.Users
join r in context.Reports on u.Id equals r.UserId
join w in context.Weeks on r.Id equals w.ReportId
join d in context.Days on w.DayId equals d.Id
where d.Id.Equals(firstday)
select r.Id).SingleOrDefault();
Reports report = context.Reports.SingleOrDefault(i => i.Id == reportId);
report.Status = "submitted";
context.SaveChanges();
}
dayId.Clear();
}
In order to keep your objects persisted in the memory, first you need to ensure that your wcf service is not working in per call mode. If this is the case, each time you make a call to the service, a new thread will be generated with new objects. I assume you have marked your service as per session.
Then you can mark the object as static and at the first call, you can fetch the data from the database; and when service receives another call, you can just check whether your list has been populated and send back the memory list.
On the otherhand, there is a problem with this approach; what if the database changes? the list remains the same even if the database changes. So you need to employ some kind of dirty mechanism and mark your list as dirty. And whenever it is dirty you need to fetch it again from the database.
If you think your list does not change, then you do not need to store it in the database, you can just use an XML file.