Would like to start off by saying, I am asking this as a purely academic exercise; I am quite happy to just continue to use the repository pattern.
I have heard "I put persistence methods in my business objects because when to persist was an integral part of the business logic" which I do not think is a valid reason, because that just means your business object needs a TimeToSave event
But one could argue, if you use the strategy pattern for the persistence methods, you are technically not including persistence logic in your domain layer. See example below:
class MyEntity
{
private ISaveMyEntity _saver;
public MyEntity(ISaveMyEntity saver) { _saver = saver; }
public void Save()
{
_saver.Save(this);
}
//Real business methods
}
Including with a business object, its own persistence methods, could prove convenient. Furthermore, the repository pattern is often abused and ends up containing all the business logic instead of just CRUD, while the business object turns into DTOs; in my opinion, this is worse than a business object containing CRUD methods.
What could be the potential pitfalls of including access to persistence in a business object via the strategy pattern? Would use of the strategy pattern mitigate some of the problems commonly associated with storage code in a business object?
Save
you are just raising, as you say, a TimeToSave event. However, a smart persister is going to batch this request, cache it, and may in fact decide not to call it because some other state has changed. This could lead to confusing code where from the entity's point of view it has been persisted but it actually hasn't been. I'd be curious to see your intended usage pattern.