简体   繁体   中英

C# Design pattern to merge data objects

I have bunch of data objects that I populate after getting data from a feed. The feed is unreliable and sometimes drops data. I need to merge the incoming data with the data that I already have. I am unable to figure out an easily extendable and scalable pattern for that.

For example, my datamodel has the following field

DataModelExample
{
    public string Name;
    public string Value;
    public string Extension;
}

If the feed drops the field Value, it is okay for me to pick data from an existing data object from cache and merge the two. I have a number of data objects with varying number of fields where this needs to be done.

Any ideas?

One possible way:

You could change all your property definitions so they are all nullable .

For instance, if you have a public int MyInt { get; set; } public int MyInt { get; set; } public int MyInt { get; set; } property, change it to public int? MyInt { get; set; } public int? MyInt { get; set; }

Then, after your object has been populated from your feed, you could iterate over all your properties using reflection (see How to loop through all the properties of a class? ) and for each property, if the value is null (which means the feed drops the property), set it with a value that comes from your cache.

In lieu of reflection, which @ken2k suggested, you could create a group of classes that process your objects and merges the data from the cache if necessary. To minimize the number of such classes that you would need, I'd have them operate on an interface, which your data objects would implement.

public interface IDataModelExample
{
    string Name { get; set; }
    string Value { get; set; }
    string Extension { get; set; }
}

public class DataModelExampleMerger
{
    public IDataModelExample Merge(IDataModelExample dme)
    {
        var cachedDme = LoadFromCache(); // This would require the key of course.
                                         // I'll leave the implementation up to 
                                         // you.

        if (string.IsNullOrEmpty(dme.Name))
        {
            dme.Name = cachedDme.Name;
        }

        // Repeat similar if-block for all properties.

        return dme;
    }
}

You would need to write one merger class per data object interface, so this solution would require considerably more code than a reflection-based solution. I don't think you can get around the work. It's a trade-off: runtime performance versus overall coding time. Honestly, I would take the runtime performance hit unless your users start complaining, but it's up to you.

Remember, premature optimization is the root of all evil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM