Facades

A facade is a design pattern that simplifies the complex details of a system. For example, a facade can make a class library easier to use by providing a simplified interface for it. A facade would usually consist of all the library classes.

Since the Fluent API is another layer of abstraction, or a wrapper of the basic API introduced in Sitefinity CMS, there are different facades that can be used in the context of the Fluent API to make the developers' work easier.

In the following example, the Blog() statement acts as a facade to everything related with the Blogcontent type. It gives options for creating, deleting, or updating a blog in Sitefinity:

C#
using Telerik.Sitefinity;

namespace SitefinityWebApp
{
    public class CreateBlogFluentAPIExample
    {
        public void EntryPointMethods()
        {
            //create a blog with the default blogs provider

            var blog = App.WorkWith().Blog().CreateNew().SaveChanges();
        }
    }
}

There are other facades that give access to other system parts and content types - facades for news, pages, taxonomies, etc.

Following is a sample list of facades:

  • Album used to work with an image library - AlbumFacade
  • Albums used to work with a set of image libraries - AlbumsFacade
  • Blog used to work with a blog - BlogFacade
  • Blogs used to work with a set of blogs - BlogsFacade
  • BlogPostused to work with a blog post - BlogPostFacade
  • BlogPostsused to work posts in any of the blogs - BlogPostsFacade
  • ContentItemused to work with a generic content item - ContentItemFacade
  • ContentItemsused to work with a set of generic content items - ContentItemsFacade
  • Documentused to work with a document - DocumentFacade
  • Documentsused to work with a set of documents - DocumentsFacade
  • DocumentLibraryused to work with a document library - DocumentLibraryFacade
  • DocumentLibrariesused to work with a set of document libraries - DocumentLibrariesFacade
  • Eventused to work with an event - EventFacade
  • Eventsused to work with a set of event items - EventsFacade
  • Imageused to work with an image - ImageFacade
  • Imagesused to work with a set of images - ImagesFacade
  • Pageused to work with a page - PageFacade
  • Pagesused to work with a set of pages - PagesFacade
  • NewsItemused to work with a news item - NewsItemFacade
  • NewsItemsused to work with set of news items - NewsItemsFacade
  • Videoused to work with a video - VideoFacade
  • Videosused to work with a set of videos - VideosFacade

Each of this facades provide methods for creating, updating, deleting, and getting items for further processing.

For more information about how to start using the App class, how to choose a provider to work with, and how to select the content type or module you want to work with, see For developers: Entry point and methods.

Child Facades

In some cases, when you work with a given facade, you need to access another one. This is usually the case when you work with parent and child objects, such as a page and a widget, a content item and a comment, a taxonomy and a taxon.

In the following code example there are both page and widget facades. It creates a page with name My Test Pageand adds two widget in it. After this, it creates another page and finally it executes all the actions in a single transaction via the SaveChanges() method:

C#
using System;
using System.Web.UI.WebControls;
using Telerik.Sitefinity;

namespace SitefinityWebApp
{
    public partial class ChildFacadesFluentAPI : System.Web.UI.Page
    {
        protected void SomeButton_Click(object sender, EventArgs e)
        {
            App.WorkWith()
               .Page()
               .CreateNewStandardPage() // returns standard page facade

                .Do(pn =>
                {
                    pn.Title = "My Test Page";
                })

               .CheckOut()    // creates a draft vresion and locks the page
                 .Control()   //child facade
                  .CreateNew(new Label() { Text = "My Test Label" }, "Content")
                  .Done()

                 .Control()    //child facade 
                  .CreateNew(new Label() { Text = "My Test Label" }, "Content")
                  .Done()

            .Publish()
            .SaveChanges();
        }
    }
}

NOTE: The Done() method is returning the parent facade, which is the page facade. This allows you to continue the work with the parent object, after you have finished with the child one. The Done() method is returning the parent facade object, and is called in child facades.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?