How would you improve this shallow copying class?

后端 未结 4 1960
暗喜
暗喜 2020-12-19 13:20

I\'ve written a class with a single static method that copies property values from one object to another. It doesn\'t care what type each object is, only that they have ide

相关标签:
4条回答
  • 2020-12-19 14:13

    A new method that created a new instance of To and called the Copy() method before returning might be useful.

    Like this:

    public static To Create<From, To>(From from)
        where To : class, new()
        where From : class
    {
        var to = new To();
        Copy(from, to);
        return to;
    }
    
    0 讨论(0)
  • 2020-12-19 14:20

    Are your DTOs serializable? I would expect so, in which case:

    MemberInfo[] sm = FormatterServices.GetSerializableMembers(typeof(From));
    object[] data = FormatterServices.GetObjectData(from, sm);
    FormatterServices.PopulateObjectMembers(to, sm, data);
    

    But note that I don't really agree with this general approach. I would prefer a strong contract for copying on your DTOs that each DTO implements.

    0 讨论(0)
  • 2020-12-19 14:20
    • Change your type parameter names to comply with naming conventions, e.g. TFrom and TTo, or TSource and TDest (or TDestination).

    • Do most of your work in a generic type instead of in just a generic method. That allows you to cache the properties, as well as allowing type inference. Type inference is important on the "TFrom" parameter, as it will allow anonymous types to be used.

    • You could potentially make it blindingly fast by dynamically generating code to do the property copying and keeping it in a delegate which is valid for the "from" type. Or potentially generate it for every from/to pair, which would mean the actual copying wouldn't need to use reflection at all! (Preparing the code would be a one-time hit per pair of types, but hopefully you wouldn't have too many pairs.)

    0 讨论(0)
  • 2020-12-19 14:20

    Decide what you want to do if passed objects of types that share some properties but not all. Check for the existence of the property in the From object in the To object before trying to set it's value. Do the "right thing" when you come to a property that doesn't exist. If all of the public properties need to be identical, then you will need to check if you've set all of them on the To object and handle the case where you haven't appropriately.

    I'd also suggest that you may want to use attributes to decorate the properties that need to be copied and ignore others. This would allow you to go back and forth between the two different objects more easily and continue to maintain some public properties that are derived rather than stored on your business object.

    0 讨论(0)
提交回复
热议问题