域对象、POCO 和实体之间有什么区别?

发布于 2024-11-09 19:45:38 字数 1153 浏览 7 评论 0原文

我的印象是它们基本上都是一样的。模型对象也一样吗?

现在,在我的架构中,我有:

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
    }

}

那么上述类中哪些是 POCO、Domain Object、Model object、entity ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇柔作态 2024-11-16 19:45:38

我的(非标准)外行定义

  • POCO - 普通旧 %Insert_Your_Language% 对象。一种没有逻辑的类型。它只是将数据存储在内存中。您通常只会在其中看到自动属性,有时还会看到字段和构造函数。
  • Domain object 与您的域相关的类的实例。我可能会从域对象中排除任何卫星或实用程序对象,例如在大多数情况下,域对象不包括日志记录、格式化、序列化、加密等内容 - 除非您专门构建一个产品来分别记录、序列化、格式化或加密。
  • 模型对象我认为与域对象相同。人们倾向于互换使用它(我可能是错的)
  • Entity 一个具有 id 的类
  • Repository 一个与数据存储交互的类侧(例如数据库、数据服务或 ORM)以及服务、UI、业务层或任何其他请求主体。它通常会隐藏所有与数据相关的内容(例如复制、连接池、关键约束、事务等),并使使用
  • 通常通过公共 API 提供某些功能的数据服务软件变得简单。根据层的不同,它可以是一个 RESTful 独立容器,或者是允许您查找所需类型的特定实例的类。

原始答案

这些是主要用于(分布式)域驱动设计的术语。它们不一样。术语模型对象可以用作域对象的同义词。

领域对象。来自业务特定领域的对象,代表对领域专家有意义的事物。领域对象主要由实体和值对象表示。一般来说,领域层中的大多数对象都对模型有贡献,并且是领域对象。

实体 一个对象从根本上不是由其属性定义的,而是由连续性和同一性的线索定义的。 (意思是必须Id

POCO。 一个没有复杂逻辑的简单对象,通常只有几个属性,与 ORM 一起使用或作为数据传输对象

class Person - 实体和 POCO,此类的实例是域对象
PersonService 类 - 服务
类 PersonRepository - 存储库

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

阳光①夏 2024-11-16 19:45:38

基本上,它归结为内部逻辑

  1. 域对象具有用于验证等内容的内部域逻辑。
  2. 模型基本上是一个轻型域对象,它们知道它们所保存的数据,但对如何使用它一无所知
  3. 实体保存数据并具有 POCO 保存数据并且可能有一些关于它自身的内部知识
  4. ,例如属性集合中所有项目的总价值是多少
  5. DTO 是最简单的一项,它只保存数据,没有逻辑

它们基本上都用于同一件事,这只是您希望它们根据

您的代码示例 有多智能
Person 类将是域对象或模型,其他两个是服务和存储库。领域对象、Pocos、模型、dtos 等像消息一样使用,从一层传递到下一层,像 PersonService 这样的服务类是应用程序中的一层,与像 PersonRepository 这样的 Repository 类相同。要获得良好的概览,请查看 http://bob-the-janitor.blogspot.com/2009/07/n-tier-design-revisit-part-1-over-view.html 在本例中它正在谈论使用基本上是 dto 的数据实体

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

oО清风挽发oО 2024-11-16 19:45:38

它更多的是功能的内涵;域对象是特定于您的逻辑实现的东西,并且可能比简单的 POCO 更复杂;实体具有表示某种事物的内涵(通常指持久性介质),而 POCO 只是类的快速标识符。模型只是用于表示对象(通常包含状态并且通常处理 UI 或 DB)的术语。

这并不是说有任何功能差异,它们只是更紧密地描述某些事物的不同术语。就像赛车、卡车和家用轿车之间的区别一样。都是汽车,但每个术语都更具描述性。

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.

明月松间行 2024-11-16 19:45:38

上面的答案中已经对领域和模型有了很好的解释。

在数据库上下文中,实体是指实体关系模型 ERD 中的项目。 (即表中的一行)

Microsoft-Dotnet-EntityFramework-World 中,实体表示可以是的对象使用 Data(Base)Context 从数据库加载并保存到数据库。通常,如果没有数据(基础)上下文,实体就无法存在。 (单元-)测试这些类的业务功能很困难。

Pocos(普通旧 CommonRuntime 对象) 可以在没有 PersistenceFramework(EntityFramework 或 NHibernate)的情况下存在,因此它们更容易测试。

poco 这个词是 pojo(普通旧 Java 对象) 的改编,出于同样的原因在 Java 世界中创建了它。

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.

瀟灑尐姊 2024-11-16 19:45:38

域对象是应用程序域层中的实体,例如。地址类。 “模型”意味着同样的事情——“域模型”中的实体。

POCO(普通旧 CLR 对象)是没有定义行为(方法)且仅包含数据(属性)的对象。 POCO 通常用作 DTO(数据传输对象)在层之间传输数据,然后数据通常用于填充域对象/实体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文