umco / umbraco-ditto

Ditto - the friendly view-model mapper for Umbraco
http://our.umbraco.org/projects/developer-tools/ditto
MIT License
79 stars 33 forks source link

DittoView: Enhancement to support unwrapping & downcasting view-models #219

Closed leekelleher closed 7 years ago

leekelleher commented 7 years ago

@mattbrailsford - I'd found an scenario a while back where if a DittoView<T> page referenced another DittoView<T> page, but the view-model was castable, the code wouldn't cast it and Ditto would recreate the object.

That might sound like a mouthful, but I'll try to explain...

Say you've got these POCOs...

public class MasterModel
{
  public string Something { get; set; }
}

public class OtherModel : MasterModel
{
  public string Whatever { get; set; }
}

If you had DittoView<OtherModel> on a page template, and DittoView<MasterModel> on the layout template, the DittoView code would be creating the object twice, rather than downcasting it to the inherited type.

This PR adds in a check to see if the model object is a DittoViewModel<> and to unwrap the inner view-model, so it can be reused.

Hopefully this makes sense? (and is backwards compatible / a non-breaking change)

leekelleher commented 7 years ago

@mattbrailsford - re: unit-test - I couldn't figure out how to unit-test the DittoView class - guessing you hit the same problem (since there aren't any existing tests for it)?

leekelleher commented 7 years ago

Merging in. We can look at the unit-test later. Thanks for the quick turnaround @mattbrailsford #h5yr

mattbrailsford commented 7 years ago

Yea, that's when it starts to get confusing to me as your start bringing in web contexts etc