如何在这样的设置中使用IoC?有没有更好的方法来测试这个?

时间:2021-06-26 12:37:09

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.

我正在构建一个博客引擎来测试一些概念,例如我们的TDD和使用控制反转...我想出了一个设置,我有一个通用的IBlogRepository接口,所以我可以支持SQL和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:

问题是XML存储库需要不同于数据库的资源...这里是构造函数:

    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.

SQL Blog存储库不需要fileReader,directoryReader和dataPath。

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.

这使得进行控制反转以加载IBlogRepository的问题变得不可能,并且由于它们具有完全不同的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.

我必须传递它们的原因是因为如果我只使用File.Open/Directory.GetFiles那么我将无法在没有硬盘驱动器上的XML文件的情况下对该类进行单元测试。

2 个解决方案

#1


I don't understand why the fact that your two IBlogRepository implementations have different ctor signatures makes IoC "impossible"; on the contrary, this is a common scenario in IoC. The way it's addressed is that your IoC framework (and only your IoC framework) is responsible for calling the ctor of the desired IBlogRepository implementation. So your IoC configuration file (or whatever other config approach you use) knows which implementation is being used (something has to, after all), but the consumers of the IBlogRepository service only access it via its interface.

我不明白为什么你的两个IBlogRepository实现具有不同的ctor签名这一事实使得IoC“不可能”;相反,这是IoC中的常见情况。它解决的方法是你的IoC框架(以及你的IoC框架)负责调用所需的IBlogRepository实现的ctor。因此,您的IoC配置文件(或您使用的任何其他配置方法)知道正在使用哪个实现(毕竟必须这样),但IBlogRepository服务的使用者只能通过其接口访问它。

#2


i think a provider will fit your needs by implementing a provider you will wrap the resources in it

我认为提供商将通过实施提供商来满足您的需求,您将把资源包装在其中

here a nice link

这是一个很好的链接

#1


I don't understand why the fact that your two IBlogRepository implementations have different ctor signatures makes IoC "impossible"; on the contrary, this is a common scenario in IoC. The way it's addressed is that your IoC framework (and only your IoC framework) is responsible for calling the ctor of the desired IBlogRepository implementation. So your IoC configuration file (or whatever other config approach you use) knows which implementation is being used (something has to, after all), but the consumers of the IBlogRepository service only access it via its interface.

我不明白为什么你的两个IBlogRepository实现具有不同的ctor签名这一事实使得IoC“不可能”;相反,这是IoC中的常见情况。它解决的方法是你的IoC框架(以及你的IoC框架)负责调用所需的IBlogRepository实现的ctor。因此,您的IoC配置文件(或您使用的任何其他配置方法)知道正在使用哪个实现(毕竟必须这样),但IBlogRepository服务的使用者只能通过其接口访问它。

#2


i think a provider will fit your needs by implementing a provider you will wrap the resources in it

我认为提供商将通过实施提供商来满足您的需求,您将把资源包装在其中

here a nice link

这是一个很好的链接