mardi 8 novembre 2016

Comparing complex object properties using Compare-Net-Objects

I want to compare 2 complex object using Compare-Net-Objects dll but it is not giving me desired result.

public class UserEntity 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    private List<string> storeDetails;
    public List<string> StoreDetails
    {
        get { return storeDetails; }
        set { storeDetails = value; }
    }

    Permission permission;
    public Permission UserPermission
    {
        get { return permission; }
        set { permission = value; }
    }
}

public class Permission 
    {
        public string PermissionName { get; set; }
        public string Condition { get; set; }

        private List<string> parameters;

        public List<string> Parameters
        {
            get { return parameters; }
            set { parameters = value; }
        }

    }



    public void GetOriginalUserEntity()
        {
            UserEntity OriginaluserEntity = new UserEntity()
            {
                FirstName = "Mark",
                LastName = "Johnell",
            }};

             UserEntity UpdateduserEntity = new UserEntity()
            {
                FirstName = "MarkDavid",
                LastName = "Johnelee",
                StoreDetails = new List<string>() { "C", "B", "D", "F" },
                UserPermission = new UserPermission()
                {
                      Permission permission = new Permission();
                      permission.PermissionName = "ACCESS_ADMIN_MODULE";
                      permission.Parameters = new List<string>() 
                      {                      
                         "Parameter_1",  
                         "Parameter_2" };
                      }
            }};
        CompareLogic compareObjects = new CompareLogic();
        compareObjects.Config.MaxDifferences = 99;
        ComparisonResult compResult = compareObjects.Compare(OriginaluserEntity, UpdateduserEntity);


foreach (var change in compResult.Differences)
            {

                string Values = string.Empty;
                if (change.PropertyName.Substring(0, 1) == ".")
                    Values = "Property Name - " + change.PropertyName.Substring(1, change.PropertyName.Length - 1);


                Values += " Origianl Value - " + change.Object1Value;
                Values += " Changed Value - " + change.Object2Value;

                Console.WriteLine(Values);

            }


        }

This gives me desired result like original value and changed values for simple fields like firstName and LastName and complex filed like storeDetails when both the object has values. But if original object storedetails has null and updated has list of values(strings) then it shows type of storesdetails(Like genericList> not actual values. This applies to Permission object as well.

enter image description here

Can anybody help me with this? I need to generate report from this to show Audit Trail of object changes during user operation (PRE and POST values).





Aucun commentaire:

Enregistrer un commentaire