Thursday, 15 March 2012

how to create an object using another as a source in Autofixture? -



how to create an object using another as a source in Autofixture? -

i have this:

public class modelentity : entity { public override int id { get; set; } public string filename { get; set; } } public class datatransferobject { public int id { get; set; } public string filename { get; set; } }

and this:

var model = _fixture.create<modelentity>(); var dto = _fixture.create<datatransferobject>().fillwith(model);

right doing next not sure if right way it

var model = _fixture.create<modelentity>(); var dto = model.assource().oflikeness<datatransferobject>().createproxy();

autofixture doesn't have feature that, think there's improve learned this:

autofixture built tool test-driven development (tdd), , tdd feedback. in spirit of goos, should hear tests. if tests hard write, should consider api design. autofixture tends amplify sort of feedback, , may case here.

it sounds need able populate datatransferobject values modelentity instance. suggest sort of mapping valuable add-on api?

depending on how these types coupled, could consider adding projection method modelentity class:

public class modelentity : entity { public override int id { get; set; } public string filename { get; set; } public datatransferobject todatatransferobject() { homecoming new datatransferobject { id = this.id, filename = this.filename }; } }

however, disadvantage of approach couples 2 types each other.

if find undesirable, instead introduce dedicated mapper service, can map modelentity instance datatransferobject object - , perhaps vice versa.

if, unfathomable reason, don't want introduce such mapper scheme under test, can still add together reusable service in test project.

if don't wish write such mapper yourself, consider using automapper purpose.

autofixture

No comments:

Post a Comment