I'm a little confuse how to create domain objects right. In all/most web application we have simple objects created from POST request. We need to create domain objects that we will use in deeper layers. How we should do this? What kind of pattern should be used? It is very general question but I've considered many patterns (factory, builder, mappers) and still have no good idea.

Thanks, Stefan


Your domain objects design depends on your, well, domain.

You haven't given one so I'll just say the point of domain objects is to create a layer in your architecture where details, like web, database, user interface, are unknown. If your domain object knows you display in a web page it's not a domain object.

Domain objects ignore how the system works. They focus on what the system should do. If your application makes sandwiches your domain objects should know about ingredients, amounts, grilling, toasting. They shouldn't know about knives, bread boards, jars of peanut butter, measuring cups, grills, or toasters. Those details should be abstracted away. A domain is what's on the recipe card. It's not the card.


Just write the code that works and does what you need. Don't worry about what to call that code.

  • 2
    Down vote if you will. It still doesn't change the fact that pattern-driven development is silly. Patterns exist to facilitate talking about code. They aren't recipes, blueprints, or out of the box generic solutions. Treating them as such limits you tremendously. Instead, be pragmatic when coding. Solve your problem the way that best suits your program. – MetaFight Jul 9 '16 at 12:11
  • 2
    Exactly. OP is approaching this backwards. If you don't know what pattern to use, then forget about the patterns, make something that works, then see if what you did could be described by one of the existing patterns. Notice: not everything will be neatly described by the GoF design patterns. – whatsisname Jul 9 '16 at 16:19

You are instanciating your domain objects from data. The repository pattern should work well.

However, given that its not a database the naming convention is to call it a service.

     DomainObject Get(string id);

Not the answer you're looking for? Browse other questions tagged or ask your own question.