Was wondering if anyone tried Ditto + Nested Content before and if there's any code example with Custom Controllers for the Nested Content and also the Nested Nested Content mapping and rendering?
The DittoView directive is a new feature in Ditto v0.10, (it came from an idea that Matt developed called DitFlo). The idea behind it is that you no longer need to explicitly call Ditto's .As<T> extension method from any controller code.
So if you have...
@inherits DittoView<MyPageModel>
then Ditto would handle the mapping of the current page's content node. (Previously you would have had to call Model.Content.As<MyPageModel>() somewhere - either in a controller or view code-block)
Then to access your view-model, you can use... @Model.View and if you still need access to the current page's content node, it is still accessible via @Model.Content.
Previously, if all your controller was doing was calling Ditto's .As<T> method, then you no longer need that controller.
If it was doing other code, then you can still use it. The DittoView directive will check if the model passed in to the view matches the target type. If it doesn't then it attempts to convert it. If it's an IPublishedContent, then Ditto knows what to do with it and maps all the properties.
I've got a quick question on the processor. The DittoProcessorAttribute seems straightforward. What I wasn't so sure is the DittoMultiProcessorAttribute.
How does it works? I had a check in the TitleAttribute.cs and it comes with the below
public class TitleAttribute : DittoMultiProcessorAttribute
{
public string TitleAttr { get; set; }
public TitleAttribute()
: base(Enumerable.Empty<DittoProcessorAttribute>())
{
base.Attributes.AddRange(new[] {
new UmbracoPropertyAttribute(TitleAttr),
new AltUmbracoPropertyAttribute("Name")
});
}
}
The bits that confuses me is the
base.Attributes.AddRange(new[] {
new UmbracoPropertyAttribute(TitleAttr),
new AltUmbracoPropertyAttribute("Name")
});
The DittoMultiProcessor is used to group multiple processors together. It's more for if you're adding the same processor attributes to your POCOs properties again and again, then you can group them.
The base.Attributes.AddRange bit is how they are added.
I'm a bit confuse on the TitleAtr being passed in.
the bits like in the below, how do Ditto know that the Umbraco Property Alias is actually called "Title" but the parameter which has been passed in is TitleAtr
Does the DittoMultiProcessorAttribute works that it looks for the Title, if it doesn't exist then it looks for the Alternative which will be Name
To be honest, I'm not familiar with the TitleAttribute code - Matt dev'd it for his demo. (I haven't looked at it in any great detail) I think it was to show an example of how DittoMultiProcessor could be used.
The processors that are defined within a DittoMultiProcessor implementation, are like a pipeline, they'll run sequentially - passing the value of one processor to the next one.
In the case of TitleAttribute, the TitleAttr property would be passed in like so...
[Title(TitleAttr = "yourTitleAlias")]
public string MyTitle { get; set; }
then it can work with the alias. But if you don't pass an alias in, then Ditto will assume the alias is the same as your POCO property name.
I don't use the Lazy Loading feature much myself. It's there for when you want to delay Ditto mapping the property value until its runtime.
An example would be that you have a news section with hundreds of child nodes, this might be an expensive task mapping in advance, so you can lazy load them for when they are needed.
To use this, make your property as virtual...
public virtual IEnumerable<MyModel> NewsItems
then Ditto will do some dark-magic around it and make the property as a Lazy<IEnumerable<MyModel>>.
In the next version of Ditto (v0.11 under development), you'll also need to add a processor to the property called [DittoLazy], (there's a long discussion about it on our GitHub repo, issue #191).
The DittoCache is another performance feature... if you have a custom processor that is doing an expensive thing - e.g. database query or HTTP request. Then you can cache the result.
I know we need to clear up the documentation on these things. Our roadmap is to make v0.11 stable, work on the docs and release as a v1.0.
Just to note, we're always open to contributions, especially around any dev notes or documentation.
No, it doesn't automatically clear the cache for you.
You can set the CacheDuration (in seconds)... or you can figure out the cache key and expire it manually. Here's the code that builds up the cache key.
When i'm within a Surface Controller, I've got the below which render the partial view.
I've got @inherits DittoView
I've just noticed in the Surface controller there's a squiggle on the model and it's showing the below when i hover over. Any idea what's that? Or should i just ignore it?
Not sure, I haven't seen that warning squiggle before, but I've also not seen entering the full path of the partial-view file before either. Interesting how the intellisense knows about the model-type of the partial-view.
I don't have any resolution to offer, so ignoring it is my only advice :-(
Ditto + Nested Content
Hi All,
Was wondering if anyone tried Ditto + Nested Content before and if there's any code example with Custom Controllers for the Nested Content and also the Nested Nested Content mapping and rendering?
Thanks
Hi JLon,
Yep it works fine. Nested Content returns a collection of
IPublishedContent
items, which then can be passed to Ditto for mapping.(This was done by design, as Matt & I developed both packages)
A code example would be...
Then
myModel
would contain be anIEnumerable<MyViewModel>
.If you have a view-model for your page, then you can skip this manual step and let Ditto handle it, like so...
then in your controller, you would only map the content node...
I hope this makes sense?
Cheers,
- Lee
Thanks Lee,
Will check it out tonight. Starting out with Ditto to trying to figure things out
Insider tip... Matt "dittoified" the Faneo starter kit at CodeCabin16, here's the zip link: https://dl.dropboxusercontent.com/u/138129/Host/DittoDemo.zip
:D thanks Lee. Massive help
Hey Lee,
I've noticed that you use DittoView which implement from the UmbracoViewPage.
Is there any extra functionality that can be done when you use the DittoView instead of the UmbracoViewPage?
Thanks
The
DittoView
directive is a new feature in Ditto v0.10, (it came from an idea that Matt developed called DitFlo). The idea behind it is that you no longer need to explicitly call Ditto's.As<T>
extension method from any controller code.So if you have...
then Ditto would handle the mapping of the current page's content node. (Previously you would have had to call
Model.Content.As<MyPageModel>()
somewhere - either in a controller or view code-block)Then to access your view-model, you can use...
@Model.View
and if you still need access to the current page's content node, it is still accessible via@Model.Content
.I hope this helps?
Cheers,
- Lee
ic. Even lesser codes then. Nice.
How will your controller looks like? I had a check in the UmbTextPageController which still comes with the .As<> so it confuses me.
Thanks Lee
Previously, if all your controller was doing was calling Ditto's
.As<T>
method, then you no longer need that controller.If it was doing other code, then you can still use it. The
DittoView
directive will check if the model passed in to the view matches the target type. If it doesn't then it attempts to convert it. If it's anIPublishedContent
, then Ditto knows what to do with it and maps all the properties.There's a lot going on with it, and it should handle most scenarios... but if you're curious about what it's doing exactly, the source-code is here:
https://github.com/leekelleher/umbraco-ditto/blob/develop/src/Our.Umbraco.Ditto/Web/Mvc/DittoView.cs#L27
Cheers,
- Lee
Nice one Lee, always helpful. I'll check it out tonight once i got home. Thanks for all the help.
Hey Lee,
Everything seems to be working nicely.
I've got a quick question on the processor. The DittoProcessorAttribute seems straightforward. What I wasn't so sure is the DittoMultiProcessorAttribute.
How does it works? I had a check in the TitleAttribute.cs and it comes with the below
The bits that confuses me is the
base.Attributes.AddRange(new[] { new UmbracoPropertyAttribute(TitleAttr), new AltUmbracoPropertyAttribute("Name") });
The
DittoMultiProcessor
is used to group multiple processors together. It's more for if you're adding the same processor attributes to your POCOs properties again and again, then you can group them.The
base.Attributes.AddRange
bit is how they are added.Does that make sense, or need more explanation?
Cheers,
- Lee
I'm a bit confuse on the TitleAtr being passed in.
the bits like in the below, how do Ditto know that the Umbraco Property Alias is actually called "Title" but the parameter which has been passed in is TitleAtr
Does the DittoMultiProcessorAttribute works that it looks for the Title, if it doesn't exist then it looks for the Alternative which will be Name
To be honest, I'm not familiar with the
TitleAttribute
code - Matt dev'd it for his demo. (I haven't looked at it in any great detail) I think it was to show an example of howDittoMultiProcessor
could be used.The processors that are defined within a
DittoMultiProcessor
implementation, are like a pipeline, they'll run sequentially - passing the value of one processor to the next one.In the case of
TitleAttribute
, theTitleAttr
property would be passed in like so...then it can work with the alias. But if you don't pass an alias in, then Ditto will assume the alias is the same as your POCO property name.
Cheers,
- Lee
ic. much clearer now. That's how you pass parameter in.
Thanks Lee
You made my day Lee. The package is awesome. It works so well with Nested Content (and also Nested Nested Content). Amazing work :)
I've got a bit of a question on the Lazy Loading and also the Ditto Cache.
How/ When do you use the Lazy Loading and the DittoCache attribute?
I don't use the Lazy Loading feature much myself. It's there for when you want to delay Ditto mapping the property value until its runtime.
An example would be that you have a news section with hundreds of child nodes, this might be an expensive task mapping in advance, so you can lazy load them for when they are needed.
To use this, make your property as
virtual
...then Ditto will do some dark-magic around it and make the property as a
Lazy<IEnumerable<MyModel>>
.In the next version of Ditto (v0.11 under development), you'll also need to add a processor to the property called
[DittoLazy]
, (there's a long discussion about it on our GitHub repo, issue #191).The
DittoCache
is another performance feature... if you have a custom processor that is doing an expensive thing - e.g. database query or HTTP request. Then you can cache the result.I know we need to clear up the documentation on these things. Our roadmap is to make v0.11 stable, work on the docs and release as a v1.0.
Just to note, we're always open to contributions, especially around any dev notes or documentation.
Cheers,
- Lee
Ic. Nice one Lee.
Do the DittoCache hooked into the Umbraco event to clear it out? As in on Publish, Save, Delete, etc?
No, it doesn't automatically clear the cache for you.
You can set the
CacheDuration
(in seconds)... or you can figure out the cache key and expire it manually. Here's the code that builds up the cache key.Cheers,
- Lee
Ic. Thanks Lee
Hey Lee,
Quick question,
When i'm within a Surface Controller, I've got the below which render the partial view.
I've got @inherits DittoView
I've just noticed in the Surface controller there's a squiggle on the model and it's showing the below when i hover over. Any idea what's that? Or should i just ignore it?
Thanks
Hey JLon,
Not sure, I haven't seen that warning squiggle before, but I've also not seen entering the full path of the partial-view file before either. Interesting how the intellisense knows about the model-type of the partial-view.
I don't have any resolution to offer, so ignoring it is my only advice :-(
Cheers,
- Lee
Okie dokie. Thanks Lee.
Just found out, it might have been my Resharper freaking out there I guess
is working on a reply...