混合值对象/实体类型?

发布于 2024-11-19 03:02:08 字数 1095 浏览 6 评论 0原文

是的,我同意标题表明我目前的思路是有缺陷的。但为了再次恢复理智,以下是我如何陷入这种令人遗憾的状态的。

主题对象是一个地址,主题应用程序在三个区域办事处有数百名员工。在这种情况下,将 (3) 个地址视为实体对象并仅将这三个地址保留在数据库中非常有用。

当然,员工有工作,因为该应用程序为数千名客户提供服务。在这种情况下,将 Address 视为 ValueObject 更有用。

现在我想要一个聚会模式来维护公司和人员所具有的共同特征,例如......地址。

最后一个事实 - 我们有一个 ValueObject 类,它可以完成您可能期望从值对象中获得的操作(即,基于公共属性确定相等)和一个 Entity 类(如果对象是持久的,则基于某些 id 相等)。

那么,该怎么办呢?在我想问这个问题并看看得到什么样的答案之前,我正在考虑以下课堂想法...

class Address : ValueObject, IHaveAddress{
    Line 1 {get;}
    ... etc
    Address {get{return this;}} // this has an awkward smell
}

class EntityAddress : Entity<int>, IHaveAddress{
    Address {get;}
}

interface IHaveAddress {
    Address {get;}
}

class Party : Entity<int> {
    IList<IHaveAddress> _address;
}

这样的想法有什么优点吗?

干杯,
Berryl

回答

是的,我的想法有缺陷:-)
我正在寻找的对象是一个实体,在下面的模型的上下文中。

感谢 IAbstract 和这个精彩的链接

在此处输入图像描述

Yes, I agree the title suggests my current line of thinking is defective. BUT in the hopes of again arriving at sanity, here's how I got to this sorry state.

The subject object is an Address, and the subject application has hundreds of Employees in three regional offices. In this context, it is useful to treat the (3) addresses as Entity objects, and keep only those three Addresses in the database.

OF course the Employees have employment because the application services thousands of Customers. In this context, it is more useful to treat the Address as a ValueObject.

Now I'd like to have a Party pattern to make the maintenance of common traits that Companies and People have, like...Addresses.

Last factoid - we have a ValueObject class that does things you might expect from value objects (ie, determine equality based on public properties) and an Entity class (equality based on some id if the object is persistent).

So, what to do? I was playing with the following class ideas before I thought I'd ask this and see what kind of answers came back...

class Address : ValueObject, IHaveAddress{
    Line 1 {get;}
    ... etc
    Address {get{return this;}} // this has an awkward smell
}

class EntityAddress : Entity<int>, IHaveAddress{
    Address {get;}
}

interface IHaveAddress {
    Address {get;}
}

class Party : Entity<int> {
    IList<IHaveAddress> _address;
}

Is there any merit to such an idea??

Cheers,
Berryl

ANSWER

Yes, my thinking was defective :-)
The object I was looking for is an Entity, in the context of the model below.

Thanks to IAbstract and this wonderful link

enter image description here

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

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

发布评论

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

评论(1

蝶舞 2024-11-26 03:02:08

这是否有帮助:MSDN 博客 ... 实现值对象基类?我想我找到了一些新东西......

更新我不确定这有多少答案
...我认为使用标准的“复合”术语比“派对”更容易混淆。实际上,您有一个公司(根),一组地址(或分支机构),许多人(或离开)在其中工作。

我还不确定 ValueObject 实际上有多少。从表面上看,超类型对于比较 Address 对象很可能很有用。不过,这似乎有点倒退的感觉。也就是说,在您的示例中,存储 1 个 Address 对象并将 Person 作为复合对象添加到分支 Address 肯定更有意义。

正如我之前所说,我有一个个人项目,在其中我将把 ValueObject 与一些 Composite 对象一起使用,以发现它的有用性。您对 Entity 的引用是否与实体框架的任何实现相关?或者 Entity 只是为了引用记录 ID 号?

Does this help: MSDN blogs ... Implementinag a Value-Object Base Class? I think I've found something new to play with ...

Update I'm not sure how much of an answer this is
...I think it is much less confusing to stay with the standard 'Composite' terminology rather than 'Party'. Effectively you have a Company (root), a set of Addresses (or branches) at which many People (or leaves) work.

I'm not sure, yet, just how much value the ValueObject actually has. On the surface, the supertype may well prove useful to compare Address objects. This seems to have a somewhat backwards feel to it, though. That said, it certainly makes more sense in your example to store 1 Address object and add Person as composite objects to the branch Address.

Like I said earlier, I have a personal project in which I will put the ValueObject to work along with some Composite objects to discover it's usefulness. Is your reference to an Entity related to any implementation of Entity Framework? Or is Entity<int> simply intended to reference a record ID number?

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