ObjectHydrator icon indicating copy to clipboard operation
ObjectHydrator copied to clipboard

Generation of values based on other values?

Open rikrak opened this issue 10 years ago • 4 comments

Hi, Neat project - this could really help me out :-)

Is it possible to generate on value based on the value of other properties?

For example: I have a gender field. If this is female I'd like to generate Female first names, otherwise I'd generate male first names

Rik

rikrak avatar Oct 03 '14 15:10 rikrak

Hmmm. I'll take a peek and see how that could be accomplished.

Sent from my phone, please pardon the occasional auto-correct hilarity.

On Oct 3, 2014, at 8:00 AM, rikrak [email protected] wrote:

Hi, Neat project - this could really help me out :-)

Is it possible to generate on value based on the value of other properties?

For example: I have a gender field. If this is female I'd like to generate Female first names, otherwise I'd generate male first names

Rik

— Reply to this email directly or view it on GitHub.

PrintsCharming avatar Oct 03 '14 15:10 PrintsCharming

:-D I was figuring there'd need to be some sort of GenerationContext passed around (perhaps as a parameter to the IGenerator.Generate() method) that contained the state of the thing being generated.

There would need to be some care around the order in which things happened too; no point generating a first name before the gender (using my original example).

Mucking about with the Generate method would be a breaking change though :-/

rikrak avatar Oct 03 '14 15:10 rikrak

I'm wondering if this could be done by passing a delegate during the instantiation, something like: Hydrator<Customer>().>AfterGenerate(x=>x.FirstName,new FirstNameGenerator(x.Gender))

PrintsCharming avatar Oct 07 '14 18:10 PrintsCharming

That might work, although the FirstNameGenerator would need to be aware of how a given system defines Gender. For example I use an enum (Male, Female, NotSpecified), whereas other systems may use a single char ('M', 'F') or string ("Male", "Female")

I was thinking about this over the weekend and wondered if the initialisation of the Hydrator could take a predicate:

var hydrator = new Hydrator() .With(t => t.FirstName, new MaleFirstNameGenerator(), t => t.Gender == Gender.Male) .With(t => t.FirstName, new FemaleFirstNameGenerator(), t => t.Gender == Gender.Female) .With(t => t.FirstName, new MaleFirstNameGenerator(), t => t.Gender != Gender.Female && t.Gender != Gender.Male)

This means the hydrator wouldn't need any domain specific knowledge (e.g. what gender actually means to the object being hydrated). Internally the hydrator would need to work out a dependency list for the properties and ensure that the properties were hydrated in the correct order. For example in the code above, the Hydrator should be able to determine that the FirstName value is dependent on the Gender value, so therefore Gender needs to be set before FirstName.

rikrak avatar Oct 08 '14 10:10 rikrak