2

I need to develop a simple ASP.NET application that allows to display client orders in the restaurant, add and remove orders from the table. The problem I have is that I was instructed to not use any database and store all the data in memory (session) as long as application is running.

I am a beginner in C# and ASP.NET and it's hard for me to figure out how to do that without actual database (I had experience with Java and Spring MVC, but I was only making applications with database so I dont have any experience in making web apps that saves the data in memory).

No matter how I think about it if I create a new order and add it to the list in a view that has adding order form, how can I make sure that the same List I created will be displayed after I move to the view that displays the list. It's a different controller so it doesn't seem like it's going to have access to the List object I created in another controller. Is there anything I can do to solve this problem?

T4under4
  • 125
  • 3
  • 13
  • 1
    _not use any database and store all the data in memory (session)_. You could probably twist this constraint and use some kind of in-process database. But that would be dumb. As is the idea of not using a database to store this stuff. If the IIS process crashes (which it will if you are using up all of it's memory storing this stuff), you lose all your orders. This is a bad idea and definitely won't scale. – Nick.Mc Oct 27 '19 at 10:37
  • ..of course you could just post the List to the next controller which just renders it (saves it) hidden on the next page. But this is also a bad idea because your list is lost when the web page disappears – Nick.Mc Oct 27 '19 at 10:39
  • 2
    Yeah, not writing this data back to a DB or other storage system will jsut fail horribly. .NET is notoriously stingy with RAM, storing a lot of data of this sort will fail. particular in Web Applications. You need to offload this job to something else. | If this is about security/cleaning the data on a reboot, you could just put the DB Files on a Ramdisk. Same level of security, none of the issues. But the best bet is to ask **why** you are not to use a Databases. There is a 95% chance, this was just a decision by someone who did not know anything. – Christopher Oct 27 '19 at 10:43
  • It's just an excersize. They want us specifically to use the session/memory to store data. I read that if memory will get used up and crashes the app all data is gone, but its just an introduction. I'm doing what I'm told to as any other solution won't be accepted as the correct answer. It needs to be done by saving data in memory. I'm just wondering how to do that and be able to pass objects created in one page to the other page that will display those objects. – T4under4 Oct 27 '19 at 11:30
  • 1
    "how can I make sure that the same List I created will be displayed after I move to the view that displays the list"... The Session object persists across all views and actions used by the current user, until such time as the session ends (e.g. because it times out, or the user closes the browser). Each user gets their own Session object. https://stackoverflow.com/questions/14138872/how-to-use-sessions-in-an-asp-net-mvc-4-application has a simple starter example – ADyson Oct 27 '19 at 11:33
  • This is something I was looking for. I'll try to work with this. – T4under4 Oct 27 '19 at 12:10
  • OK, so to clarify.. this is not real life, it's just an exercise. – Nick.Mc Oct 27 '19 at 22:44

2 Answers2

2

Comments are right, but anyway you can achieve this using differents solutions (singleton service, session, static property)

In the startup :

services.AddSingleton<OrdersManager>();

An OrdersManager class or whatever :

public class OrdersManager 
{
    public OrdersManager()
    {
        OrdersById = new ConcurrentDictionary<string, Order>();
    }

    private ConcurrentDictionary<string, Order> OrdersById { get; }

    public void AddOrder()
    {
        OrdersById.TryAdd(...);
    }

    ... All others usefull methods
}

And wherever you can recall your OrdersManager :

var manager = serviceProvider.GetService(typeof(OrdersManager));
manager.AddOrder(new Order(...));

This is just one example of one of many possible implementations.

Dmo
  • 161
  • 1
  • 7
1

I think you should argue for a persistent storage. You could use SQLLite or even a file.

For in-memory databases you could use:

Here's is an example connection string SQLLite, please note the .InMemory bit.

@"Server=(localdb)\mssqllocaldb;Database=EFProviders.InMemory;Trusted_Connection=True;ConnectRetryCount=0");
tymtam
  • 31,798
  • 8
  • 86
  • 126