0

my Problem: i have got a collection with items, which must have a unique Name (choosen by the Operator). If an item is added to the collection, i would like to check if the Name is already used. So i have a consistence boundary around all items -> my aggregate root.

So i create an item parent aggregate root which implements the collection of items and checks if the item names are correct (unique), manages delete item/add item/validate item. This Aggregate parent root is stored in the repository. But this Aggregate parent is not a collection!? Do i have the wrong design!? My Aggregate root is only for Managing the items and i do not really need a database table for this parent (cause it is a singleton). Have i missed a Point!?

Thank you!

Stephan

Step
  • 1

3 Answers3

0

Whether you have a database table or not is not a concern for defining an Aggregate root, if you can reconstruct the Aggregate root based on its contents, here items, it is sufficient. E.g for a smallest Aggregate root you need a table mostly to store its ID, the entities inside it are any how stored separately, but in your case you are saying it is a singleton so you don't need a table to store the ID, a hard coded ID might be enough.

Update

If your aggregate is there just to validate the uniqueness then you may consider other solutions, check this answer

msmani
  • 730
  • 1
  • 7
  • 24
  • Thank you very much for your quick Response, wolverine. I designed it this way, BUT: I get my parent Aggregate from my repository and construct this aggregate with the db set items, means: IRepository.GetParentAggregate and inside i create "new Parent(DbSet ItemsCollection). Now if i manipulate my items in the collection or child items in the collection EntityFramework does recognize these changes automatically, when i commit my Transaction. But if i delete an item the Framework does not detect it, cause it is only deleted from the parent Aggregate which is not in the database. – Step Nov 22 '17 at 11:38
  • Do i Need to set a deleted flag and then later enumerate through this collection and set the EntityState to deleted, to let the entity Framework detect the deletion!? Not realy nice... :,-( – Step Nov 22 '17 at 11:47
  • Something similar, you may need to implement [UOW pattern](https://martinfowler.com/eaaCatalog/unitOfWork.html), BTW my answer was about whether an Aggregate root needs a table or not, in your case you have to evaluate whether you have modeled it correctly, if you are sure that the collection of items is indeed an Aggregate root then UOW might be the solution. – msmani Nov 22 '17 at 12:08
  • I am using the unit of work :-]...but the entity Framework does not detect the changes....thank you ver much....just so your update and will look at the provided link...THANK YOU! – Step Nov 22 '17 at 12:50
  • My solution (not the best): I am using my Aggregate parent, which is not in the database. When i delete a child item ('connected Scenario')' it is not detected cause my parent Aggregate is not tracked -> in my unit of work (i am using Mehdime.Entity package for that) i call now IRepositor.Update(Parent) and then i compare the dbset items with my parent items -> one item es missing -> i set entity state to deleted. – Step Nov 22 '17 at 14:27
0

My Aggregate root is only for Managing the items and i do not really need a database table for this parent (cause it is a singleton). Have i missed a Point!?

It seems you missed the Ubiquitous Language part of the problem.

FooCollection or FooManager are usually not good names for an Aggregate. You need to model aggregates around meaningful business concepts from your problem domain.

If an aggregate has no reason to be other than as a bag of Foo's, doesn't have an identity and has no other data you want to persist, you probably misidentified it or you're missing an important concept in your design.

guillaume31
  • 13,738
  • 1
  • 32
  • 51
0

Create Aggregate with data from repository with only parts of the aggregate stored in the Repository:

https://vaughnvernon.co/?p=879

Step
  • 1