我正在構建一個博客引擎來測試一些概念,如TDD和使用控制反轉...我想出了一個設置,我有一個通用的IBlogRepository接口,所以我可以同時支持SQL和XML。如何在像這樣的設置中使用IoC?有沒有更好的方法來測試這個?
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);
}
的問題是XML庫比一個做數據庫需要不同的資源......這裏是構造函數:不需要由SQL博客庫
public XmlBlogRepository(string dataPath, IFileReader fileReader, IDirectoryReader directoryReader, ILogger logger)
{
this.dataPath = dataPath;
this.fileReader = fileReader;
this.directoryReader = directoryReader;
this.Logger = logger;
}
的FileReader,directoryReader,和數據路徑。
這使得控制反轉加載IBlogRepository成爲不可能的問題,並且由於它們具有完全不同的ctors,所以它通常很難一般地使用它們。
我必須通過它們的原因是因爲如果我只是使用File.Open/Directory.GetFiles,那麼我將無法在硬盤上沒有XML文件的情況下單元測試該類。
你用什麼工具測試它? – 2009-01-31 08:03:33