99

I was under the impression they are all basically the same. Are model objects also the same?

Right now, in my architecture, I have:

class Person 
{

    public string PersonId;        
    public string Name;
    public string Email;

    public static bool IsValidName() { /* logic here */ }
    public static bool IsValidEmail() { /* logic here */ }
}


class PersonService
{
    private PersonRepository pRepository;

    PersonService()
    {
        pRepository = new PersonRepository();
    }

    public bool IsExistingEmail(string email)
    {
        //calls repo method to see if email is in db
    }


    public Person GetPerson(email)
    {
        return pRepository.Get(email);
    }


    public void SavePerson(Person p)
    {
        if (Person.IsValidEmail(p.Email) && !IsExistingEmail(p.Email)
        {
            pRepository.Save(p);
        }
    }

}


class PersonRepository
{
    public void Save(Person p)
    {
        //save to db
    }

    public Person Get(string email)
    {
        //get from db
    }

    public bool IsExistingEmail(string email)
    {
        //see if email in db
    }

}

So which of the above classes are POCO, Domain Object, Model object, entity?

5 Answers 5

133

My (non-standard) Layman definitions

  • POCO - Plain Old %Insert_Your_Language% Object. A type with no logic in it. It just stores data in memory. You'd usually see just auto properties in it, sometimes fields and constructors.
  • Domain object an instance of a class that is related to your domain. I would probably exclude any satellite or utility objects from domain object, e.g. in most cases, domain objects do not include things like logging, formatting, serialisation, encryption etc - unless you are specifically building a product to log, serialise, format or encrypt respectively.
  • Model object I think is the same as Domain object. Folks tend to use this interchangeably (I can be wrong)
  • Entity a class that has id
  • Repository a class that speaks to a data storage from one side (e.g. a database, a data service or ORM) and to the service, UI, business layer or any other requesting body. It usually hides away all the data-related stuff (like replication, connection pooling, key constraints, transactions etc) and makes it simple to just work with data
  • Service software that provides some functionality usually via public API. Depending on the layer, it can be for example a RESTful self-contained container, or class that allows you to find a particular instance of needed type.

Original answer

These are terms that are largely used in (Distributed) Domain Driven Design. They are not the same. The term model Object can be used as a synonym to the domain object.

Domain Objects. Objects from the business specific area that represent something meaningful to the domain expert. Domain objects are mostly represented by entities and value objects. Generaly speaking, most objects that live in domain layer contribute to the model and are domain objects.

Entity. An object fundamentally defined not by its attributes, but by a thread of continuity and identity. (Meaning it must have Id)

POCO. A simple object without complicated logic, usually it has just a few properties and is used with ORM or as a Data Transfer Object

class Person - Entity and POCO, instance of this class is Domain Object
class PersonService - Service
class PersonRepository - Repository

19
  • 4
    Please see my code sample above and let me know how you would apply the terms.
    – jpshook
    Commented May 27, 2011 at 20:06
  • 1
    If a POCO does not need to be identified, how then can it be loaded by an ORM. Thats not logical!
    – Pascal
    Commented May 11, 2016 at 18:04
  • 4
    Why is this blatantly false information the most upvoted and accepted. POJO is NOT a type with no logic in it. In fact, Martin Fowler specifically coined the term POJO to contrast against Entity Beans ("we were pointing out the many benefits of encoding business logic into regular java objects rather than using Entity Beans."). martinfowler.com/bliki/POJO.html Commented Apr 20, 2018 at 23:14
  • 3
    I am sorry if my comment was strongly worded, but again, your personal experience don't really matter when the coiner of the term defined POJO to explicitly contain business logic. In fact, he would classify your practice under anemic domain model (martinfowler.com/bliki/AnemicDomainModel.html) which he considers to be an anti-pattern, and he recommends that you use POJO instead. Whether it's an anti-pattern is out of scope of this definition, but one thing is clear: POJO does not mean an object without any business logic. Commented Apr 23, 2018 at 18:21
  • 2
    @JohnPancoast An object with setters/getters and little else is a Data Transfer Object (DTO). Sometimes it's a full blown javabean. POJO/POCO's are a rejection of such frameworky nonsense that tries to convince you that you can't solve problems with normal objects without inheriting their magical powers. When you write a good POJO you can't tell if a framework is even being used. Commented Jan 15, 2021 at 7:27
21

basically it comes down to internal logic

