Benjamin Schaefer
Apr 13, 2017
  3229
(3 votes)

Working with Episerver Social References

Episerver Social is a stand-alone platform that provides unparalleled control over the construction of models used to deliver social content. Social has two ways to uniquely reference content stored within it.

  • An Id that is generated when a model is persisted into the Social system.
  • A Reference, a means of identifying relationships between social content and application entities.

One aspect of building a maintainable social application is ensuring that you apply a consistent scheme for representing the relationships between your application and its social content. References must be capable of uniquely identifying your application's entities, being easily interpreted by your application, and robust enough to support your application's inevitable future changes.

Example use cases for References might include the following:

  • Referencing a third-party platform that Social is interacting with (for example, Episerver, Ektron, SAP, etc.)
  • Referencing a third-party provider that Social is being used with (for example, Commerce, Cms, ERP, etc.)
  • Referencing unique user Ids
  • Referencing unique product/variant Ids

To ensure the consistent construction of a reference a developer might find it advantageous to standardize the way references are created. Here are three guidelines to help you to create references.

1. Build an easily expandable mechanism for handling strings that will be used most often.

//Outline the standard platforms that will be in use with Social.
public static class Platform
{
    public const string Episerver = "Episerver";
    public const string Ektron = "Ektron";
    public const string Salesforce = "Salesforce";
    public const string SAP = "SAP";
    public const string Microsoft = "Microsoft";
}
//Outline the standard providers that exist for your platforms and will be referenced by your builder. 
public static class Provider
{ 
    //Episever and Ektron
    public const string CMS = "CMS";
    public const string Commerce = "Commerce";
 
    //Salesforce
    public const string Demandware = "Demandware";
    public const string SalesCloud = "SalesCloud";
    public const string Pardot = "Pardot";
 
    //SAP
    public const string ERP = "ERP";
    public const string Anywhere = "Anywhere";
 
    //Microsoft
    public const string Sharepoint = "Sharepoint";
    public const string Dynamics = "Dynamics";
} 

2. Build an efficient way to assemble the relevant strings needed for your Social References.

// This class builds up a string from the relevant details of a product in a third party system.
public class ProductReferenceBuilder
{
    public string Platform { get; set; }
    public string Provider { get; set; }
    public string ProductId { get; set; }
    public string Variant { get; set; }

    public ProductReferenceBuilder AddPlatform(string platform)
    {
        Platform = platform;
        return this;
    }
    
public ProductReferenceBuilder AddProvider(string provider)     {         Provider = provider;         return this;     }
public ProductReferenceBuilder AddProductId(string productId)     {         ProductId = productId;         return this;     }
public ProductReferenceBuilder AddVariantId(string variant)     {         Variant = variant;         return this;     }
  public Reference Build()     {         ProductReferenceValidation();         var referenceString = $"Product://{Platform}/{Provider}/{ProductId}/{Variant}";         return Reference.Create(referenceString);     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(ProductId) ||         string.IsNullOrWhiteSpace(Variant))         {             throw new Exception("Missing field value for ProductReference");         }     }
// This class builds up a string from the relevant details of a user in a third-party system. public class UserReferenceBuilder {     public string Platform { get; set; }     public string Provider { get; set; }     public string UserId { get; set; }
public UserReferenceBuilder AddPlatform(string platform)     {         Platform = platform;         return this;     }
  public UserReferenceBuilder AddProvider(string provider)     {         Provider = provider;         return this;     }
public UserReferenceBuilder AddUserId(string userId)     {         UserId = userId;         return this;     }
public Reference Build()     {         ProductReferenceValidation();         var referenceString = $"User://{Platform}/{Provider}/{UserId}";         return Reference.Create(referenceString);     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(UserId))         {             throw new Exception("Missing field value for UserReference");         }     }
private void ProductReferenceValidation()     {         if (string.IsNullOrWhiteSpace(Platform) ||         string.IsNullOrWhiteSpace(Provider) ||         string.IsNullOrWhiteSpace(UserId))         {             throw new Exception("Missing field value for UserReference");         }     } }

3. Add unit tests for your reference related classes to ensure that the creation of a reference matches what is expected.

[TestClass]
public class ProductBuilderTest
{
    [TestMethod]
    public void ProductReferenceBuilder_Build_ReturnsProperlyConstructedReference()
    {
         var expectedProductReference = Reference.Create("Product://Episerver/Commerce/ProductId/VarientId");
         var actualProductReference = new ProductReferenceBuilder().AddPlatform(Platform.Episerver)
                                                                   .AddProvider(Provider.Commerce)
                                                                   .AddProductId("ProductId")
                                                                   .AddVariantId("VarientId")
                                                                   .Build();
        Assert.IsTrue(expectedProductReference == actualProductReference);
    }

 

    [TestMethod]
    public void UserReferenceBuilder_Build_ReturnsProperlyConstructedReference()
    {
        var expectedProductReference = Reference.Create("User://Microsoft/Dynamics/UserId");
        var actualProductReference = new UserReferenceBuilder().AddPlatform(Platform.Microsoft)
                                                               .AddProvider(Provider.Dynamics)
                                                               .AddUserId("UserId")
                                                               .Build();
        Assert.IsTrue(expectedProductReference == actualProductReference);
    }
}

By employing these guidelines, developers can create reusable code to help construct references consistently.
Additional information on Episerver Social Reference can be found at:
http://world.episerver.com/documentation/developer-guides/social/social_platform-overview/discovering-the-platform/#referenceandIDs.
 

Apr 13, 2017

Comments

192.168.l.254 192.168.1.254
192.168.l.254 192.168.1.254 Sep 7, 2018 05:29 PM

192.168.l.254

Please login to comment.
Latest blogs
Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024

Efficient Catalog Metadata Management and Product Updates Using DTOs in Optimizely Commerce

This post explores ways to manage and update catalog metadata in Optimizely Commerce by utilizing Data Transfer Objects (DTOs). DTOs provide a...

Sujit Senapati | Oct 31, 2024

Effortlessly Resize Images with Cloudflare's On-the-Fly Solution

Resizing images in C# has traditionally been a complex and time-consuming task, often requiring intricate code and handling various image processin...

Manoj Kumawat | Oct 31, 2024 | Syndicated blog

XSS Vulnerabilities Patched with TinyMCE 6.8.4

Two different XSS vulnerabilities were fixed in the latest update of the NuGet package EPiServer.CMS.TinyMce. Update today!

Tomas Hensrud Gulla | Oct 30, 2024 | Syndicated blog