Facades
This topic describes what facades and how they relate to the Fluent API
What is Facade?
Facade is a term used in the Software Design Patterns field. A facade is an object that provides a simplified interface to a larger body of code, such as a class library (source: Wikipedia - http://en.wikipedia.org/wiki/Facade_pattern)
Since the Fluent API is another layer of abstraction, or a wrapper of the basic API introduced in Sitefinity, there are different Facades than can be used in the context of the Fluent API to make the developers' work easier. Following the code example from the previos topic - Overview:
using Telerik.Sitefinity
..
var blog = App.WorkWith().Blog().CreateNew().SaveChanges();
..
The
Blog() statement acts as a Facade to everything related with the Blog content type. It gives options for creating, deleting or updating a blog in Sitefinity. There are other Facades, giving access to other parts and content types of the system - facades for News content, Pages, Taxonomies etc. Here is a sample list of facades:
- Album, to work with a Library Album - AlbumFacade
- Albums, to work with Library Albums - AlbumsFacade
- Blog, to work with a Blog - BlogFacade
- Blogs, to work with Sitefinity Blogs - BlogsFacade
- BlogPost, to work with blog post - BlogPostFacade
- BlogPosts, when you want to work with posts in any of the blogs - BlogPostsFacade
- ContentItem, for working with Generic Content type item - ContentItemFacade
- ContentItems, for working with Generic Content items - ContentItemsFacade
- Document, for working with Documents - DocumentFacade
- Documents, to work with a set of Documents - DocumentsFacade
- DocumentLibrary, to work with document library - DocumentLibraryFacade
- DocumentLibraries, to work with a set of document libraries - DocumentLibrariesFacade
- Event, to work with the event content type - EventFacade
- Events, to work with events items - EventsFacade
- Image, to work with with Library Image type - ImageFacade
- Images, to work with Library images - ImagesFacade
- Page, to work with Page object - PageFacade
- Pages, to work with Pages - PagesFacade
- NewsItem, to work with news object - NewsItemFacade
- NewsItems, to work with set of news items objects - NewsItemsFacade
- Video, to work with Library Video item - VideoFacade
- Videos, to work with Library Video items - VideosFacade
Each of this facades provide methods for creating, updating, deleting or getting items for further processing. If you have followed the article
Overview, you should now know 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.
Child Facades
Sometimes when you work with a given facade, you may need to access another one. This is usually the case when you work with Parent and Child objects, like Page and Control, Content item and a Comment, Taxonomy and Taxon.
Consider the following example:
using Telerik.Sitefinity;
public partial class SomeClass : 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();
}
}
In the example above, we are working with both Page and Control facades. Note the Done() method. It is returning the parent facade so in our case this is the Page facade object. This allows us to continue the work with the parent object in case we have finished with the child one.
Let's summarize what the code example is doing - it creates a page with name My Test Page, adds two controls on it. After the controls are added, another page is created and finally all the actions are executed in a single transaction via the SaveChanges() method.
Note |
|
Done() method is returning the parent facade object, and is called in child facades.
|