  1. Domain objects have internal domain logic for things like validation, etc.
  2. Model is basically a light Domain object, they know about the data they hold but nothing really about how it's going to be used
  3. Entities hold data and have some internal knowledge of where it came from, and where it's going to be saved, updated, etc
  4. POCO holds data and may have some internal knowledge about it's self, things like what is the total value of all the items in a property collection
  5. DTO is the simplest item of all, it just holds data and has no logic

They are all basically used for the same thing, it's just how smart you want them to be

according to your code sample The Person class would be a domain object or a model, the other 2 are a service and a repository. Domain objects, Pocos, models, dtos, etc. are used like messages, passed from one layer to the next, a service class like PersonService is a layer in the application and the same with the Repository class like PersonRepository. for a good over view take look at http://bob-the-janitor.blogspot.com/2009/07/n-tier-design-revisit-part-1-over-view.html in this case it's talking about using a data entity which is basically a dto

18

It's more of a connotation of function; a domain object is something that is specific to your logic implementation and might be more complex than a simple POCO; an entity has a connotation to represent something (usually in reference to a persistence medium), and a POCO is just a quick identifier for a class. A model is just a term used to represent an object (usually containing state and usually dealing with the UI or DB).

It's not that there is any functional difference, they're just different terms to more closely describe something. Like the difference between race car, truck, and family sedan. All are automobiles, but each term is more descriptive.

1
  • I updated to include a code sample, can you please comment? I just want to make sure when I am asking questions, that I am using the correct terms.
    – jpshook
    Commented May 27, 2011 at 20:04
13

There are already good explainations of Domain and Model in the answers above.

In a Database-Context Entity means Item in a Entity Relationship Model ERD. (i.e. a Row in a Table)

In the Microsoft-Dotnet-EntityFramework-World Entity means an Object that can be loaded from and saved to a database using a Data(Base)Context. Usually an Entity cannot exist without its Data(Base)Context. (Unit-) Testing the business functionality of these classes is difficuilt.

Pocos (Plain Old CommonRuntime Objects) can exist without the PersistenceFramework (EntityFramework or NHibernate) thus they are much easier to test.

The word poco is the adaptaion of pojo (plain old java object) that were created in the java world for the same reason.

3
  • whats the reason for downvoting? The question was "What is the difference between domain objects, POCOs and entities?" i explained the difference between Entity and Poco
    – k3b
    Commented May 31, 2011 at 13:52
  • "There are already good explainations of Domain and Model in the answers above." How could I select a answer that depends on other answers as a valid answer?
    – jpshook
    Commented May 31, 2011 at 15:04
  • np. In retrospect I should have just commented asking you to provide a complete answer. Will think 2x next time.
    – jpshook
    Commented May 31, 2011 at 18:05
3

A domain object is an entity in the domain layer of your application, eg. an Address class. "Model" means the same thing - an entity in the "Domain Model".

A POCO (plain old CLR object) is an object that has no behaviour (methods) defined, and only contains data (properties). POCO's are generally used as DTOs (data transport objects) to carry data between layers, and the data is then commonly used to populate a domain object/entity.

4
  • Can't a POCO be a entity? I thought POCOs can have behavior but must be persistence ignorant?
    – jpshook
    Commented May 27, 2011 at 19:49
  • @Developr Yeah, your domain models/entities could indeed be POCO's - this is what's known as an "anemic" domain model - see martinfowler.com/bliki/AnemicDomainModel.html
    – MattDavey
    Commented May 31, 2011 at 7:53
  • How would you consider this anemic? How do you define "rich domain object?" If the domain objects cannot interact directly or indirectly with the database, what kind of rich functionality can they have?
    – jpshook
    Commented May 31, 2011 at 13:28
  • @Developr I'd consider a domain model composed of POCO objects anemic because traditionally POCO classes don't have any rich functionality, just data. Following the Seperation of Concerns principle (en.wikipedia.org/wiki/Separation_of_concerns), an object in the domain model should not be concerned with database access at all (hence the popular term "persistence ignorance"). The kind of functionality an entity in the domain model should have would be business logic - they should capture and encapsulate the logical rules & workflows of the problem/domain they represent.
    – MattDavey
    Commented Jun 1, 2011 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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