I'm using C# to code a basic property/value mapping class.
I want to be able to target a specific member on an object, and then specify various things to do with that member through a lambda expression.
The idea is to construct a new mapping using a static method something like so:
var myMapping = Mapping<TObject>.New(o => o.MyProperty).Read(GetString);
In this example, TObject is the Type of object whose property I want to map, New(...) targets the 'MyProperty' member of the TObject and Read(...) defines an expression which returns a value that is the same type of 'MyProperty'.
So we now have a 'Mapping' object that is aware of:
- What member it is associated with.
- How to get a value for that member.
We can then leave the logic around how to set that member to that value up to inheritors of 'Mapper'. e.g. a certain type of mapping might want to 'validate' values that are read, only setting MyProperty if they are valid.
You would then use your mapping like so:
myMapping.Read(myTObject);
Where 'myTObject' is an instance of type 'TObject'.
I see implementations of this kind of thing commonly in mocking libraries like RhinoMock, where you can target specific members and set options for them.
Can somebody provide me with an example of how to achieve this, or at least tell me if my head is in the clouds?