Domain Services don't typically decide to raise events on their own -- that sort of logic is normally reserved for the domain model itself. For example, if we change the example slightly
public Account ViewAccount(Guid Id) { var account = repository.GetById(Id); if (null != account) { dispatcher.Raise(new AccountViewedEvent(account)); } return account; }
you can get a sense for the fact that it's a model's decision that an event is appropriate; the dispatcher is just implementing the capability.
It seems like a lot of layers to have an application service call a domain service that calls a repository.
Yeah, that sounds too elaborate.
The right way to go really depends on the motivation for the requirement. What is the business value of the logging views? Do you have a model whose correct behavior depends on previously logged values?
If you've got a "write only" log, then domain events are probably overkill; as is the repository. Create an interface for the logging capability at the correct abstraction level, and just wire it directly into your logging framework.
On the other hand, if you've got requirements for doing interesting things with the AccountViewedEvents after they have been logged, then you've probably got an implicit model that should be dragged into the light, and start asking questions like "is this part of the same domain that accounts live in, or is it something else?"
I don't know about doing interesting things with the events, but it's to satisfy HIPAA requirements and extremely important. This is why I wanted to make it part of the domain
That's definitely something you want to make explicit
public Account ViewAccount(Guid Id) { var account = repository.GetById(Id); if (null != account) { hipaaCompliance.recordForAudit(new AccountViewedEvent(account)); } return account; }
You may even want to design your API such that you can only access the Account via the HIPAA compliance module.
staticlogger object, using Tracing and Instrumentation in the .NET Framework, or otherwise implementing a typicalILoggerimplementation?