如何做到依赖注入用C? [英] How to do dependency injection in C?

查看:130
本文介绍了如何做到依赖注入用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个良好的技术解决方案,在C做DI

I'm looking for a good technical solution to doing DI in C.

我已经看到了一些问题DI已经在这里,但我还没有见过任何实际的例子或具体实施意见。

I have seen some of the DI questions here already, but I haven't seen one with any actual examples or concrete implementation suggestions.

所以,让我们说,我们有以下情况:

So, lets say we have the following situation:

我们有一组在C模块;我们要重构的模块,这样我们就可以使用DI运行单元测试等。

We have a set of modules in c; we want to refactor those modules so that we can use DI to run unit tests and so on.

每个模块有效地由一系列C函数的:

Each module effectively consists of a set of c functions:

module_function(...);

module_function(...);

模块互相依赖。 IE浏览器。通常情况下,你可能有一个调用,如:

Modules depend on each other. Ie. Typically you may have a call such as:

int module1_doit(int x) {
  int y = module2_dosomethingelse(x);
  y += 2;
  return(y);
}

什么是正确的做法,以DI为这个?

What is the correct approach to DI for this?

可能的解决办法似乎是:

Possible solutions seem to be:


  • (1)使用函数指针的所有模块的功能,并调用当函数做到这一点(或类似):

  • (1) Using function pointers for all module functions, and when invoking a function do this (or similar):

INT Y =模块 - > module2-> dosomethingelse(X);

int y = modules->module2->dosomethingelse(x);

(2)编译与相同的符号多个库(模拟,标准等)和动态的正确实施联系起来。

(2) Compile multiple libraries (mock, std, etc.) of with the same symbols and dynamically link in the correct implementation.

(2)似乎是这样做的正确的方式,但难以配置和烦人迫使你建立多个二进制文件为每个单元测试。

(2) seems to be the correct way of doing it, but is difficult to configure and annoyingly forces you to build multiple binaries for each unit test.

(1)好像它可能工作,但在某些时候你DI控制器将会停留在你需要动态调用的通用工厂函数(无效的的工厂的情况)( ...)表示)与一些需要在运行时被注入其他模块的?

(1) Seems like it might work, but at some point your DI controller is going to get stuck in a situation where you need to dynamically invoke a generic factory function (void ( factory) (...) say) with a number of other modules that need to be injected at runtime?

有另一种,在C这样做的更好的办法?

Is there another, better way of doing this in c?

什么是做的正确的方式?

What's the 'right' way of doing it?

推荐答案

我的结论是,这样做在C.它总是会比其他语言更困难和繁琐的没有正确的方式。我认为这是重要的,但是,不要混淆您code的单元测试的缘故,虽然。让一切在C函数指针听起来不错,但我认为它只是使code可怕最后调试。

I've concluded that there is no 'right' way of doing this in C. It's always going to be more difficult and tedious than in other languages. I think it's important, however, not to obfuscate your code for the sake of unit tests, though. Making everything a function pointer in C may sound good, but I think it just makes the code horrific to debug in the end.

我的最新的方法是让事情变得简单。我不改变在一个文件的顶部比小 #IFDEF UNIT_TESTING 其他C模块的任何code的外扩和内存分配跟踪。然后我去了模块和删除,因此无法链接的所有依赖编译。当我查看未解决的符号,以确保它们是我想要的,我运行分析这些依赖关系,并产生存根原型所有符号的脚本。这些都被人抛弃在单元测试文件。 YMMV取决于你的外部依赖多么复杂。

My latest approach has been to keep things simple. I don't change any code in C modules other than a small #ifdef UNIT_TESTING at the top of a file for externing and memory allocation tracking. I then take the module and compile it with all dependencies removed so that it fails link. Once I've reviewed the unresolved symbols to make sure they are what I want, I run a script that parses these dependencies and generates stub prototypes for all the symbols. These all get dumped in the unit test file. YMMV depending on how complex your external dependencies are.

如果我需要模拟一个依赖于一个实例,使用另一个真实的,还是在另一个存根,然后我结束了被测一个单元三单元测试模块。拥有多个二进制文件可能不理想,但它与C的唯一真正的选择,他们都得到在同一时间运行,虽然,所以它不是一个真正的问题对我来说。

If I need to mock a dependency in one instance, use the real one in another, or stub it in yet another, then I end up with three unit test modules for the one module under test. Having multiple binaries may not be ideal, but it's the only real option with C. They all get run at the same time, though, so it's not really a problem for me.

这篇关于如何做到依赖注入用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