Hi I am using an IoC container, and I would like to initialize a service (part of which includes "hard work" talking to the database) inside the constructor.
This particular service stores information found by the injected IPluginToServiceProviderBridge
service, this information is stored in the database through UnitOfWork
.
Once everything is loaded, controllers with commands and services with handlers are used for all other interactions. All commands are wrapped within the validity period, so saving and deleting UnitOfWork
is done by the handler, not the service (this is great for clean code).
The same accuracy and separation of problems for saving and transactions does not apply to the Initializer
inside the service, since everything happens in the constructor:
public PluginManagerService( IPluginToServiceProviderBridge serviceProvider, IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; this.serviceProvider = serviceProvider; lock (threadLock) { if (initialised == false) { LinkPluginsWithDatabase(); initialised = true; }
A few points:
Ideally, I want to avoid using a factory, since this complicates the processing of the validity of the region, I would be happy to reorganize for a better separation, if I knew how to do it.
I really want to avoid using a separate Init()
method for the service, while it will allow transactions and save with a command / handler, a lot of validation code will be required, and I believe that this will also lead to temporary problems.
Given the above, is it permissible to call UnitOfWork.Save()
in my constructor, or can I reorganize for cleaner code and better separation?