Click here to Skip to main content
15,903,012 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: I want to develop any website Pin
Ankur\m/19-Jun-10 0:57
professionalAnkur\m/19-Jun-10 0:57 
AnswerRe: I want to develop any website Pin
Peace ON21-Jun-10 1:03
Peace ON21-Jun-10 1:03 
QuestionDeserialization of object after field name change fails Pin
davejsmith16-Jun-10 8:41
davejsmith16-Jun-10 8:41 
AnswerRe: Deserialization of object after field name change fails Pin
Luc Pattyn19-Jun-10 0:39
sitebuilderLuc Pattyn19-Jun-10 0:39 
AnswerRe: Deserialization of object after field name change fails Pin
freakyit21-Jun-10 5:19
freakyit21-Jun-10 5:19 
GeneralRe: Deserialization of object after field name change fails Pin
Dave Kreskowiak21-Jun-10 5:42
mveDave Kreskowiak21-Jun-10 5:42 
GeneralRe: Deserialization of object after field name change fails Pin
freakyit21-Jun-10 9:46
freakyit21-Jun-10 9:46 
QuestionMerging object graph. Pin
Tiju John15-Jun-10 22:42
Tiju John15-Jun-10 22:42 
Hi guys, has anyone come across any scenario wherein you needed to merge one object with another object of same type, merging the complete object graph.

for e.g. If i have a person object and one person object is having first name and other the last name, some way to merge both the objects into a single object.
public class Person
{
 public Int32 Id { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
}

public class MyClass
{
  //both instances refer to the same person, probably coming from different sources
  Person obj1 = new Person(); obj1.Id=1; obj1.FirstName = "Tiju";
  Person obj2 = new Person(); ojb2.Id=1; obj2.LastName = "John";


  //some way of merging both the object
  obj1.MergeObject(obj2); //??
  obj1.Id // = 1
  obj1.FirstName // = "Tiju"
  obj1.LastName // = "John"
}



I had come across such type of requirement and I wrote an extension method to do the same.
public static class ExtensionMethods
{
    private const string Key = "Id";

    public static IList MergeList(this IList source, IList target)
    {
        Dictionary<string, object> itemData = new Dictionary<string, object>();

        //fill the dictionary for existing list
        string temp = null;
        foreach (object item in source)
        {
            temp = GetKeyOfRecord(item);
            if (!String.IsNullOrEmpty(temp))
                itemData[temp] = item;
        }

        //if the same id exists, merge the object, otherwise add to the existing list.

        foreach (object item in target)
        {
            temp = GetKeyOfRecord(item);
            if (!String.IsNullOrEmpty(temp) && itemData.ContainsKey(temp))
                itemData[temp].MergeObject(item);
            else
                source.Add(item);
        }

        return source;
    }

    private static string GetKeyOfRecord(object o)
    {
        string keyValue = null;
        Type pointType = o.GetType();
        if (pointType != null)
            foreach (PropertyInfo propertyItem in pointType.GetProperties())
            {
                if (propertyItem.Name == Key)
                { keyValue = (string)propertyItem.GetValue(o, null); }
            }
        return keyValue;
    }

