Often time we need to copy values for objects having same property names and data types. Sometimes this can be done efficiently using libraries such as AutoMapper. However I have found it to be cumbersome to use it and also it adds lot of testing issues like explained at
https://softwareengineering.stackexchange.com/questions/167250/am-i-wrong-in-thinking-that-needing-something-like-automapper-is-an-indication-o
https://cezarypiatek.github.io/post/why-i-dont-use-automapper/
Instead I personally prefer plain old code and have found some useful shortcuts to reduce the efforts.
1) The following regex can be used in Find and Replace window to copy values between two such objects
Find: [a-zA-Z]* [a-zA-Z]*[\?]? ([a-zA-Z]* ){ get; set; }
Replace: $1 = SourceObj.$1,
https://softwareengineering.stackexchange.com/questions/167250/am-i-wrong-in-thinking-that-needing-something-like-automapper-is-an-indication-o
https://cezarypiatek.github.io/post/why-i-dont-use-automapper/
Instead I personally prefer plain old code and have found some useful shortcuts to reduce the efforts.
1) The following regex can be used in Find and Replace window to copy values between two such objects
Find: [a-zA-Z]* [a-zA-Z]*[\?]? ([a-zA-Z]* ){ get; set; }
Replace: $1 = SourceObj.$1,
2) Another regex
Find: public (?:virtual )?(?:\w+\<)?\w+\??(?:\>)? (\w+) { get; set; }
Replace: this.$1 = obj.$1;
2) Use custom reflection method as explained at
https://www.pluralsight.com/guides/property-copying-between-two-objects-using-reflection
Although end result by this method is similar to that of AutoMapper, a very big advantage is since we have entire source code in our project, it is easier to debug or add custom functionality to it.
2) Use custom reflection method as explained at
https://www.pluralsight.com/guides/property-copying-between-two-objects-using-reflection
Although end result by this method is similar to that of AutoMapper, a very big advantage is since we have entire source code in our project, it is easier to debug or add custom functionality to it.
No comments:
Post a Comment