ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

什么是包装第三方课程的最佳方式c#

2019-05-28 07:53:03  阅读:232  来源: 互联网

标签:c net dependencies code-injection


我开始使用依赖注入,并且很难挫败一些第三方库类.例如,我的项目中有EPPlus库,它有ExcelRange类,没有实现接口.由于我正在使用这个库,我发现我的代码明确依赖,无法正确单元测试代码的某些部分.

所以我的问题是什么是使用依赖注入与第三方库类的好方法.

解决方法:

我对此场景的解决方案是创建另一个类和接口作为第三方库的包装器.在您的包装器中,创建与您在第三方库中使用的名称相同的功能.只创建那些对您的代码有价值的函数,如果您需要其他函数,请在您的包装器中添加它.现在,出于测试目的,您可以在不使用第三方库的情况下模拟/存储您的包装器界面.使用您的包装器注入需要此服务的其他类.

您可以从简单的代码开始,随着知识的增长进行扩展:

public interface IWrapperService
{
    Method(Dto model);

    Dto MethodProcess(Dto model);
}

public class WrapperService : IWrapperService
{
    private readonly ThirdPartyLib _thirdPartyLib;

    public WrapperService(ThirdPartyLib thirdPartyLib)
    {
        _thirdPartyLib = thirdPartyLib;
    }

    // Create your model - Dto
    // Dto will help you in your logic process
    // 
    public void Method(Dto model)
    {   
        //extract some properties in you model that only needed in your third party library 
        _thirdPartyLib.Method(parameter needed);
    }

    public Dto MethodProcess(Dto model)
    {   
        //extract some properties in you model that only needed in your third party library 
        ThirdPartyReturn value = _thirdPartyLib.MethodProcess(parameter needed);

        // Do the mapping
        var model = new Dto 
        {
            property1 = value.property1 // Do the necessary convertion if needed.
            .
            .
        }

        return model;
    }
    .
    .
    .
}

public interface IOtherClass 
{
  ...
}

public class OtherClass : IOtherClass 
{
   private readonly IWrapperService _wrapperService;

   public void OtherClass(IWrapperService wrapperService)
   {
        _wrapperService= wrapperService;
   }
   .
   .
}

对于依赖注入,你可以使用Microsoft unity.它将为你的依赖做出惊人的工作.你可以像这样使用它:

var unity = new UnityContainer();

// This is how you will inject your ThirdPartyLib
// You can also do it this way - unity.RegisterType<ThirdPartyLib>() but of course we need to limit the usage of your ThirdPartyLib in 
// our wrapper. We will not allowing directly access to Third Party Lib rather than wrapperService.

unity.RegisterType<IWrapperService, WrapperService>(new InjectionConstructor(new ThirdPartyLib()));
unity.RegisterType<IOtherClass, OtherClass>();

我同意@Alexei Levenkov,你需要阅读一些关于Gang of Four(GOF)的内容来改进这个样本.以我的样本为出发点.

包装您的第三方库具有以下优势:

>它消除了对第三方库的分散和直接使用.
>它在您的第三方库中封装了一些复杂性.
>通过包装器可以轻松跟踪和维护第三方库.
>现在通过使用包装器可以轻松进行单元测试.
>依赖注入将帮助您解决交叉问题.

几个缺点:

>乏味并引入重复的方法.
>介绍新模型的创建 – 这取决于,如果你的第三方lib只询问一些参数,如(int,string,boolean),请不要打扰模型.
>首先应用设计模式可能很困难,但从长远来看它会给你带来优势.

标签:c,net,dependencies,code-injection
来源: https://codeday.me/bug/20190528/1169721.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有