    public static object MergeObject(this object source, object target)
    {
        if (source != null && target != null)
        {
            Type typeSource = source.GetType();
            Type typeTarget = target.GetType();

            //if both types are same, try to merge
            if (typeSource != null && typeTarget != null && typeSource.FullName == typeTarget.FullName)
                if (typeSource.IsClass && !typeSource.Namespace.Equals("System", StringComparison.InvariantCulture))
                {
                    PropertyInfo[] propertyList = typeSource.GetProperties();

                    for (int index = 0; index < propertyList.Length; index++)
                    {
                        Type tempPropertySourceValueType = null;
                        object tempPropertySourceValue = null;
                        Type tempPropertyTargetValueType = null;
                        object tempPropertyTargetValue = null;

                        //get rid of indexers
                        if (propertyList[index].GetIndexParameters().Length == 0)
                        {
                            tempPropertySourceValue = propertyList[index].GetValue(source, null);
                            tempPropertyTargetValue = propertyList[index].GetValue(target, null);
                        }
                        if (tempPropertySourceValue != null)
                            tempPropertySourceValueType = tempPropertySourceValue.GetType();
                        if (tempPropertyTargetValue != null)
                            tempPropertyTargetValueType = tempPropertyTargetValue.GetType();



                        //if the property is a list
                        IList ilistSource = tempPropertySourceValue as IList;
                        IList ilistTarget = tempPropertyTargetValue as IList;
                        if (ilistSource != null || ilistTarget != null)
                        {
                            if (ilistSource != null)
                                ilistSource.MergeList(ilistTarget);
                            else
                                propertyList[index].SetValue(source, ilistTarget, null);
                        }

                        //if the property is a Dto
                        else if (tempPropertySourceValue != null || tempPropertyTargetValue != null)
                        {
                            if (tempPropertySourceValue != null)
                                tempPropertySourceValue.MergeObject(tempPropertyTargetValue);
                            else
                                propertyList[index].SetValue(source, tempPropertyTargetValue, null);
                        }
                    }
                }
        }
        return source;
    }
}


However, this works when the source property is null, if target has it, it will copy that to source.

IT can still be improved to merge when inconsistencies are there e.g. if FirstName="Tiju" and FirstName="John"

Any commments appreciated.



Thanks
TJ
Question.net framework automatic update? Pin
divyesh143213-Jun-10 22:48
divyesh143213-Jun-10 22:48 
AnswerRe: .net framework automatic update? Pin
Abhinav S13-Jun-10 22:58
Abhinav S13-Jun-10 22:58 
AnswerRe: .net framework automatic update? Pin
Henry Minute14-Jun-10 3:48
Henry Minute14-Jun-10 3:48 
AnswerRe: .net framework automatic update? Pin
AnnieMacD15-Jun-10 12:41
AnnieMacD15-Jun-10 12:41 
GeneralRe: .net framework automatic update? Pin
divyesh143215-Jun-10 18:34
divyesh143215-Jun-10 18:34 
QuestionProblem with autoscrolling of the .NET ScrollableControl Pin
Shao Voon Wong13-Jun-10 21:04
mvaShao Voon Wong13-Jun-10 21:04 
AnswerRe: Problem with autoscrolling of the .NET ScrollableControl Pin
Adam R Harris17-Jun-10 6:11
Adam R Harris17-Jun-10 6:11 
GeneralRe: Problem with autoscrolling of the .NET ScrollableControl Pin
Shao Voon Wong27-Jun-10 17:26
mvaShao Voon Wong27-Jun-10 17:26 
Questionfaster C++ .NET webclient usage? Pin
T210213-Jun-10 19:27
T210213-Jun-10 19:27 
AnswerRe: faster C++ .NET webclient usage? Pin
Eddy Vluggen14-Jun-10 2:34
professionalEddy Vluggen14-Jun-10 2:34 
GeneralRe: faster C++ .NET webclient usage? Pin
T210214-Jun-10 19:24
T210214-Jun-10 19:24 
QuestionDatabase or no database in Software development? Pin
cyberexel12-Jun-10 18:33
cyberexel12-Jun-10 18:33 
AnswerRe: Database or no database in Software development? Pin
Dave Kreskowiak13-Jun-10 5:25
mveDave Kreskowiak13-Jun-10 5:25 
AnswerRe: Database or no database in Software development? Pin
PIEBALDconsult13-Jun-10 18:51
mvePIEBALDconsult13-Jun-10 18:51 
GeneralRe: Database or no database in Software development? Pin
cyberexel14-Jun-10 23:10
cyberexel14-Jun-10 23:10 
GeneralRe: Database or no database in Software development? Pin
PIEBALDconsult15-Jun-10 3:10
mvePIEBALDconsult15-Jun-10 3:10 
GeneralRe: Database or no database in Software development? Pin
Eddy Vluggen15-Jun-10 3:36
professionalEddy Vluggen15-Jun-10 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.