Hi Harry,
In your situation the difference between the DI and non-DI solutions I
described would probably involve passing 'now' into the domain object
methods that require the current time, rather than having the domain
objects get this value from the clock service. Not flash, I agree.
Service Locator is a sound pattern, but as soon as you introduce it
into a DI-managed application, you lose much of the predictability
that justifies using DI in the first place (classes no longer
advertise their dependencies through their constructor parameters and/
or properties.) Go too far down that path and you might as well do
service location throughout.
Tough situation to be in.
If it is at all possible, you might consider doing property injection
into your domain objects after all. It is feasible that this could be
done by hand for a few classes - e.g. Foo.TimeService =
container.Resolve<ITimeService>().
Otherwise, you need to find a location in your app from which you can
call container.InjectInto(myInstance) for each loaded domain object.
At the end of the day, the best solution might turn out to be Service
Locator, but exploring your options first is a good move.
Building a service locator with Autofac, BTW, is a bit trickier than
with other containers, because of the need to always use the innermost
nested container for dependency resolution. You can take a look at the
ASP.NET integration and its ContainerProvider class for some clues as
to how this should be done (your static Service Locator in the domain
layer has to be wired to something like IContainerProvider rather than
IContainer, so that when service location is performed the current
container can be used.)
Cheers,
Nick