0

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

stefan.m
  • 17
  • 1
  • 1
    I have used mappers for this. If you use a mapper the advantage is you don't have to write a lot of boilerplate code as there are mapper libraries that can do automatic mapping based on attribute names. – Chamindu Jul 08 '16 at 05:30
  • http://programmers.stackexchange.com/questions/70877/are-design-patterns-really-essential-nowadays/70893#70893 – MetaFight Jul 09 '16 at 15:58

3 Answers3

3

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.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
1

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

MetaFight
  • 11,549
  • 3
  • 44
  • 75
  • 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 09 '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 09 '16 at 16:19
0

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.

IWebDataService
{
     DomainObject Get(string id);
}
Ewan
  • 70,664
  • 5
  • 76
  • 161