4

Our supplier's webservice is returning address objects (~30 fields) and I'm using LINQ and reflection to store the returned data straight into the databse. I loop over the attributes and set the value from the object to the LINQ class.

I'm fairly new to refection and I'm worried this may be bad practice; it saves ~30 lines of code in 3 or 4 methods but I'm afraid it could introduce hard to fix bugs.

EDIT Here is the code. Target is normally a LINQ to SQL entity and input would be a webservice object.

public static void SetValues(object target, object input)
{
    Type targetType = target.GetType();
    Type inputType = input.GetType();

    PropertyDescriptorCollection inputProperties = TypeDescriptor.GetProperties(inputType);
    PropertyDescriptorCollection targetProperties = TypeDescriptor.GetProperties(targetType);

    foreach (PropertyDescriptor p in inputProperties)
    {
        object value = p.GetValue(input);

        var t = targetProperties.Find(p.Name, false);
        if (t != null && value != null)
        {
            if (value.GetType() == typeof(string) || value.GetType() == typeof(Int64) || value.GetType() == typeof(int) || value.GetType() == typeof(Boolean))
            {
                t.SetValue(target, value);

            }
            else if (value.GetType() == typeof(DateTime) || value.GetType() == typeof(DateTime?))
            {
                if ((DateTime)value > DateTime.MinValue)
                {
                    t.SetValue(target, value);
                }

            }
            else
            {
                t.SetValue(target, value.ToString());
            }
        }

    }
2
  • Could you post concrete code to illustrate what you mean? Commented Oct 14, 2011 at 15:54
  • @PéterTörök See the edit Commented Oct 14, 2011 at 16:00

2 Answers 2

5

Personally, I hate reflection in most cases -- I've seen is used to get around bad design decisions or because people don't know better more often than not.

This is not one of those cases -- really it is a key-value match you are doing and it makes sense if you are willing to always keep field names and types in lock-step.

All that said, you might want to look at AutoMapper as it does this in about 3 lines of code and lots more to boot.

4

From a performance perspective, there is nothing wrong with this code as time it takes to call to the database will dwarf any performance implications of the reflection. However, you are coupling your domain object to your data object by forcing the property names and types to identical. If this isn't an issue for you, I think it's fine.

2
  • I'm forced into coupling anyway. If they change property names it wold break however I did it Commented Oct 15, 2011 at 19:18
  • I think the problem is, if you refactor the name of one of your properties in your domain object and forget to do so in your data object, you'll have a bug on your hands that'll be very difficult to track down. At the very least you could force your domain and data objects to implement the same interface, and only copy members of this interface. This will catch any issues at compile time.
    – afeygin
    Commented Oct 24, 2011 at 18:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.