Sitefinity 4 Generic APIs vs Fluent APIs

I just released a new video demonstrating the power of the new APIs in Sitefinity 4.0 Beta that was just released last week by the Telerik team (CONGRATULATIONS! to the entire Telerik team worldwide).

The essence of the video was to demonstrate the use of the regular APIs to create a new PAGE into the system using two different approaches:

1- The Regular API way that most core developers will use that require understanding of the architecture and the fundamental knowledge of how the Object hierarchy relates to the database layer behind Sitefinity 4.0

2- The FLUENT API way that does not require the knowledge from the previous point and was made for designer and developers to be able to accomplish jobs into the Sitefinity system really quickly working on Objects in an atomic way to accomplish a task.

 

Both ways have their uses and make Sitefinity 4 extremely powerful in bringing the level of integration into the system very easy and powerful.

I see the resemblance to the old days when we used VBA to write macros against Word, Excel, Powerpoint, etc… A lot of people did not really care about learning about COM, Type Libraries, IUnknown, IDispatch, etc… they just want to Create an Excel document, write some cells to it and save it.  VBA allowed for that very easily by hiding the complexity of the system behind the scene.  For some others that was not enough, they wanted to attach to a specific instance of Excel, embed their own ActiveX Control into the spreadsheet and communicate both ways with their internal system, for those the COM layer was available and ready to program against as long as you understood the relationships between the Application, Workbook, Worksheet, Range, Cell, etc…

Same thing here in Sitefinity 4, and I believe it is a very welcome addition to that excellent platform that will bridge the gap between IT professionals and Developers to get tasks accomplished quickly in Sitefinity.

 

First, the code I used for the Generic API to create a new Page is here below based on a Button click event on a test page I created:

 

protected void Button1_Click(object sender, EventArgs e)
{
  PageManager manager = PageManager.GetManager();
 
  PageData pagedata = manager.CreatePageData();
  pagedata.Template = manager.GetTemplates().First();
  pagedata.HtmlTitle = "Lino testing page";
 
  PageNode node = manager.CreatePageNode();
  node.Title = "Lino Test Page";
  node.ShowInNavigation = true;
  node.UrlName = "LinoPage1";
 
  var parent = manager.GetPageNode(SiteInitializer.FrontendRootNodeId);
  manager.ChangeParent(node, parent);
 
  node.Page = pagedata;
 
  manager.SaveChanges();
 
  SiteMapBase.Cache.Flush(); // This is needed only for the Beta
}

The main points as you will see in the video is to learn the relationship between PageManager, PageData and PageNode objects and how the system brings them all together to communicate with the OpenAccess ORM layer to place the new created page in its correct place in the database with all its corresponding data.

 

Finally, the code for the alternative method using FLUENT APIs is found below to accomplish the same thing:

 

protected void Button2_Click(object sender, EventArgs e)
{
  PageManager pmanager = new PageManager();
  Guid tempGuid = pmanager.GetTemplates().First().Id;
 
  App.WorkWith()
     .Page()
     .CreateNewStandardPage(PageLocation.Frontend)
     .Do(p =>
        {
           p.Name = "Lino Page 2";
           p.Title = "Lino Second Page";
           p.ShowInNavigation = true;
           p.UrlName = "Lino2Page";
         })
     .SetTemplateTo(tempGuid)
     .SaveChanges();
 
     SiteMapBase.Cache.Flush();  // This is needed only for the Beta
 
}

Notice that the entire instruction occurred in one line using the “App” object that allows you to choose a preferred Object to “workwith” and then focus on that Object to accomplish whatever task specific to that object without worrying about what the OpenAccess ORM layer needs or the other objects in the hierarchy require to make it work.

 

Hope this gives you an entry into the power of using the Sitefinity 4.0 APIs and Fluent APIs and I would be interested in your opinion and recommendation for other videos and blogs to make Sitefinity 4.0 Development easier for everyone.

comments powered by Disqus