Eco for MVC

Let it be known from the start that I am a MVC newbie so please correct me when needed.

I looked over Peter Morris’s example posted here and got intrigued…

MVC runtime does a lot of magic name matched routing to reduce coding for us. Like this for example:

        [HttpPost]
        public ActionResult Edit(
            string statusCode, //This is the original value passed from the route in the URL
            string code, //From the html form post
            string name)
        {
            var status = GetStatusObject(statusCode);
            status.Code = code;
            status.Name = name;
            if (Commit())
                return RedirectToAction("Index");
            return View(status);
        }

The MVC runtime understands that we want to the call Edit method when the Edit action is performed on a view. And it also figures out what the arguments mean by matching the names from the model.

So if I were to rename the arguments like this:

        [HttpPost]
        public ActionResult Edit(
            string statusCode, //This is the original value passed from the route in the URL
            string codex, //From the html form post
            string nayme)
        {
            var status = GetStatusObject(statusCode);
            status.Code = codex;
            status.Name = nayme;
            if (Commit())
                return RedirectToAction("Index");
            return View(status);
        }

… the Edit will still be called, but codex and nayme will always be null.

The preferred way to send in data to our controllers is to use the whole object, rather than the separate attributes. Like this:

    [HttpPost]
    public ActionResult Edit(
      string statusCode, //This is the original value passed from the route in the URL
      Status thestatus)
    {
      var status = GetStatusObject(statusCode);
      status.Code = thestatus.Code;
      status.Name = thestatus.Name;
      if (Commit())
        return RedirectToAction("Index");
      return View(status);
    }

… but this will require the MVC runtime to create a Status object for us – in order to do so it will need a parameterless constructor – but ECO does not offer that, well not until now anyway.

The thing is that the sent in object that is created by the MVC runtime is just a convenient data carrier. MVC cannot know much about the environment this object really lives in.

This was the problem for ECO. ECO objects are very capable objects that adhere to numerous enterprise patterns, like  IdentityMap to mention one.

Hmm… What to do….

This is how we solved it – Offline awareness

From today and forward classes generated with our tools can be in an Offline mode. The Offline mode is when an object of a class in your model is not connected to an EcoSpace. It is just data.

Offline objects are created with the parameterless constructor:

    /// <summary>
    /// Creates an Offline object. Can for example be used by MVC runtime to pass values
    /// This is type equivalent to the eco object in everyway - but has none of the eco services
    /// It does however implement INotifyPropertyChanged and INotifyCollectionChanged
    /// </summary>
    [GeneratedCodeAttribute("ECO", "6.0.0.0")]
    public Status() {

This way MVC can create an offline Status object for us. In fact we can use our whole model in a POCO way – set attributes and set associations (that are not maintained the way ECO does, ie both ends consistent all the time).

How do we get an offline object online then? Well we don’t. Offliners are Offliners until destruction. You can however apply data from an Offline object to an Online object (normal eco object, created by constructor with IEcoServiceProvider).

Like this:

          Class1 onlinec1 = new Class1(ecoSpace);
          Eco.Services.EcoOfflineObjectHelper.ApplyValues(c1, onlinec1);

It even recurse for you (follow any associations in the offline world) if you send in a way to resolve offline objects to online objects:

          Eco.Services.EcoOfflineObjectHelper.ApplyValues(c1, onlinec1,ResolveToOnline);

        }
        public IEcoObject ResolveToOnline(IEcoObject offline)
        {
          if (offline is Class2)
            return new Class2(ecoSpace); You might want to look up the correct online objects from data in the offline
          return null;
        }
This entry was posted in MVC. Bookmark the permalink.

3 Responses to Eco for MVC

  1. Alois Schmid says:

    Sounds great! Thank you!

  2. Peter Buchmann says:

    Indeed very, very cool. But..

    We now have classes that don’t live in a EcoSpace and so they are lacking all the EcoServices. It would at least be cool to have a factory like IObjectFactoryService to dynamically create such offline objects.

    Is this already possible or what’s the best way to do this?

  3. Pingback: MVC continued | CapableObjects

Leave a Reply

Your email address will not be published. Required fields are marked *

*