Re: Should entities save/update themselves?
johnygold <johnygold <at> yahoo.com>
2008-04-02 14:50:57 GMT
David,
When your DB change - nothing changes in the Domain. It's the IDao
implementaion (located in different package/project) which changes.
Al,
1. About seperation of concern:
Theoretically entities deal only with the Domain logic ("isolate the
domain"), but practically sometime there is no escape from technical
tasks. A real life example i had: Product entity which has "validate"
method - it performs some business logic , then checks that all it's
resources (images, custom files) are indeed on the physical disc,
then continue with the business logic. The trade-off is between
(a) pure seperation of concern, preventing the Product entity from
checking the disc and loosing it's ability to validate itself and
perform atomic operation. It can indeed throw an event when the disc
should be checked but then part of the validation will be external.
After throwing an event, the entity will not know if the disc
validation succeeded.
(b) atomic operation with minimal mixing of concern, the product
entity is being inject with IPhysicalResourcesProvider which is being
used to check the disc. This way all the validation phase is
hapenning on the Product entity. And all it depends on is provider
Interfaces.
2. About saving/updating in many places:
You are right, the DB operation should be managed by some component
who has birds-eye view on the transaction, but i think this should be
the aggregate (Producy , in my case) and not the service. If the
transaction is being shared by few aggregates, why won't you pass
ITransaction to each aggregate?
Yoni
--- In domaindrivendesign <at> yahoogroups.com, "Alasdair Gilmour"
<alasdairg@...> wrote:
>
> IMHO, its just down to separation of concerns and the Single
Responsibility
> Principle: A Product entity represents a specific business concept
in the
> domain. Saving an object's state to the database is a completely
orthogonal
> responsibility that is nothing to do with "being a Product", so it
doesn't
> conceptually belong within the Product entity.
>
> Moreover, within an application, an entity can be changed from many
> different places as part of many different use cases. Since an
entity can
> not know what particular application use case it is being
manipulated for,
> how can it know when it is the right time to call save? As a very
simple
> example, if a use case were to change a Product's name and then
immediately
> its product number, I wouldn't want to call save() on both the name
change
> and the product number change - but just once when all the changes
for that
> particular use case had been made ....
>
> Anyway, just my 2c
>
> Al
>
> > Thanks for your replies, i'm still not sure why entities should be
> > unaware of the need to save/commit themself after some change.
> > From the rational point, it seems logical to me for an entity to
call
> > "save()" on some provider when some change is finished (for
example
> > when product entity finish to change its name, it calls IDao.save.
>
------------------------------------