0

I'm pretty new to ASP.NET MVC but I know that it's pretty much superseded WebForms for any new projects.

I'm a little bit lost how to structure a web application I'm designing that doesn't really have any database elements to it. For simplicities sake it's similar to an online drawing tool, whereby each client would dynamically build something on a canvas (which would instantiate various objects on the server) and then do some processing (probably on it's own thread) on that drawing.

In triad terminology, my "model" is dynamically built up by the client, and will persist as objects (as various data structures which aren't important at this stage) on the server until the user quits. My understanding on ASP.NET MVC is to keep everything stateless, so use of sessions are heavily discouraged.

What is a good way of handling this type of scenario?

6
  • Choose what mechanism you will use to persist your objects. This will help narrow down the subject and make it easier for people to answer. Commented Sep 6, 2013 at 15:40
  • Without using some sort of sessions relying generally on cookies on the user's machine it will be impossible to tell these users apart. Commented Sep 6, 2013 at 15:40
  • Conceptually, I doubt MVC is a pattern you want to use for an application like this. Commented Sep 6, 2013 at 15:41
  • If these objects are small, you could potentially write them out entirely, maybe serialized to the client, and get them back on a post. But I don't think Sessions are discouraged, much less heavily discouraged. Commented Sep 6, 2013 at 15:42
  • indeed, if the drawing is done client-side it's most likely involving some kind of javascript i would imagine. If you use JS to store those objects and send them to an action at time of "save" you should be safe. (e.g. window.shapes = [] then as they draw shapes.push(new rectangle(...)) & shapes.push(new circle(...))--then just pass that data off to an action) Commented Sep 6, 2013 at 15:44

1 Answer 1

1

Everything about the pattern still holds, all you're changing is how you persist your models. You certainly can persist them to session, or anywhere else you'd like. Just encapsulate that persistence layer and the rest of the application won't know/care what the difference is.

For example, let's say you have a Widget model. And you want to save it to and retrieve it from some persistence layer (possibly a database, but it doesn't have to be). You might put some simple factory methods on the model. Something like this:

public class Widget
{
    public static Widget Fetch(int id)
    {
        // fetch the Widget from persistence by its ID and return it
    }

    public void Save()
    {
        // write the current Widget to the persistence medium
    }
}

If you were saving them to a database, you might have some sort of repository which you'd interact with there. An Entity Framework data context often serves the job. But that doesn't have to be what you use.

In this case you might want to create a WidgetRepository class which abstracts the persistence medium. The above factory methods would just use that class directly. Inside of that class, in this particular case, you would have references to System.Web and whatnot for persisting to the session state. (Or it could persist to an XML file, or application state, or a database, or anything.) Outside of that class, nothing else knows that's what's happening. All other code just knows that Widgets are saved to and fetched from that repository. The rest of the code carries on like any other ASP.NET MVC application.

Sign up to request clarification or add additional context in comments.

5 Comments

In my case, the language becomes confusing, because the type of dynamic flow-chart drawing we build up is called a "model"! But what would happen is this would be built over a period of time within a logged in session. How does the Repository pattern help with storing multiple "models" in server memory (with a userid or similar) at run time? Is this the right type of pattern for that case? Writing to the session seems a bit counter-intuitive, but maybe that's just my inexperience? I love the idea of abstracting the persistence medium. Thanks for your input!
@sxthomson: For the terminology, the MVC "models" can also be commonly referred to as "business objects" since your business already has an internal definition of "model." The pattern is really just for abstracting the persistence behind an interface, multiple or single models shouldn't make a difference. I only suggested session because I thought you'd mentioned that originally, but any persistence medium would do the job. If a lot of the data is being built up client-side (it sounds like that may be the case) then the repository interface can also be exposed in JSON via a controller.
It's tricky with our domain, whilst the "model" would get built up client side we need the server to know about that pretty much as it happens. It's because it's a type of simulation model so we start building up what we call a "future event list" (i.e. pre-determined things that will be processed which the user can view). The reason isn't too important but it does require the server to build up a representation of the client "model" in relative real time. Does that make sense? Then the simulation runs and we update the client in relative real-time (using SignalR / Knockout).
@sxthomson: I haven't used SignalR, but as long as the client is constantly notifying the server of updates to the model then the server can still constantly persist it. It seems that the overall patterns still hold, the interface just isn't a traditional "view" in the framework's sense. But any javascript API action, WebAPI method, etc. can still use the server-side models (business objects) like any other controller action.
Thanks David, just wanted a bit of a sanity check! Thanks for all your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.