- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Unity 并尝试尽可能遵循 SOLID 原则。因此,所有实现都只依赖于接口(interface)。
我有一个 collectionwrapper,看起来像这样:
public interface ICollectionWrapper<TModel>
{
int TotalCount { get; set; }
IEnumerable<TModel> Items { get; set; }
}
现在我想创建 ICollectionFactory<T>
的实例与工厂。这是我到目前为止得到的:
public interface ICollectionWrapperFactory
{
ICollectionWrapper<T> CreateCollection<T>();
ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items);
ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int totalCount);
}
public class CollectionWrapperFactory : ICollectionWrapperFactory
{
private readonly IUnityContainer _container;
public CollectionWrapperFactory(IUnityContainer container)
{
_container = container;
}
public ICollectionWrapper<T> CreateCollection<T>()
{
var collectionWrapper = _container.Resolve<ICollectionWrapper<T>>();
return collectionWrapper;
}
public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items)
{
throw new System.NotImplementedException();
}
public ICollectionWrapper<T> CreateCollection<T>(IEnumerable<T> items, int totalCount)
{
throw new System.NotImplementedException();
}
}
我知道使用容器作为服务定位器被认为是一种反模式,但我不知道有什么更好的方法来解决这个问题。如果有更好的模式来做这件事,我洗耳恭听……另一种方法是使用 Activator,但是工厂需要知道 ICollectionWrapper<T>
的实际实现。 .
但真正的问题是我无法正确注册 ICollectionWrapper。
container.RegisterType<ICollectionWrapper<T>, CollectionWrapper<T>>(new TransientLifetimeManager()); // Does not compile.
T
可以是任何类型。 我希望能够创建 ICollectionWrapper<T>
的实例无需注册 T
的所有可能组合.
目前我只有一个 ICollectionWrapper<T>
的实现.但重点是,我真的希望 Unity 成为唯一知道实际实现的部分。
[DataContract]
public class CollectionWrapper<TModel> : ICollectionWrapper<TModel>
{
public CollectionWrapper(IEnumerable<TModel> items)
{
Items = items;
}
public CollectionWrapper(IEnumerable<TModel> items, int totalCount)
{
Items = items;
TotalCount = totalCount;
}
public CollectionWrapper()
{
}
[DataMember]
public int TotalCount { get; set; }
[DataMember]
public IEnumerable<TModel> Items { get; set; }
}
最佳答案
T may be any type. I want to be able to create instances of ICollectionWrapper without having to register every possible combination of T.
这就是注册泛型的用途。一些 IOC 将该方法命名为 RegisterGeneric
以使其 self 解释(例如 autofac),但统一保留它 just an overload of RegisterType
.
container.RegisterType(typeof(ICollectionWrapper<>), typeof(CollectionWrapper<>), new TransientLifetimeManager());
另请注意,您的可注入(inject)物具有多个构造函数。 That itself is considered as anti-pattern .
如果你修复了多构造器的问题,上面的注册就可以了。
关于c# - 统一: Register and resolve class with generic type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334240/
我正在更新a boilerplate中使用的babel包,从 babel-core、babel-register 等到@babel/core、@babel/register > 等 问题:在 npm
为了在 Babel 中进行运行时转换,您需要 require 并使用 babel-core/register。我不知道 register 在这个意义上意味着什么,即实际的定义。 The page is
我们可以使用“babel-register”即时转换源代码 https://babeljs.io/docs/usage/require/ 但我无法验证要使用哪一个: require('babel-co
我有一个用 RAL 建模的大寄存器映射,我想随机化一些寄存器。如果我想单独限制寄存器,那么这很简单: reg_model.register_a.randomize() with {value > 5;
我正在尝试制作一个使用 django 内置用户模型的简单应用程序。我已经创建了一个注册页面,但是当我运行服务器时,我在索引页面上收到此错误。这是我正在使用的代码: 注册.html {% extend
我正在尝试制作一个使用 django 内置用户模型的简单应用程序。我已经创建了一个注册页面,但是当我运行服务器时,我在索引页面上收到此错误。这是我正在使用的代码: 注册.html {% extend
在下面的应用程序中,我尝试将 id 动态添加到生成的项目中。我的代码工作正常,但是当我在其中添加以下两行注释时。它抛出错误 Uncaught Ext.AbstractManager.register(
当我尝试运行 SignUpFragmentTest 类时,出现错误“未注册检测!必须在注册检测下运行”。我认为当我使用@Rule 时会抛出错误。 import android.support.test
到目前为止,我目前的理解是:movq %rdi, %rax 将值从寄存器 %rdi 移动到寄存器 %rax 和movq (%rdi), %rax 会将内存中的值从(%rdi) 移动到寄存器%rax 但
我做了一个common view,所有页面都需要这个view。所以无论我需要什么,我都将这个 View 称为 xtype 。在这个公共(public) View 中,有一些组件由 id 值定义。 根据
app.get("/register", function(req, res) { res.render("register"); }); app.get("register", function(r
我目前正在开发我的个人网站 和我的网站的一部分,我有偏见的公关,以避免重复的代码... 这个 View 我有一个dojox.grid.datagrid ... 我可以在同一页面中两次调用此 View
调试版本对我来说工作正常。当我选择Active Build Variant = release,并尝试运行Build-> Generate Bundle(s)/ APK(s)-> Build APK时
我创建了一个异步函数如下: let createUserWrapper = async function(user){ await Log.createUser(use
我在 Dojo 的内容 Pane 中遇到问题,它出现一秒钟,然后内容消失。我收到以下错误: Uncaught ReferenceError: dijit is not defined index.p
我正在尝试在前端使用 React 创建一个注册表单,并在后端使用 Typescript 创建 Express 和 MongoDB atlas。当我单击提交按钮时,没有任何反应,但是当我使用 Postm
我已经使用 graph ql(具有非常基本的模式)创建了一个 Node js 项目,但是当我在为 graphql 和 graphiql 注册插件后尝试启动服务器时,我收到注册丢失错误。下面是我的代码
我的计算机体系结构书籍解释说 "Since writes to the register file are edge-triggered, our design can legally read an
我有一个 Google Pub/Sub 项目并创建了一个主题和一个简单的订阅。 但是,在指定用于推送的 URL 时,我不断收到以下错误。我已经完成了站点验证过程并在 API 和服务中注册了域,如 ht
您好,我在再次选择 DOJO 中的下拉菜单时遇到错误 function onReportTypesSelect() { if(
我是一名优秀的程序员,十分优秀!