<分区>
我从依赖注入(inject)开始,很难阻止一些第三方库类。例如,我的项目中有 EPPlus 库,它有不实现接口(interface)的 ExcelRange 类。由于我正在使用这个库,我发现我的代码是明确依赖的,无法正确地对代码的某些部分进行单元测试。
所以我的问题是什么是对第 3 方库类使用依赖注入(inject)的好方法。
<分区>
我从依赖注入(inject)开始,很难阻止一些第三方库类。例如,我的项目中有 EPPlus 库,它有不实现接口(interface)的 ExcelRange 类。由于我正在使用这个库,我发现我的代码是明确依赖的,无法正确地对代码的某些部分进行单元测试。
所以我的问题是什么是对第 3 方库类使用依赖注入(inject)的好方法。
最佳答案
对于这种情况,我的解决方案是创建另一个类和接口(interface)作为第三方库的包装器。在您的包装器中,创建与您在第三方库中使用的名称相同的函数。只创建那些对您的代码有值(value)的函数,如果您需要其他函数,请一点一点地添加到您的包装器中。现在,出于测试目的,您可以在不使用第三方库的情况下模拟/ stub 您的包装器接口(interface)。使用您的包装器注入(inject)到需要此服务的其他类。
您可以从简单的代码开始,然后随着知识的增长扩展它:
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;
}
.
.
}
对于依赖注入(inject),您可以使用 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 的观点,您需要阅读一些关于四人帮 (GOF) 的内容来改进此示例。以我的示例为起点。
包装您的第三方库可为您带来以下优势:
一些缺点:
关于c# - 包装第 3 方类 c# 的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31530165/