Comparing Sitefinity 3.x to 4 (Beta): Fluent API

Comparing Sitefinity 3.x to 4 (Beta): Fluent API

November 03, 2010 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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.

Compatibility

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.

Facades

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!

Code Examples

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
            fluent
                .BlogPost()
                .CreateNew()
                .Do(p =>
                {
                    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);
                })
                .Publish();
        }

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[0]).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
    blogManager.Content.SaveContent(postContent);
}  

Wrap Up

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.

progress-logo

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar