patterncsharpMinor
Fair use or Abuse of the dynamic (System.Dynamic.DynamicObject)?
Viewed 0 times
dynamicobjectfairthesystemabusedynamicuse
Problem
I have a T4 Templates project that generates methods to "convert" one Type (class, struct, enum) from a source Assembly its corresponding Type in another. The two types have the identical names and property names, just different namespaces and different types. This may be a re-invented a wheel. Nonetheless, I didn't find any solutions out there immediately, and it felt like a good programming exercise anyway. I'm also not familiar with dynamic languages and though it would be a worthwhile application of the dynamic (System.Dynamic.DynamicObject) keyword.
This following is the .tt file which writes out all the "convert" methods, but most of the work is done in a static class with numerous helper extension methods.
Notice the first line in every generated convert method will declare a series of dynamic variables, most of which will be reused. The reason I used dynamic keywords was so I could reuse the same variable names, even if the types assigned to those variables change later. This would make this T4 template function "WriteConvertType" easier to finish writing, in my opinion. To clarify, the dynamic keyword is not used anywhere within this helper method - the dynamic keyword will show up in the generated code with all the conversion methods. At the end of the question I'll post a snippet of what the generated code looks like, since it is lengthy.
This is the helper class where a recursive extension method "WriteConvertType" is defined.
```
public static void WriteConvertType(BaseTemplate template, Type destType, Type srcType, Stack stack = null)
{
if (stack == null)
stack = new Stack();
if (srcType.IsNullableType())
{
template.WriteLine("dest{0} = new
This following is the .tt file which writes out all the "convert" methods, but most of the work is done in a static class with numerous helper extension methods.
namespace
{
public static class ConvertExtension
{
public static Convert(this src0)
{
dynamic dest0;
dynamic
return dest0;
}
}
}
Notice the first line in every generated convert method will declare a series of dynamic variables, most of which will be reused. The reason I used dynamic keywords was so I could reuse the same variable names, even if the types assigned to those variables change later. This would make this T4 template function "WriteConvertType" easier to finish writing, in my opinion. To clarify, the dynamic keyword is not used anywhere within this helper method - the dynamic keyword will show up in the generated code with all the conversion methods. At the end of the question I'll post a snippet of what the generated code looks like, since it is lengthy.
This is the helper class where a recursive extension method "WriteConvertType" is defined.
```
public static void WriteConvertType(BaseTemplate template, Type destType, Type srcType, Stack stack = null)
{
if (stack == null)
stack = new Stack();
if (srcType.IsNullableType())
{
template.WriteLine("dest{0} = new
Solution
With AutoMapper (automapper.codeplex.com), your complete code could be flattened down to:
As you've said that the types use the same property types and names, you won't need to customise a mapping profile.
using SLProduct = Northwind.SL.Model.Product;
using NetProduct = Northwind.NET.Model.Product;
//
SLProduct slProduct = Mapper.Map(nlProduct);As you've said that the types use the same property types and names, you won't need to customise a mapping profile.
Code Snippets
using SLProduct = Northwind.SL.Model.Product;
using NetProduct = Northwind.NET.Model.Product;
//
SLProduct slProduct = Mapper.Map<NetProduct, SLProduct>(nlProduct);Context
StackExchange Code Review Q#2772, answer score: 8
Revisions (0)
No revisions yet.