简体   繁体   English

依赖注入和单元测试 - 静态辅助方法或私有实例方法

[英]dependecy injection and unit testing - static helper methods or private instance methods

From unit testing and dependency injection point of view, what's the usual adopted norm when it comes to helper methods? 从单元测试和依赖注入的角度来看,在辅助方法方面通常采用的规范是什么?

Here is my example situation: 这是我的示例情况:

public class GoodiesController : Controller
{
   private IMyContext _context;

   public GoodiesController(IMyContext context)
   {
      _context = context
   }

   public async Task<IAction> GetThoseGoodies()
   { 
       if(YouLikeThemThisWay(Request.Path))
        {
           var result = await _context.GoGetThemThisWay()
        } else { }
    }

My question is am I better off with YouLikeThemThisWay(string path) as a static helper in some class or as a private instance method? 我的问题是,我最好将YouLikeThemThisWay(string path)作为某个类中的静态助手或作为私有实例方法吗? Assuming I might have a couple of the likes of YouLikeThemThisWay ? 假设我可能有几个像YouLikeThemThisWay的喜欢?

It really depends on what your YouLikeThemThisWay(string path) method does. 这实际上取决于你的YouLikeThemThisWay(string path)方法的作用。 My rules for using a static method or as follows: 我使用静态方法的规则如下:

  1. Does it require a non-primitive dependency? 它是否需要非原始依赖? If so, don't use static. 如果是这样,请不要使用静态。
  2. Does it affect the state of the application? 它会影响应用程序的状态吗? If so, don't use static. 如果是这样,请不要使用静态。
  3. Does it extend the functionality of a class or type you do not have access to internally (IE BCL classes or primatives)? 它是否扩展了您无法在内部访问的类或类型的功能(IE BCL类或灵活性)? If so use a static extension! 如果是这样,请使用静态扩展!
  4. Will it impact unit tests--as in make them more difficult--if I cannot mock the routine? 它是否会影响单元测试 - 如果让它们变得更加困难 - 如果我不能模拟例程? If no, then make it static! 如果不是,那就让它静止!
  5. Will it be used by more than one type or class? 它会被多种类型或类使用吗? If so that it makes it a better candidate for static! 如果是这样,它使它成为更好的静态候选者!
  6. Does the routine perform some sort of IO, like calling a database or the filesystem? 例程是否执行某种IO,如调用数据库或文件系统? If so, I would not make it static. 如果是这样,我不会让它变得静止。

Basically, small helper functions that are easily tested and don't affect state or usually OK to make static. 基本上,小辅助函数很容易测试,不会影响状态或通常可以静态。 If there is state involved, the routine requires a dependency that you would normally inject, or the routine is making IO or IPC calls then do not make it static. 如果涉及到状态,则例程需要您通常会注入的依赖项,或者例程正在进行IO或IPC调用,然后不要使其静态。

One caveat to the dependency issue is technically you could use method injection to handle the dependencies, but I like to keep it simple. 对依赖性问题的一个警告是技术上你可以使用方法注入来处理依赖项,但我喜欢保持简单。 Your method is probably OK to be static. 你的方法可能是静态的。

Reuse is a big factor in statics too. 重用也是静力学中的一个重要因素。 If the routine will only be used in one class, it may be pointless to make static. 如果例程只在一个类中使用,那么静态化可能毫无意义。 Most of my static methods live in helper classes that are easily accessed anywhere. 我的大多数静态方法都存在于任何地方都可以轻松访问的辅助类中。

EDIT: Note that I usually require most or all of those five rules to favor statics in order for me to even consider making something static. 编辑:请注意,我通常要求这五条规则中的大部分或全部都支持静态,以便我甚至考虑制作静态的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM