An introduction

By Matt Brailsford / @mattbrailsford

 

 

The Outfield

An introduction

By Matt Brailsford / @mattbrailsford

Same same but different

By Lee Kelleher / @leekelleher

The Outfield Umbrella Inc

About Me

Code Cabin

#codecabin16

20 developers, 3 nights, 1 cabin

Sept 2nd - 5th 2016

What is Ditto?

Ditto is...

...an IPublishedContent model mapper for Umbraco

Why use Ditto?

Because...

  • Strongly types your models/views
  • Separates logic from views
  • Encapsulates logic in reusable blocks
  • A more granular/flexible approach

How is this different from Models Builder?

Models Builder = Domain Models

Ditto = View Models

The Model Builders Way

The Ditto Way

Creating Strongly Typed Models
With Ditto *

* based on the upcoming v0.9 release

Simple Model

MyModel.cs

public class MyModel { 
    public string Name { get; set; }
    public string Url { get; set; }
}
					
MyView.cshtml / MyController.cs

var myModel = Model.Content.As<MyModel>();
					

Attributed Model

MyModel.cs

[UmbracoProperties(Recursive = true)]
public class MyModel { 

    [UmbracoProperty("heading", "pageName")]
    public string Name { get; set; }

    public string Url { get; set; }

    [DittoIgnore]
    public string Ignore { get; set; }
}
					
MyMView.cshtml / MyController.cs

var myModel = Model.Content.As<MyModel>();
					

Custom Attributes

HelloAttribute.cs

public class HelloAttribute : DittoProcessorAttribute {
    public string Name { get; set; }
    public override object ProcessValue() {
    	return "Hello " + Name; // Could be any type, from anywhere
    }
}
					
MyModel.cs

public class MyModel { 
    [Hello(Name = "Matt")]
    public string MyString { get; set; }
}
					
MyView.cshtml / MyController.cs

var myModel = Model.Content.As<MyModel>();
					

Attribute Contexts

PagedContext.cs

public class PagedContext : DittoProcessorContext {
        public int Page { get; set; }
        public int PageSize { get; set; }
}
					
NewsAttribute.cs

[DittoProcessorMetaData(ValueType = typeof(object), 
    ContextType = typeof(PagedContext))]
public class NewsAttribute : DittoProcessorAttribute {
    public override object ProcessValue() {
    	var ctx = Context as PagedContext;
    	return NewsService.GetLatestNews(ctx.Page, ctx.PageSize);
    }
}
					
MyModel.cs

public class MyModel { 
    [News]
    public IEnumerable<NewsItem> LatestNews { get; set; }
}
					
MyView.cshtml / MyController.cs

var ctx = new PagedContext { Page = 1, PageSize = 10 };
var myModel = Model.Content.As<MyModel>(processorContexts:new []{ ctx });
					

Chained Attributes

MyModel.cs

public class MyModel { 
    [UmbracoProperty("heading", "pageName", Order = 0)]
    [Uppercase(Order = 1)]
    public string Name { get; set; }
}
					
MyView.cshtml / MyController.cs

var myModel = Model.Content.As<MyModel>();
					

Caching

MyModel.cs

public class MyModel { 
    [DittoCache(CacheDuration = 300)]
    [MyComplexData]
    public IEnumerable<MyComplexData> Data { get; set; }
}
					
MyView.cshtml / MyController.cs

var myModel = Model.Content.As<MyModel>();
foreach(var item in myModel.Data){
	...
}
					

Creating Strongly Typed Views
With Ditto *

* based on the upcoming v0.10 release

Simple View

MyModel.cs

public class MyModel { 
    public string Name { get; set; }
    public string Url { get; set; }
}
					
MyView.cshtml

@inherits DittoView<MyModel>

@Model.View.Name

Contextual View

NewsPageController.cs

public class NewsPageController : DittoController  {
    public override ActionResult Index(RenderModel model)
    {
        // Construct search context
        var ctx = new PagedContext
        {
            Page = int.Parse("0" + Request.QueryString["p"]),
            PageSize = int.Parse("0" + Request.QueryString["ps"])
        };

        // Register the context
        RegisterProcessorContext(ctx);

        // Continue to the page
        return CurrentView(model);
    }
}
					
NewsPage.cshtml

@inherits DittoView<MyModel>
@foreach(var newsItem in Model.View.LatestNews){
	...
}
					

Useful Links

Questions?

The End

By Matt Brailsford / @mattbrailsford

By Lee Kelleher / @leekelleher

The Outfield Umbrella Inc