- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基于数据库中的用户配置构建用户界面的应用程序。我创建了一个名为 IAction 的界面,如下所示;
public interface IAction
{
ActionType ActionType { get; }
bool CanExecute { get; }
void Configure(ActionConfigDto config);
void Execute();
}
像 AddItemAction 这样的实现看起来像这样;
public class AddItemAction : IAction
{
public ActionType ActionType
{
get { return ActionType.AddItem; }
}
// Rest of implementation
}
在启动时,我迭代了一组来自数据库的 ActionConfigDto。它们指定了操作的一些可配置参数以及我用来匹配相应操作的 ActionType。可能有多个具有相同 ActionType 的 ActionConfigDto,因此应为每个配置创建相应 Action 的多个实例。创建 IAction 实例后,应将配置传递给操作 Configure 方法。
我正在使用 Simple Injector 作为我的 DI 容器,但我还没有找到一个示例来说明如何使用我只在运行时知道的数据来实例化 Action 的实例。
我知道 Simple Injector 的编写方式是为了阻止不良做法,所以,我的方法是否完全错误,您将如何实现这一要求,或者是否有一种方法可以使用 Simple Injector 实现这种配置?
最佳答案
在进行更多搜索后,我找到了一些关于 resolving instances by key 的文档并实现了一个手动注册每种类型的 ActionFactory。
public class ActionFactory : IActionFactory
{
private readonly Container _container;
private readonly Dictionary<string, InstanceProducer> _producers;
public ActionFactory(Container container)
{
_container = container;
_producers = new Dictionary<string, InstanceProducer>(StringComparer.OrdinalIgnoreCase);
}
public IAction Create(ActionType type)
{
var action = _producers[type.ToString()].GetInstance();
return (IAction) action;
}
public void Register(Type type, string name, Lifestyle lifestyle = null)
{
lifestyle = lifestyle ?? Lifestyle.Transient;
var registration = lifestyle.CreateRegistration(typeof (IAction), type, _container);
var producer = new InstanceProducer(typeof (IAction), registration);
_producers.Add(name, producer);
}
}
我按如下方式配置工厂;
var registrations =
from type in AssemblySource.Instance.GetExportedTypes()
where typeof (IAction).IsAssignableFrom(type)
where !typeof (ActionDecorator).IsAssignableFrom(type)
where !type.IsAbstract
select new {Name = type.Name, ImplementationType = type};
var factory = new ActionFactory(container);
foreach (var reg in registrations)
{
factory.Register(reg.ImplementationType, reg.Name);
}
container.RegisterSingle<IActionFactory>(factory);
Simple Injector 有很好的文档,在找到该链接之前,我没想过使用 key 来注册 Actions。
关于c# - 在 Simple Injector 中使用运行时数据获取实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044938/
我想创建一个对象,比方说一个饼图。 class Pie def initialize(name, flavor) @name = name @flavor = flavor e
我正在寻找类似于 Log property in Linq DataContext 的功能. 最佳答案 所有执行的 SQL 都写入 Trace,因此您可以使用 TraceListener 访问。 关于
使用 django-simple-history ,如何从我的模型中获取最后更改的对象? 我尝试了 MyModel.history.most_recent(),它需要一个模型实例,因此可能会返回所选实
我定期使用 Eclipse 调试器,这一直困扰着我关于 IDE 的问题。步进过滤是一个非常宝贵的工具,这样我就不会进入没有源代码或者我根本不感兴趣的类。但是,Eclipse 在大多数情况下都没有正确处
我基于有向图构建一棵树。源数据是 SQL 表中的一系列父子关系。它肯定是一棵树(无论如何我都会验证)。我想要一组从根到每片叶子的简单路径。数据是会计“科目表”中的标题,路径类似于“根 -> Asset
我对如何在torii中使用ember-simple-auth有点困惑 我正在使用ember-cli-simple-auth和ember-cli-simple-auth-torii 我得到下面提到的错误
考虑以下用 simple-xml 注释注释的枚举: @Root(name="days") public enum DaysOfWeek { SUNDAY("blue", 30), MO
我有一个 json 文件,我正在 excel 中创建一些 vba 代码。我想用java读取这个json文件。为此,我编写了以下代码 try { Class.forName("org.j
我在尝试使用 google 的 simple-json 解析一个简单的 json 时遇到了一个奇怪的问题。 这是我的代码,它不起作用: String s = args[0].toString(); J
我正在尝试解析下面的 json 文件: {"units":[{"id":42, "title":"Hello World", "positi
我正在使用 simple-import-sort eslint 插件进行 react 。我想我的 .eslintrc.js是对的,但我无法使这个特定的插件工作。我在文件的第一行收到以下错误: 未找到规
我正在尝试解析子文件,但我不知道自己做错了什么(当然,我也不知道自己做对了什么)。 文件.json [{ "arrOne":{ "one":"a", "two":"b",
在我的Angel 16(独立的)应用程序中,我有一个简单的服务来测试LangChain和OpenAI。同样的代码在NodeJS应用程序中运行得非常好,然而,当从角度运行时,OpenAI返回的结果总是空
我正在尝试测试连接到数据库的 Web 应用程序 - 我的 Junit 代码不执行连接。 设置按照 https://github.com/h-thurow/Simple-JNDI InitialCont
我正在尝试制作一个小型应用程序,它可以通过 ISBN 在亚马逊上搜索一本书。我是 Amazon Web Service 的新手。 我正在关注以下链接: http://flyingpies.wordpr
我正在使用简单 XML 序列化 (simple-xml-2.6.6.jar) here将我的 XML 响应从 webservice 转换为 POJO 类。 XML 是:
Simple UI混合开发的必经之路~ Simple UI快速上手 在混合开发的模式下,如果想使用django admin,又嫌弃后台不符合你的审美?Simple UI给你想要的答案,我不是打广告的~
Simple JSON是Google开发的Java JSON解析框架,基于Apache协议。 下载的文件是:json_simple.jar 例子1:很方便的方式,使用JSONValue
我有一个 simple_form 表单设置,它会很好地显示内联错误。我遇到过一些用户看不到这些错误的问题,并要求在非常长的表格顶部进行清晰的枚举。我使用了 Rails 教程中的代码设置:
我正在使用简单的 XML 框架,只是重命名了一些 XML 布局,这些布局现在似乎不再起作用了。 这是我的 XML: 2 0 1
我是一名优秀的程序员,十分优秀!