I'm building a blog engine to test some concepts such us TDD and using inversion of control... I came up with a setup where I'd have a generic IBlogRepository interface so I could support both SQL and XML.
public interface IBlogRepository : IRepository
{
Post GetPostById(Guid postId);
Post GetPostByFriendlyUrl(string friendlyUrl);
List<Post> GetPostsByAuthor(string userName);
List<Post> GetPostByTags(params string[] tags);
List<Author> Authors { get; }
void Delete(Post post);
void Delete(Comment comment);
void Save(Post post);
void Save(Comment comment);
}
The problem is the XML repository needs different resources than the database one does... here is the constructor:
public XmlBlogRepository(string dataPath, IFileReader fileReader, IDirectoryReader directoryReader, ILogger logger)
{
this.dataPath = dataPath;
this.fileReader = fileReader;
this.directoryReader = directoryReader;
this.Logger = logger;
}
fileReader, directoryReader, and dataPath are not needed by the SQL Blog Repository.
This makes the issue of doing inversion of control to load the IBlogRepository impossible and it also makes it really hard to generically use them since they have completely different ctors.
The reason I have to pass them in is because if I just used File.Open/Directory.GetFiles then I wouldn't be able to unit test the class without having XML files on the hard drive.