The series Comparing Sitefinity 3.x to 4 Beta continues with a quick look a the new Fluent API. This is an incredible new addition to the Sitefinity platform, opening an entire new world for developers to accomplish new and exciting tasks and projects.
Existing Closer Looks
I had hoped to put together a detailed code walkthrough with tons of examples. However, it turns out a lot if this is already out there! The second half of the Telerik webinar on Sitefinity 4.0 gives a great introduction to the Fluent API by Ivan Osmak, including a few live examples of it in action.
In addition, Falafel Software has also put together not only blog post comparing the Sitefinity APIs, but a companion video as well.
Finally, there is an excellent Fluent API Overview in the Sitefinity 4 Beta Documentation. This documentation includes several complete code examples detailing everything from creating pages to creating your own custom data types (more on this later).
If you're interested in learning more about the Fluent model, take a look at the Fluent Interface and the related Facade Pattern articles on Wikipedia.
Before we continue it's important to point out that the Fluent API is NOT replacing the existing 3.x pattern of using Content Managers. The Content Managers are still available for use, so you can continue to leverage your existing skills from 3.x for accessing, importing, or managing Sitefinity content.
However, the Fluent API IS likely to offer developers an improved experience by simplifying and abstracting away a lot of the boilerplate code needed to develop with the previous model of Content Managers. Many of the tasks involved with managing Sitefinity content will be simplified greatly by using the Fluent API, as we'll see in a quick example later on.
Even if you encounter a custom, complex scenario that isn't yet natively supported in the Fluent API, the Content Managers are still available to you for complete control over your code. This is, once again, another fine example of how Sitefinity continues to combine new features without sacrificing compatibility and flexibility.
Another benefit of this API is the use of Facades. Previously, when developing Sitefinity addons or plugins, you had to program directly against a versioned DLL that corresponded to your current Sitefinity version. However with the new Facade pattern in Sitefinity 4, you now develop with a version-agnostic facade. This means you should no longer require multiple projects to support different versions of Sitefinity; they all support the same facades!
This article wouldn't be complete without at least a quick example comparing the Fluent API to the Content Managers. Here is an example taken from the Sitefinity 4 documentation on creating a blog post:
using (var fluent = App.WorkWith())
Blog blog = fluent.Blogs().Where(b => b.Title == "Sitefinity developers blog").Get().First();
//you should have a blog with that name
//so we can create a post inside
p.Parent = blog;
p.Title = "Blog post in MyBlog";
p.Content = "Here goes my content";
p.PublicationDate = DateTime.Now;
p.ExpirationDate = DateTime.Now.AddDays(3);
As you can see, the code is very straightforward and almost self-documenting. You retrieve the Blogs facade from an instance of the fluent interface, then use chained methods to create and publish a new post.
Comparing it to how it would be done with a Blog Content Manager, you can see that, although they basically perform the same actions, the fluent interface is much more natural:
// create new instance of BlogManager
Telerik.Blogs.BlogManager blogManager = new Telerik.Blogs.BlogManager();
// get all blogs
IList listOfAllBlogs = blogManager.GetBlogs();
if (listOfAllBlogs.Count > 0)
// get the first blog item
Telerik.Blogs.IBlog firstBlog = blogManager.GetBlog(((Telerik.Blogs.IBlog)listOfAllBlogs).ID);
// create a blog post by calling the CreateContent method of the
// ContentManager class through the BlogManager class
Telerik.Cms.Engine.IContent postContent = blogManager.Content.CreateContent("text/html");
// set the parent of the post item to be firstBlog
postContent.ParentID = firstBlog.ID;
// save the Content property value and the Title meta key
postContent.Content = "Here goes my content";
postContent.SetMetaData("Title", "Blog post in MyBlog");
//save the Content item through the BlogManager
The Fluent API is an excellent new feature that I'm confident all current Sitefinity developers will appreciate. Developing against facades ensures that developers can focus on functionality, ensured that compatibility will automatically be provided by the Framework.
Ultimately, as always, Sitefinity still gives you the choice to decide which format works better for you and your development needs.