静态存储库...无法使用DI,怎么办?

发布于 2024-09-15 09:32:19 字数 196 浏览 9 评论 0原文

我遇到的情况是,我们需要修改从第三方开源应用程序 (NopCommerce) 中的静态存储库返回的内容。问题是它们使用静态存储库,所以我不能仅仅继承一个接口并 DI 我自己的存储库。我试图在不修改 NopCommerce 代码库的情况下做到这一点......有什么新想法吗?

编辑:我希望 NopCommerce 使用我的存储库,而不是让我的代码使用他们的代码。

I am in a situation where we need to modify what is being returned from the static repository in a 3rd party open-source application (NopCommerce). The problem is that they use static repositories, so I can't merely inherit an interface and DI my own repository. I'm trying to do this without modifying the NopCommerce code-base... any fresh ideas?

Edit: I want NopCommerce to use my repos, rather than have my code use theirs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2024-09-22 09:32:19

您可以通过创建自己的接口和委托给 NopCommerce 的类实现来抽象掉他们的东西。然后让您的代码使用该接口,而不是直接访问 NopCommerce 的类。在结果返回到应用程序之前,您可以在类中修改 NopCommerce 的输出。

作为额外的好处,您还可以模拟接口来执行一些不需要成熟的存储库实现的测试。

像这样的代码:

public interface IRepository 
{
   MyItem GetItem(int id);
}

public class MyNopCommerceWrapper : IRepository
{
   public MyItem GetItem(int id)
   {
       // I have no idea what NopCommerce API looks like, so I made this up.
       var myItem = NopCommerce.GetItem(id);

       ModifyMyItem(myItem);

       return myItem;
   }
}

You could abstract away their stuff by creating an interface of your own and a class implementation that delegates to NopCommerce. Then have your code use the interface instead of directly accessing NopCommerce's classes. You can modify the output of NopCommerce inside your class before the result is returned to your application.

And as an added bonus you could also mock the interface to do some tests that didn't require the full-blown repository implementations.

Something like this, in code:

public interface IRepository 
{
   MyItem GetItem(int id);
}

public class MyNopCommerceWrapper : IRepository
{
   public MyItem GetItem(int id)
   {
       // I have no idea what NopCommerce API looks like, so I made this up.
       var myItem = NopCommerce.GetItem(id);

       ModifyMyItem(myItem);

       return myItem;
   }
}
半仙 2024-09-22 09:32:19

我们目前的期限非常非常紧迫,而且这个问题是没有预见到的。所以我想首先从穷人的静态接口/穷人的 DI 开始,如下所示(这样我就不必修改整个解决方案)。然后稍后,当我们时间不那么紧迫时,改用接口和依赖注入并向 NopCommerce 提交补丁:

// Poor-man's static interface (DI).
public static class OriginalBuiltInStaticClass {
    private static IMyNewClass _myNewClass;

    public static void Inject(IMyNewClass myNewClass) {
        _myNewClass = myNewClass;
        A = _myNewClass.A;
        B = _myNewClass.B;
        C = _myNewClass.C;
    }

    public static Action A = CopySimpleRenameBuiltInStaticClass.A;
    public static Func<int, string> B = CopySimpleRenameBuiltInStaticClass.B;
    public static Action C = CopySimpleRenameBuiltInStaticClass.C;
}

// Original vendor class which was copied and renamed.
public static class CopySimpleRenameBuiltInStaticClass {
    public static void A() {
        Console.WriteLine("OriginalBuiltInStaticClass.A()");
    }

    public static string B(int id) {
        Console.WriteLine("OriginalBuiltInStaticClass.B()");
        return id.ToString();
    }

    public static void C() {
        Console.WriteLine("OriginalBuiltInStaticClass.C()");
    }
}

// Creating an interface to merge into trunk of NopCommerce (convert static repositories)
public interface IMyNewClass {
    void A();
    string B(int id);
    void C();
}

// Implementation of interface.
public class MyNewClass : IMyNewClass {
    public void A() {
        Console.WriteLine("MyNewClass.A()");
    }

    public string B(int id) {
        Console.WriteLine("MyNewClass.B()");
        return id.ToString();
    }

    public void C() {
        CopySimpleRenameBuiltInStaticClass.C();
    }

}

有什么想法吗?

We are currently on a really, really tight deadline, and this problem was not forseen. So I am thinking of first starting with a poor man's static interface/poor man's DI like the following (so I don't have to modify the entire solution). Then at a later time, when we are not-so-pressed for time, change over to use an interface and dependency injection and submit a patch to NopCommerce:

// Poor-man's static interface (DI).
public static class OriginalBuiltInStaticClass {
    private static IMyNewClass _myNewClass;

    public static void Inject(IMyNewClass myNewClass) {
        _myNewClass = myNewClass;
        A = _myNewClass.A;
        B = _myNewClass.B;
        C = _myNewClass.C;
    }

    public static Action A = CopySimpleRenameBuiltInStaticClass.A;
    public static Func<int, string> B = CopySimpleRenameBuiltInStaticClass.B;
    public static Action C = CopySimpleRenameBuiltInStaticClass.C;
}

// Original vendor class which was copied and renamed.
public static class CopySimpleRenameBuiltInStaticClass {
    public static void A() {
        Console.WriteLine("OriginalBuiltInStaticClass.A()");
    }

    public static string B(int id) {
        Console.WriteLine("OriginalBuiltInStaticClass.B()");
        return id.ToString();
    }

    public static void C() {
        Console.WriteLine("OriginalBuiltInStaticClass.C()");
    }
}

// Creating an interface to merge into trunk of NopCommerce (convert static repositories)
public interface IMyNewClass {
    void A();
    string B(int id);
    void C();
}

// Implementation of interface.
public class MyNewClass : IMyNewClass {
    public void A() {
        Console.WriteLine("MyNewClass.A()");
    }

    public string B(int id) {
        Console.WriteLine("MyNewClass.B()");
        return id.ToString();
    }

    public void C() {
        CopySimpleRenameBuiltInStaticClass.C();
    }

}

Any thoughts?

情深如许 2024-09-22 09:32:19

听起来像是 Facade 的工作。

Sounds like a job for Facade.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文