I have 3 classes:
class SqlQueryService : IQueryService
class FileQueryService : IQueryService
class NCRFileQueryService : FileQueryService
I have create an interface factory:
public interface IQueryServiceFactory
{
IQueryService Create(string connection);
}
In the application module:
Bind(typeof(IQueryService)).To(typeof(SqlQueryService)).Named("Data Source");
Bind(typeof(IQueryService)).To(typeof(NCRFileQueryService)).Named("NCR File Source");
Bind(typeof(IQueryService)).To(typeof(FileQueryService)).Named("File Source");
Bind<IQueryServiceFactory>().ToFactory();
In my app I want to create an instance of one of three classes based on a parameter like the following:
IQueryService queryService =
_queryServiceFactory.Create(_configuration.SelectedTPV.Connection);
- If the string parameter starts with "Data Source" create a
SqlQueryService
- If the string parameter starts with "File Source" create a
FileQueryService
- If the string parameter starts with "NCR File Source" create a
NCRFileQueryService
It's possible to do that?
*Note: My app is a winforms app with .NET Framework 3.5 because is for and OLD windows
The Ninject version that I use is 3.2.2.0 and the version of the Ninject Extensions Factory is 3.2.1.0
_configuration.SelectedTPV.Connection
a fixed value that doesn't change at runtime, or does that value actually change while the application is running? In either case however, you don't need the factory. RemovingIQueryServiceFactory
will improve your design. - Steven