开发者

Aggregate Pattern and Performance Issues

开发者 https://www.devze.com 2022-12-25 09:34 出处:网络
I have read about the Aggregate Pattern but I\'m confused about something here. The pattern states that all the objects belonging to the aggregate should be accessed via the Aggregate Root, and not di

I have read about the Aggregate Pattern but I'm confused about something here. The pattern states that all the objects belonging to the aggregate should be accessed via the Aggregate Root, and not directly.

And I'm assuming that is the reason why they say开发者_开发百科 you should have a single Repository per Aggregate.

But I think this adds a noticeable overhead to the application. For example, in a typical Web-based application, what if I want to get an object belonging to an aggregate (which is NOT the aggregate root)? I'll have to call Repository.GetAggregateRootObject(), which loads the aggregate root and all its child objects, and then iterate through the child objects to find the one I'm looking for. In other words, I'm loading lots of data and throwing them out except the particular object I'm looking for.

Is there something I'm missing here?

PS: I know some of you may suggest that we can improve performance with Lazy Loading. But that's not what I'm asking here... The aggregate pattern requires that all objects belonging to the aggregate be loaded together, so we can enforce business rules.


I'm not sure where you read about this "Aggregate Pattern". Please post a link.

One thing it could be is where we encapsulate a list into another object. For a simple example if we have a shopping cart instead of passing around a list of purchases we use a cart object instead. Then code that works on the whole cart (eg. getting total spend) can be encapsulated in the cart. I'm not sure if this is really a pattern but google found this link: http://perldesignpatterns.com/?AggregatePattern

I suspect when you say

"The aggregate pattern requires that all objects belonging to the aggregate be loaded together, so we can enforce business rules. "

this depends on your business rules.

Generally patterns should not be regarded as a set of rules you must follow for everything. It is up to you as a developer to recognise where they can be used effectively.

In our cart example we would generally want to work on the whole cart at once. We might have business rules that say our customers can only order a limited about of items - or maybe they get a discount for order multiple items. So it makes sense to read the whole thing.

If you take a different example eg. products. You could still have a products repository but you needn't not load them all at once. You probably only ever want a page of products at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消