Fluent API Example

Posted by Community Admin on 03-Aug-2018 19:02

Fluent API Example

All Replies

Posted by Community Admin on 29-Jul-2010 00:00
Can someone post some fluent api examples from the webinar for me?  Specifically the one that Ivan showed where a blog is created and then post are added to it within the same scope.  There was a piece of code that added a description to the blog after it had been created and a child post was created.. We are curious to see this code again...


Thanks
Posted by Community Admin on 29-Jul-2010 00:00
Hi Drew Greenwell,

Below is a sample code. I believe that the webinar will be available on tv.telerik.com or/and sitefinitywatch.com so you will be able to take a look at it again.

using Telerik.Sitefinity;
using Telerik.Sitefinity.GenericContent.Model;
..
     ...
            
    var BlogId = Guid.Empty;
    App.WorkWith()
        .Blog()
        .CreateNew()
        .Do(b =>
            
                b.Title = "NewBlog";
                b.Description = "My 4.0 Blog";
                BlogId = b.Id;
            )
        .SaveChanges();


You can retrieve the blog like this

myBlog = App.WorkWith().Blogs().Where(b => b.Title == "NewBlog").Get().First();

For creating content items you will be able to use App.WorkWith().Blog(NewBlog) .BlogPost().CreateNew().Do( b  => b.Owner = myBlog.Id) and finally commit the changes.

We will provide more documentation and samples with the BETA.

Kind regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 29-Jul-2010 00:00
Thanks for the response Ivan. I was wondering about the code where you created a blog then created posts for it all in one statement..

it was something like

App.WorksWith()
    .Blog()
    .CreateNew()
    .Do(b=>
        b.Title = "A new Blog";
        )          
            .BlogPost().CreateNew()
            .Do(bp => bp.Title = "A new Blog Post";)
            .Done()
     //b shouldnt exist here so I think Im missing a step you took to reaccess the blog object you created
    .Do(b.Description = "Oops forgot to add a description")
    .SaveChanges();


Thanks again for the response and thanks for the outstanding effort you guys are putting in
Posted by Community Admin on 30-Jul-2010 00:00
Hi Drew Greenwell,

Yes, some minor corrections are needed:

App.WorksWith()
    .Blog()
    .CreateNew()
    .Do(b =>
        b.Title = "A new Blog";
        )         
            .BlogPost()
              .CreateNew()
              .Do(bp => bp.Title = "A new Blog Post";)
              .Done()
    .Do( b => b.Description = "Oops forgot to add a description")
    .SaveChanges();

Check the highlighted. Everytime you use Do(), you are able to access the content item's properties behind the current facade (in the highlighted case, Blog() facade.

Sincerely yours,
Georgi
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 30-Jul-2010 00:00
I figured that was it, Thanks!
This thread is closed