- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 delphi 中创建了一个 IoC,能够自动注册任何具有 IocSingletonAttribute 的类。
自动注册如下所示。
procedure TIocContainer.AutoRegister;
var
ctx: TRttiContext;
rType: TRttiType;
attr: TCustomAttribute;
&Type: PTypeInfo;
begin
ctx := TRttiContext.Create;
for rType in ctx.GetTypes do
Begin
for attr in rType.GetAttributes do
Begin
if TypeInfo(IocSingletonAttribute) = attr.ClassInfo then
Begin
&Type := IocSingletonAttribute(attr).&Type;
RegisterType(&Type, rType.Handle, True);
End;
End;
End;
end;
然后,我创建一个实现并向其中添加 IocSingletonAttribute。看起来像这样
[IocSingleton(TypeInfo(IIocSingleton))]
TIocSingleton = class(TInterfacedObject, IIocSingleton)
procedure DoSomeWork;
end;
现在我们来看看程序的实际代码。如果我在下面编写代码,IoC 将不起作用。 AutoRegister 过程没有获取 TIocSingleton。
var
Ioc: TIocContainer;
Singleton: IIocSingleton;
begin
Ioc := TIocContainer.Create;
try
Ioc.AutoRegister;
Singleton := Ioc.Resolve<IIocSingleton>();
Singleton.DoSomeWork;
finally
Ioc.Free;
end;
end.
但是如果我编写下面的代码,一切都会按预期工作。请注意我如何声明 TIocSingleton 类并使用它。
var
Ioc: TIocContainer;
Singleton: IIocSingleton;
ASingleton: TIocSingleton;
begin
Ioc := TIocContainer.Create;
ASingleton := TIocSingleton.Create;
try
Ioc.AutoRegister;
Singleton := Ioc.Resolve<IIocSingleton>();
Singleton.DoSomeWork;
finally
Singleton.Free;
Ioc.Free;
end;
end.
因此,基于此,我假设 Delphi 的编译器链接器正在第一个示例中删除 TIocSingleton,因为它从未在应用程序的任何部分中显式使用。所以我的问题是,是否可以为某个类关闭编译器的“删除未使用的代码”功能?或者,如果我的问题不是链接器,任何人都可以解释为什么第二个示例有效但第一个示例无效?
最佳答案
将 {$STRONGLINKTYPES ON}
指令添加到 .dpr。那么这些类型应该包括在内。但它肯定会毁掉你的应用程序,因为它不适用于单个类。
关于delphi - 创建 Delphi IoC。如何禁用 Delphi 的链接器删除未使用的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054460/
我开始认真考虑使用 IoC 容器会引发创建过度设计的解决方案(至少它会促使我尝试使用各种不必要的功能:)。 是时候将我的“IoC”反模式列表与社区列表同步了。 我短暂的经验告诉我们,在启动时每个应用程
我一直在阅读有关控制反转框架的内容,而我只是在玩弄这个问题:“我到底为什么需要一个框架来做到这一点?” 不要误解我的问题...该模式是我们程序员经常使用的,但是...一个功能齐全的框架可以做到这一点?
想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的。没有足够细节的答案可能会被编辑或删除。 我正在尝试确定是否需要付出额外的努力来封装我的 IoC 容器。经验告诉我,我应该
有人建议我,在使用 IOC 容器时,我应该改变这个: class Foobar: IFoobar, IDisposable {}; 进入这个: interface IFoobar: IDisposab
《畜牧代码》播客第 68 期有人,http://herdingcode.com/herding-code-68-new-year-shenanigans/ ,表示 IOC 容器不适合使用 Python
我们正在使用 NInject 框架在我们的应用程序中实现 IoC/DI。我们有具有内部方法的内部类。要实现 IoC/DI,我们必须提取接口(interface)。但是如果我们在一个内部类中只有内部方法
Spring IOC 相关接口分析 1.BeanFactory Spring 中 Bean 的创建是典型的工厂模式,这一系列的 Bean 工厂,即 IOC 容器,为开发者管理对象之间的依赖关系提供了很
MEF is not an IoC container .不过好像是差不多 一个 IoC 容器。似乎我可以很容易地让 MEF 表现得像一个 IoC 容器(见下面的例子),而且让 MEF 成为一个完整的
只是想继续了解 IOC 的原则。 Q1:静态方法 - 具有静态辅助方法的实用程序类是否应该与 IOC 连接? 例如,如果我有一个带有许多静态方法的 HttpUtils 类,我是否应该尝试通过 IOC
众所周知,在asp.net Startup 类中有一个方法ConfigureServices,我们可以添加自定义服务。服务通过依赖注入(inject)提供。 ASP.NET Core includes
所以..我一直在深入研究 IoC 容器和服务定位器。 我认为 IoC 容器是 IoC 容器,而不是服务定位器,因为 您使用它的方式。您将服务定位器传递给需要依赖项的类,然后通过容器检索依赖项。另一方面
阅读许多有关这三个成语之间差异的帖子。但是比较困惑,然后我遇到了这篇文章: http://martinfowler.com/articles/injection.html 只是想看看我是否做对了。如果
我正在寻找用于 asp.net webapi 的 ioc 容器。我们正在寻找的几个关键功能如下 自定义生命周期 对网络请求生命周期的内置支持 在管理依赖项注册方面与 Web API 的良好集成。 最佳
我很难跟随 FP。当人们说“更惯用的风格”时,我必须明白:99% 的 Java 库不适用于 Kotlin 和 Scala 的 FP 惯用风格,对吧?好吧,我需要 Spring Boot 来快速启动 V
目录 1、Spring 1.1、简介 1.2、优点 1.3、组成 1.4、扩展 2、IO
重要提示:请注意,我并不是说单例具有私有(private)构造函数和静态实例变量(或有人建议使用静态类),而是单例在应用程序生命周期内从控制容器的反转返回相同的实例。 许多容器默认使用较短的生命周期。
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
松耦合当然很棒,但我经常想知道使用 IoC 容器(例如 CaSTLe Windsor)动态连接的开销对紧耦合系统有什么影响? 我知道详细的答案将取决于 IoC 的用途,但我真的只是想了解 IoC 工作
我正在努力让 IOC 在远程处理场景中工作。我将我的应用程序服务器设置为发布通过 XML 配置的服务(SingleCall)。 众所周知,这就像这样: RemotingConfiguration.Co
我使用 IoC (DI) 方法并且通常有参数,这些参数由最低层(数据库层等)从配置设置(即连接字符串、静态值等)中读取。最好的方法是什么? 直接在这个最底层读取,即: string sendGridA
我是一名优秀的程序员,十分优秀!