tags:

views:

63

answers:

5

How do I store a list of data that is submitted to a controller for about an hour in memory, so I can call another action and pull this data?

Reason for this: I need to show a demo application, no time to write up the database logic. So I want to simulate a database with a list of objects.

For eg. I send a list of Persons { FirstName, LastName} to a controller action 'Create' to temporarily store it in a list.

I then call another controller action 'GetPeople' at a later time to retrieve this list of Persons.

A: 

Can I understand what your goal here is ? Is it passing data between two controllers? The role of the controller is to fetch data from the underlying data store, process the data, and pass the data to the view for display.

In the scenario you are describing you want to submit data to a controller for a hour and then another controller wants to fetch this data? Why do you want to keep this data in memory? Is it because that both the controller uses the same data and you want to avoid two round trip to the data store.

Venki
I edited my question.
GatesReign
A: 

Depending on how volatile your web server processes are and how important the data is, you can:

  • Store it temporarily in a database (will be the safest)
  • Store it in the web cache
Lance McNearney
A: 

You could use a session

Session["list"] = MyList
Flexo
A: 

It sounds like you just want to mock up some data. Why not just create a mocked up interface that you can put in place of your DB. This would also make your testing much easier.

You can create a static class that returns all the data you need and then just call it. If you want to be able to add to it, make an initialize function that runs when your app first starts up stores all your data in the Session and then just add and remove your data from your Session store.

Kelsey
A: 
public static class PeopleRepository
{
    private static List<Person> people;

    static PeopleRepository()
    {
        people = new List<Person>();
        // some test data
        people.Add(new Person { FirstName = "Foo", LastName = "Bar" });
        // ...
    }
    public static void AddPerson(Person p)
    {
        people.Add(p);
    }
    public static List<Person> GetPersons()
    {
        return people;
    }
}
Mika Kolari