What are the solutions for data persistance in a .NET webservice?
I have a webservice. I give an id to my webservice and this one return the correct objet.
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetMyObject?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
MyObject GetMyObject(string id);
I don't want to use a database. I would like to keep my collection of objects in "memory". So I decided to create a simple object in my webservice like this
public class Service : IService
{
List<MyObject> list = new List<MyObjec>();
public Service()
{
list.Add(new MyObject() { Id = 1, Data = ...} );
list.Add(new MyObject() { Id = 2, Data = ...} );
list.Add(new MyObject() { Id = 3, Data = ...} );
list.Add(new MyObject() { Id = 4, Data = ...} )
...
}
public MyObject GetMyObject(string id)
{
// code to get my object from the list
return myObject;
}
}
It works but the constructor is called each times I call my webservice and I would like to initialize this list once and apply modification on it later. How should I initialize my list and persists it?