Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to update audit fields? DDD

I have a Meeting Object:

Meeting{id, name, time, CreatedBy, UpdatedBy}

and a

MeetingAssignee{id, MeetingID, EmployeeId, CreatedBy, UpdatedBy)

Meeting, as Aggregate root, has a method AssignEmployee.

I was about to pass in the current user to the Meeting object as I call AssignEmployee, so that it can update its audit fields accordingly.

But this doesn't seem right - is it? Obviously I can keep the audit fields public and change them later - perhaps at service level?

What is everyone's else preferred method for updating these fields?

Please note: We are not using Nhibernate, but a custom ORM which does not have anything automatic in place.

Thanks.

like image 827
user676767 Avatar asked Aug 31 '12 15:08

user676767


2 Answers

Auditing and logging are fun as they are usually needed everywhere in the application and they are both requirements (logging is a requirement from the OPs guys).

Without knowing much of your model, and since auditing must be a requirement, I would pass the current user to AssignEmployee and instead of having a line there that says AuditBlahBlahBlah, I would add an event (maybe MeetingUpdated or AssigneeAdded... you'll find a good name) and that event gets dispatched to the class that does the auditing. In this way the Meeting class has no clue about auditing and dispatches business events for auditing purposes (which, in my view, is very DDDish).

I wonder what other people might say (hopefully, I can learn something new!)

like image 109
Augusto Avatar answered Oct 11 '22 14:10

Augusto


Consider using domain events.

Everything interesting in your domain model should raise an event shouting aloud of what just have happened. From outside, just attach log handler that dumps them in db or somewhere else.

That way - you don`t need to mess up your domain with some kind of IAuditService's.

Even better - domain model can use eventing as a way to communicate inside of itself.
To show why that`s a good idea - visualize that we are describing domain model of morning, sunrise and flowers.

Is it responsibility of sun to tell all the flowers that they should open? Not really. Sun just needs to shine brightly enough (to raise an event), light must travel down to earth (there must be some kind of infrastructure that makes eventing possible) and flowers must react themselves when receiving light (other domain models should handle events).

Another analogy - it's responsibility of driver to see what's the color of traffic lights.

like image 36
Arnis Lapsa Avatar answered Oct 11 '22 16:10

Arnis Lapsa