Can anyone point me to some full code samples of how to integrate the Glue framework into a project? I am attempting to use it a bit like AutoMapper in that I would hope to be able to register a bunch of conversions to Glue, then arbitrarily hand it two objects for it to figure out from there. The website gives a good idea of how to make individual mappings, but not how to integrate those into the project at large.
It would all be abstracted away, of course, so one could do a manual mapping if Glue became too slow or the mapping framework needed to change, but as it stands right now the main use-case I see is creating a lot of Glue.Converter.IConverter objects and having the IoC container of your choice inject them to wherever they are needed. This sounds fine, but our project will require dozens upon dozens of these zero-calorie objects if this is the procedure we use.
This required a hell of a lot of finagling, but I took care of it.
Using the TypeSwitch code to allow me to do some switching on type, I initially wrote out this code:
retMapping = AddressToDatabaseAddressMappingwas causing a compiler error because you can’t automatically assignMapping<Address,DatabaseAddress>toMapping<T1,T2>. Even if, heuristically, they are the same thing here, they’re not LOGICALLY the same thing.Turns out, you can effectively make C# operate as a dynamic language by leveraging the
objecttype, though.This is a little more verbose than it needs to be to show my point, but it’s interesting to note that at run-time,
AddressToDatabaseAddresswill maintain its identity even though its static type doesn’t imply it needs to. And since, heuristically,Mapping<T1, T2>ISMapping<Address, DatabaseAddress>, it will assign itself properly at runtime and work perfectly.It is important to note that I had to add something onto
TypeSwitchto maketypeof(T1)andtypeof(T2)work properly, however. The way it’s written by default,object sourcethat is passed in isSystem.Type, which is not the behavior you want! However, adding an overload of:Will make it behave as intended. Since the
System.Typeoverload is more specific than theSystem.Objectoverload, the framework will prefer to use it. MakesTypeSwitcha little more sane for a common use-case, if someone prefers to throw in the type directly.