0
votes

I'm 'conjoining' Umbraco with another web application and using Elasticsearch to store data from both applications in one index. So far, I've been only searching through Umbraco content, getting its ID and then using UmbracoHelper to retrieve the proper IPublishedContent item, which then I mapped to a strong-typed object [Class1], which inherits from PublishedContentModel abstract class, using Ditto.

Currently in the Umbraco application, the Class1 object is being serialized with JSON.Net and output and I can't change the structure of it. With addition of the items coming from another, non-Umbraco application I wanted to completely omit the step of retrieving the content from Umbraco and instead hold all of the relevant data directly in the index. That way I could just create strong typed objects based on the content of the index. However, after remapping the item to it's DTO [Class1DTO], I found myself unable to map those onto the Class1.

Obviously, I cannot use Ditto anymore, as it only works when mapping from IPublishedContent and it's derivatives. I was thinking about using AutoMapper, but the problem is, I cannot instantiate my Class1, without passing IPublishedContent object (because of the need to implement the PublishedContentModel constructor).

I know I could duplicate all of my strong-typed objects without the PublishedContentModel inheritance, but it feels really wrong doing that. Is there a way to somehow fake the IPublishedContent object, so that I could use AutoMapper? Or is there any other way of achieving the same output?

Example classes below:


[JsonObject(MemberSerialization.OptIn)]
public class Class1 : PublishedContentModel
{
    public Class1(IPublishedContent content)
        : base(content)
    {
    }

    [JsonProperty]
    public string type { get; set; }

    [JsonProperty]
    public override int Id { get { return base.Id; } }

    [JsonProperty]
    public override string SomeData { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class Class1DTO
{
    [JsonProperty]
    public string type { get; set; }

    [JsonProperty]
    public int Id { get; set; }

    [JsonProperty]
    public string SomeData { get; set; }

    [JsonProperty]
    public SomeFilter FilterForSearch { get; set; }        
}
1

1 Answers

2
votes

Couldn't you create your own version of the PublishedContentModel that doesn't have a reliance on IPublishedContent? Something like this:

public abstract class Base
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public String Path { get; set; }

    public IList<Int32> PathIds
    {
        get
        {
            return !String.IsNullOrEmpty(Path) ? Path.Split(',').Select(x => Convert.ToInt32(x)).ToList() : null;
        }
    }

    public Int32 ParentId
    {
        get
        {
            if (PathIds.HasItemsAndNotNull() && PathIds.Count >= 2)
                return PathIds[PathIds.Count - 2];
            return -1;
        }
    }

    public Int32 Level { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime UpdateDate { get; set; }
    public String WriterName { get; set; }
    public String DocumentTypeAlias { get; set; }
}

Unless you have a need of very specific fields that are on PublishedContentModel.