class CustomService(IProcessor processor) : ICustomService
The class CustomService
has a dependency that will be injected. There are multiple implementations for IProcessor
. Which implementation that should be used is determined by a setting that is stored in a database for the current user.
At first, I thought about implementing an IHandlerSelector
that would retrieve the setting from the database and determine which implementation to use. But there is business logic for cases where the setting is missing from the database, etc.... Since there is b开发者_JAVA技巧usiness logic, I'm not sure it would be correct to put this in the IHandlerSelector
.
I thought about creating another class called TopService
:
class TopService(ISettingsProvider provider, IProcessorFactory procFactory, ICustomServiceFactory serviceFactory)
TopService
uses two typed factories. It would use ISettingsProvider
to retreive the settings from the database and use the IProcessorFactory
to resolve the appropriate IProcessor
. After I have the correct IProcessor
, I would use the ICustomServiceFactory
factory to resolve CustomService
.
However, this seems a little off to me. Is there a better way to do this?
Why not just pass the IProcessorFactory
into the CustomerService
class? This would still isolate your CustomerService
class from the implementation of your IProcessorFactory
. That way if your business logic to select the proper IProcessor
changed in the future you could just create a new IProcessorFactory
.
I would imagine it being something like this:
public interface IProcessorFactory {
IProcessor GetProcessor();
IProcessor GetProcessor(string processorType);
}
Then in your CustomerService
class could call the correct GetProcessor()
method depending on whether or not the setting is provided for the customer.
精彩评论