Skip to main content

Experience Management

Umbraco Where GlassMapper == null

What happened? One of the best assists for a CMS is now unavailable for Umbraco, and just before we got the GridEditor, so now everything is more complicated! There is an answer, pick up all those dreams of pretty grids and components and take a look at factories.
Factories take the place of GlassMapper and allow you to get your content and define it for your project. Which is great when your content editor calls images ‘Pretty Pictures’ and links ‘blue underlines’. Here is the general idea, you send your Umbraco object to the factory and break it up into defined variables that your project understands. You then send those variables to your view as a new model and content appears on your site. Magical, I know.
Here’s a step-by-step look at how to implement factories.
Step 1. Create an Overlord (Controller)
If it’s for a page, content module, or Grid Editor, the controller is looking for this Umbraco object and is going to tell it what to do and where to go. Inside your namespace you will have these elements:

public class ObjectNameController : RenderMvcController
    {
        // Object is received as a RenderModel
        public override ActionResult Index(RenderModel model)
        {
          // Create an empty object to populate
            Page page = new Page();
            // Create your factory object
            PageFactory factory = new PageFactory();
      // Send the RenderModel.Content to the factory and assign it
            page = factory.FactoryMethod(model.Content);
            // Send this object to whatever view needs it
            return View("~/Views/HomePage/Page.cshtml", model);
        }
    }

Step 2. Create your Factory

public class PageFactory : ModuleFactory<Page>
    {
       public override Page FactoryMethod(IPublishedContent content)
       {
    //Create your object
          Page page = new Page();
    //ID exists in all Umbraco Objects
    page.Id = content.Id;
    //Reference the variable inside the Model Object
          page.Image = content.GetProperty("Image");
    //Get the property inside the content from Umbraco
          page.Title = content.GetProperty("Title");
    //The property name is the Alias from Umbraco
          page.Date = content.GetProperty("Date");
    //I need to send some of my content to a different factory
    ConcreteM1Factory M1Fac = new ConcreteM1Factory();
          page.M1 = M1Fac.FactoryMethod(content);
          return page;
        }
    }

There are many elements that all Umbraco Elements will have; ID, DocumentType and Name that you can assign to a variable inside your object without defining it explicitly in Umbraco. These variables are listed and defined in Umbraco.Core.Models.IPublishedContent, you will need to include that Using.
This is a perfect place to write in any logic for your content. Create a list of all the object’s children, or take several properties to create a unique URL.
Any content objects you created in Umbraco need their own factory. You can send content to a factory while inside another factory, there is no limit to the nesting.
This is then sent back to your Overlord as an offering, hoping it did everything right and that this won’t break the page by assigning the ID to a negative number.
Step 3. Use it for Something
Once your controller has the new object you can send it to any view, which will accept the model, and use its variables.
But I want the cool new toy! GridEditor
You can actually get what you want. In the new versions of Umbraco we now have the GridEditor, which lets you put content anywhere. You create components and page templates the same way but now you add layouts for your page templates. The steps we followed for controllers and factories are the same for the grid editor with two major exceptions:

  • Pages and GridEditors both get controllers
  • The GridEditor model from Umbraco is called LeBlenderModel
public class GridController : UmbracoBaseGridEditorController
    {
        // You get a LeBlenderModel now
        public ActionResult Index(LeBlenderModel model)
        {
            Grid grid = new Grid();            
            if(model != null)
          GridFactory factory = new GridFactory();
                grid = factory.FactoryMethod(model);
      }
            return View(grid);
        }
    }

Since the page can have any content in any configuration you now must have an Overlord for everything separately. Each controller will send the content to a factory then to a separate view.
Before, when we could tell the page exactly how to look, we would render all of the content in one specific way. Now we need to let 15 image sliders exist on one page and to do that, we have a separate controller, factory, and view for everything.
 

Tags

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Nicole Baker

More from this Author

Follow Us