Using Automapper while using specification pattern to compose objects

374 views Asked by At

A specification pattern can be used to compose objects as shown in the example below:

IUser user =
                UserSpecification
                    .ForPerson()
                    .WithName("myname")
                    .WithSurname("mysurname")
                    .WithPrimaryContact(ContactSpecification.ForEmailAddress("[email protected]"))
                    .AndNoMoreContacts()
                    .Build();

This leads to manually map the data from DTO to the specification object. Is there a way, we can use automapper to fill object while using specification pattern? Does Automapper support this in any way?

Thanks

1

There are 1 answers

0
Jimmy Bogard On BEST ANSWER

I don't think so, typically the specification pattern is used for piecemeal setting of individual properties. The implementation of the pattern involves for each method actually setting a property, by hand.

AutoMapper always maps from an object, in the above, I don't see a source object, just a specification. If the specification filled an object, then that object was mapped to the destination, then it would work. The result above from "Build()" could be mapped to "IUser".

Otherwise, it doesn't make much sense. The code inside a specification pattern is setting up an object, and trying to map this to AutoMapper configuration I think would be far more trouble/confusing than it would be worth.