I have a data access layer, which currently communicates with a database.
public interface IDao<T> // T is my DTO
{
Write(IEnumerable<T> dtosToPersist)
}
public class Dao<T> : IDao<T>
{
private readonly IBulkCopySaver<T> _bulkSaver;
private IEnumerable<T> _buffer;
public Dao(IBulkCopySaver<T> bulkSaver)
{
_bulkSaver = bulkSaver;
_buffer = new List<T>();
}
public void Write(IEnumerable<T> dtosToPersist)
{
// implementation logic.
}
}
I would like to also add persistence into XML for example. My goal is to replace the Dao with a new object implementing IDao, changing calling code as little as possible (Dao is massively used). I'm using a dependancy injector and the golden goose would be to just switch from database to xml persistence just by changing the injection interface/concrete type bindings.
The problem for me is writing in the XML case would need more information, I would need the folder path for example. Also that folder path is determined at runtime from program arguments and the type T. The interface contract of my data access object is not satisfying as it only takes an IEnumerable.
What are my options here ?
My first idea is to make the dependancy injector, aware of the filepath parameter and inject it as constructor parameter. Supposing the folderPath is in a context local field of the injector, using Ninject :
Bind<IDao<DtoConcreteType>.To<Dao<DtoConcreteType>
Bind<IDao<DtoConcreteType>.To<XmlDao<DtoConcreteType>.WithConstructorArguments("filePath", context.folderPath)
However this feels a bit wrong to me. The injection framework will do reflection under the hood and I feel there should be a OOP way to solve this.
Another idea as I was writing this bits is that maybe the XmlDao.Write may be calling a provider object (injected) that would give it the details it lacks (an IFilePathProvider that knows program arguments and makes decision based on type T). This seem way more simple
What is your opinion ? Do you have an idea for this ? What about the ideas I had ?