0

I'm trying to improve myself with generics but actually I dont know how to even start this task:

I need to build a method that will record two tables with logs. This method will recieve two objects of the same type and then I need them to be compared.

That the first table will be recorded with the name of the object.

The second table will receive the following data: the first table record ID, field name, old value and new value.

I need this to be build with generics since I have MANY different objects and I'm sure that's going to be painful and weird to build any kind of switch case for each object that I want to record those information.

I hope you guys help me !

1 Answer 1

1

Unless these objects share a common interface you'll have to use reflection to get the properties of an object, then loop through them and compare the values between objectA and objectB. Something like this:

public void CompareTwoObjectsAndSaveChanges<TObjectType>(TObjectType objectA, TObjectType objectB )
    {
        //makes sure both objects match in Type
        if(objectA.GetType() == objectB.GetType())
        {
           //uses reflection to get all the properties of that object.
           foreach (var prop in objectA.GetType().GetProperties())
           {
               //checks to see if the value is different
               if(prop.GetValue(objectA, null) != prop.GetValue(objectB, null))
               {
                   //get the property name and its two different values
                   string nameOfPropertyThatChanges = prop.Name;
                   string objectAValue = prop.GetValue(objectA, null).ToString();
                   string objectBValue = prop.GetValue(objectB, null).ToString();

                   //logic to save to database
               }
           }   
        }
    }

You'll have to have oldValue and newValue columns be set to varchar or nvarchar in the database and convert all property values to strings before saving, otherwise you can't do what you are asking. Technically you wouldn't even have to use generics here as both parameters could simply take an object and the reflection would still work AS LONG AS both objects were the same type. The generic parameter here just makes it a bit more clean and makes sure that the person calling this method can't pass in two objects of different types accidently.

EDIT: To get the name of the object for your first table you would do:

typeof(TObjectType).ToString();
Sign up to request clarification or add additional context in comments.

1 Comment

Except really he should be verifying that objectA.GetType() == objectB.GetType() and be using objectA.GetType().GetProperties(), that way you are checking the data on all of the runtime data rather than just compile-time. Because if at compile-time the objects are casted down to (System.Object), it won't be checking any relevant properties at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.