0
votes

We are developing a multisite sitecore solution where each sites can have have their own News as well as able to display the combined news from other Sites.

Problem: Each site have their unique News requirements where 90% of the template fields matches but rest 10% are different.

For example, Site-A has news template with Authors drop down list where Author List are authored on Configuration Node. Where as Site-B has news template where Author is a FREE TEXT Field.

Therefore, when Glass Mapper automatically tries to Map Authors field it fails for Free Text one.

Solution: This can be resolved either by creating a Author as drop down on all Sites but Product owners don't want this.

The other solution is manual mapping of news fields from both sources or use AUTOMAP etc.

Desired Solution: Glassmapper automatically resolves and populates the Author Text Field or Drop Down Field on the fly.

Is above possible?

Thank you.

3

3 Answers

1
votes

I would solve this by "fluent configuration", http://glass.lu/Mapper/Sc/Tutorials/Tutorial8.aspx. Combined with the new Delegate feature added to the Glass Mapper recently. The Delegate feature was originally introduced and described here: http://cardinalcore.co.uk/2014/07/02/controlling-glass-fields-from-your-own-code/

Nuget package for the Delegate feature: https://www.nuget.org/packages/Cardinal.Glass.Extensions.Mapping/

1
votes

You can use Infer types as follows:

public interface IBaseNews
{
    string Author {get; set;}
    //List all other shared fields below
}

[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteA : IBaseNews
{
    [SitecoreField]
    public string Author {get; set;}

    //List all fields which are unique for SiteA

}

[SitecoreType(TemplateId="....", AutoMap = true)]
public class NewsSiteB : IBaseNews
{
    [SitecoreField]
    public string Author {get; set;}

    //List all fields which are unique for SiteB

}

Now, Your code should be:

IBaseNews newsClass = NewsItem.GlassCast<IBaseNews>(true,true);
//You can use Author property now
1
votes

Firstly, I would recommend updating to the latest version of Glass for many other reasons including the delegate feature.

From the infer type example in the comment - you shouldn't use GlassCast, use CreateType(Item item) from the sitecore service / context. If you adopt the version with Delegate in, there is now an official Cast(Item item) on the sitecore service instead.

Also the example there uses a would not solve the difference in type. Delegate would make this very easy. Remember with delegate that there is no lazy loading, this shouldn't matter in this case.

public interface INews
{
    // All my other fields
    string Author { get; set; }
}

The fluent configuration would be something like (to be done in GlassScCustom)

SitecoreType<INews> = new SitecoreType<INews>();
sitecoreType.Delegate(y => y.Author).GetValue(GetAuthor);
fluentConfig.Add(sitecoreType);

private string GetAuthor(SitecoreDataMappingContext arg)
{
    Item item = arg.Item;
    if(item.TemplateID == <templateid>)
    {
        // return the value from the drop link
    }

    return item["Authors"];
}