依赖注入和code混淆code

2023-09-05 04:19:50 作者:古城老巷旧少年

在马克·塞曼的书依赖注入的.NET ,他指出,越来越多的人使用DI容器自动注册的支持下,容器可以解决所需要的具体类型的实例,而几乎任何种类的优先配置,而这远远超过配置的体系结构方法的公约。但是这让我想知道如果我用了一个混淆器就不是这个突破code碱基,并导致容器失败,因为该公约有哪些变化?

In Mark Seemann's book Dependency Injection in .NET, he states that more people are using DI containers with AUTO-REGISTRATION support where the container can resolve instances of the required concrete types without almost any sort of prior configuration, and this goes well with the convention over configuration architecture approach. but this made me wonder if i used an obfuscator wouldn't this break the code base and cause the container to fail because the conventions have changed ?

推荐答案

您通常可以仍然使用依赖注入,即使你发货您的应用程序集模糊化的方式。这将工作,因为你通常使用的通用输入(如注册< IService,默认地将Impl>())或者的typeof 参数(如注册(typeof运算(IService)的typeof(默认地将Impl)))注册类型。换句话说,所有的编译器可以检查你仍然可以工作(当混淆器正常工作)。

You can typically still use dependency injection, even if you ship your application assemblies in an obfuscated way. This will work, because you would typically use generic typing (such as Register<IService, Impl>()) or typeof arguments (such as Register(typeof(IService), typeof(Impl))) to register types. In other words, everything the compiler can check for you will still work (when the obfuscator works correctly).

你应该密切关注就是一切的编译器无法检查。事情是:

What you should watch closely is everything the compiler can't check. Things as:

通过指定参数的名称使用字符串覆盖构造函数的参数。 在任何convension优于配置的方法,其中convension是基于名称,而不是类型的信息,如的的后缀'与构造函数参数的名称为AppSetting或ConnectionString的,期待各类与登记端名称控制器,期待的类型是在一个特定的命名空间。 Override constructor arguments by specifying the name of the argument using a string literal. Any convension over configuration approach where the convension is based on a name instead of type information, such as postfixing the name of constructor arguments with "AppSetting" or "ConnectionString", expecting the name of all types to register end with "Controller", expecting types to be in a particular namespace.

所以,你必须仔细观察这些问题。不要忘了保护自己通过创建一个可核查的配置,然后在你的情况我会核实应用程序启动这个配置(或添加一个命令行开关,使应用程序可以做一个自我检查)。

So you will have to watch these issues carefully. Don't forget to safeguard yourself by creating a verifiable configuration and in your case I would verify this configuration on application startup (or add a command line switch so that the application can do a self-check).