What is Java domain model?
Asked Answered
S

6

38

I am studying a Spring book and they mention Java domain model.
What is that?

Sweetbread answered 6/2, 2011 at 13:51 Comment(0)
C
42

A domain model (the term is not at all Java specific) is a class that models something in the problem domain, as opposed to a class that exists for technical implementation reasons.

Domain model instances often need to be persisted in a database, and in Java, they typically conform to the Java Beans specification, i.e. they have get and set methods to represent individual properties and a parameterless constructor. Spring and other frameworks allow you to access these properties directly in your JSPs.

For example, in a shop application, some of your domain model classes would be Product, Order, ShoppingCart and Customer.

Cicelycicenia answered 6/2, 2011 at 13:58 Comment(3)
what does the word "persisted in database" means in layman languageSweetbread
@Name, it means that they outlive the application instance containing them. E.g. after the shop app is shut down and restarted, it will still recognize you as a registered Customer and will remember the contents of your shopping cart and your past orders.Quadrillion
@Name: "persist" basically means "save".Cicelycicenia
F
11

A Domain model is a conceptual model of the problem domain. By "java domain model" they just mean the java classes representing that model. There's nothing specific to java in the concept.

See also Domain Driven Design for an approach to focusing your development on the business domain needs.

Ferret answered 6/2, 2011 at 14:0 Comment(0)
C
7

Michael Borgwardt's answer "A domain model (the term is not at all Java specific) is a class" is wrong. I am very surprised so many agree with that answer.

A domain model is all the classes that model the behavior of the solution. It is the minimum necessary to accomplish the required behavior. The domain model is free of UI and persistence functionality (unless the problem revolves around UI or persistence).

I have seen domain model implemented in one class but that is not the design of an object-oriented solution. In an object-oriented domain model, each concept has its own class that implements the behavior required of that concept and contains the necessary fields to maintain the state of the class.

Cassilda answered 29/10, 2011 at 1:14 Comment(1)
The term "domain model" has more Han one definition. It may mean the entire app model, or a single entity model. It depends on context.Hansel
B
5

Let’s start with an example. You are creating an application with will be used by some people in your locality. When design the system you call these people users of your system. You also have to manage a list of roles for these people in the system and authentication information. So, you decide to create a conceptual entity in the system. This conceptual entity is further mapped to a User object in you software solution (your application). Now when you represent your application, you describe that User object as a Domain Model. The basic idea behind this term is that only. You can further read about it in the following Wikipedia link.

Babu answered 6/2, 2011 at 14:9 Comment(0)
M
5

I know it has been a long time since the last post here. But it is important that information around this concept is clear. A domain model is often a set of classes which represent a particular problem domain. The concept is not tied to any one type of technology implementation. I think it is a little misleading to say :

"Domain model instances often need to be persisted in a database, and in Java, they typically conform to the Java Beans specification, i.e. they have get and set methods to represent individual properties and a parameter-less constructor. Spring and other frameworks allow you to access these properties directly in your JSPs"

Domain models are often the result of domain driven design. Domain driven design is teh key to a good and robust domain model. I suggest having a read through Eric Evans' book Domain Driven Design, to give you a better understanding.

Domain model classes do have information associated to them but behavior in my opinion, is more important than data in this context. A big mistake around domain driven design is to create data classes that represent the data of a domain entity ,such as customer and provide only public getters and setters for the customer attributes. These object tend just to mimic your database structure and as a result the actual business logic is more likely to reside in domain services , resulting in a anemic domain model. This model is closer to a Transaction Script than a domain model.

Medrek answered 24/8, 2013 at 22:7 Comment(0)
B
0

In layman's terms, your domain package is the object representation of elememts you would primarily but not necessarily need to render UI fields like username, client_info, solution specific pojos etc. The classes contained in the domain package will be used in the DAO(DATA ACCESS OBECT) package, where the dao will query the database and map these fields in the classes in domain and return them. Or you could use a framework like hibernate and return this object, where querying the DB will be taken care by a framework like hibernate.

This answer is more MVC and java specific but details one of the possible implementations.

Berfield answered 13/5, 2019 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.