Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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());
            }
        }

    }
share|improve this question
    
Could you post concrete code to illustrate what you mean? –  Péter Török Oct 14 '11 at 15:54
    
@PéterTörök See the edit –  Tom Squires Oct 14 '11 at 16:00
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
I'm forced into coupling anyway. If they change property names it wold break however I did it –  Tom Squires Oct 15 '11 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 Oct 24 '11 at 18:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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