获得相同类型的多个guice单例(Getting multiple guice singletons of the same type)
你能得到2个相同基础类型的单例实例吗?
这在春天显然是微不足道的,因为它基于附加范围的命名实例,但我无法在guice中看到与绑定类型到实现类相同的等价物。 请注意,我不想绑定到实例,因为有问题的实例被guice注入其他依赖项。
can you get 2 singleton instances of the same underlying type?
this is obviously trivial in spring as it is based on named instances to which you attach a scope but I can't see the equivalent in guice which is about binding types to implementation classes. Note that I don't want to have to bind to the instance as the instances in question are injected with other dependencies by guice.
原文:https://stackoverflow.com/questions/1221920
最满意答案
我通常会做你在测试中最终做的事情。 在编写我的测试时,我假定.Net库类能够正常工作,并且不包含错误,所以我可以在测试中使用它们。 当我需要测试列表,集合,查询,字典等时,我只是创建真实的东西并填充测试数据。 它使得测试更加可读和更快写入,而且老实说风险是不存在的。
I usually do exactly what you ended up doing in your test. When writing my tests I assume that the .Net library classes work correctly and don't contain bugs, so I can use them in the tests. When I need a test list, collection, queryable, dictionary, etc. I just create the real thing and populate with test data. It makes the tests much more readable and quicker to write, and to be honest the risk is non-existent.
相关问答
更多-
我如何模拟IQueryable
(How do I mock IQueryable [2023-09-12]) 我通常会做你在测试中最终做的事情。 在编写我的测试时,我假定.Net库类能够正常工作,并且不包含错误,所以我可以在测试中使用它们。 当我需要测试列表,集合,查询,字典等时,我只是创建真实的东西并填充测试数据。 它使得测试更加可读和更快写入,而且老实说风险是不存在的。 I usually do exactly what you ended up doing in your test. When writing my tests I assume that the .Net library classes wo ... -
我应该从我的DAL返回IEnumerable
还是IQueryable [2022-05-11]?(Should I return IEnumerable or IQueryable from my DAL?) 这取决于你想要的行为。 返回IList告诉主叫方他们已收到他们要求的所有数据 返回一个IEnumerable 告诉调用者他们需要迭代结果,并且可能会被延迟加载。 返回一个IQueryable 告诉调用者该结果由Linq提供程序支持,它可以处理某些类的查询,从而使调用者的负担形成一个执行查询。 尽管后者为呼叫者提供了很大的灵活性(假设您的存储库完全支持它),但是测试最难,而且可以说是最不确定性的。 It depends on what behavior you want. Returnin ... -
您的代码中存在多个问题: 你在KeyValuePair
上调用GetType KeyValuePair ,(因为_gameLookup是一个字典,所以实现了IEnumerable >它是d的类型),显然你想在值上使用它。 之后选择集合( games )的类型为IEnumerable >所以你需要删除KeyValuePair部分 演员AsQuer ... -
IList
到IQueryable [2022-05-23](IList to IQueryable ) Listlist = new List () { 1, 2, 3, 4, }; IQueryable query = list.AsQueryable(); 如果没有看到AsQueryable()方法,请为System.Linq添加一个using语句。 List list = new List () { 1, 2, 3, 4, }; IQueryable query = list.AsQueryable(); If you don't see t ... -
专业人士 可组合: 呼叫者可以添加过滤器 呼叫者可以添加分页 呼叫者可以添加排序 等等 缺点 非可测性: 您的存储库已不再适当的单元测试; 你不能依靠:工作,b:它做什么 ; 调用者可以添加一个不可翻译的函数(即没有TSQL映射;在运行时断开) 呼叫者可以添加一个过滤器/排序,使其像狗一样执行 由于调用者希望IQueryable
可以组合,它排除了非可组合的实现,或者强制您为自己编写自己的查询提供者 这意味着您无法优化/配置DAL 为了稳定,我不得不在我的存储库中暴露IQueryable 或Expr ... -
扩展IQueryable
Where()作为OR而不是AND关系(Extend IQueryable [2022-03-02]Where() as OR instead of AND relationship) 我假设查询的不同部分只在运行时才知道,即不能只使用|| 在where ... 一个懒惰的选择是Concat - 但这往往导致TSQL差等; 但是,我倾向于改为编写自定义Expression 。 采取的方法取决于提供者是什么,因为LINQ-to-SQL支持EF的不同选项(例如) - 在这里有一个真正的影响(因为你不能在EF中使用子表达式)。 你能告诉我们哪个? 这里有一些应该与LINQ到SQL一起工作的代码; 如果你构建一个数组(或列表,并调用.ToArray() )的表达式,它应该工作正常; 示例是LINQ ... -
如果Any() == true ,它可能会更快,但差异可能可以忽略不计。 另一方面,如果查询返回某些内容,则可能会显着较慢,因为您之后有两次往返数据库。 一个用于查询是否存在任何内容,其次用于实际检索数据。 It might be faster, if Any() == true, but the difference will probably be negligible. On the other hand it can be significantly slower, if the query ret ...
-
模拟和智能
(Mocking and IQueryable [2022-06-19]) 在Moq ,它将是: mockRepository.Expect( r => r.Query() ).Returns( myEnumerable.AsQueriable() ); 在RhinoMocks它将是: Expect.Call( repository.Query() ).Return( myEnumerable.AsQueriable() ); In Moq it would be: mockRepository.Expect( r => r.Query() ).Returns( myEnume ... -
通用IQueryable
是您在方法签名等中最常使用的。 非通用IQueryable主要用于为您提供主要用于动态查询构建方案的弱类型入口点 。 来自LINQ的Matt Warren :建立一个可以提供智能的提供者 - 第一部分 你应该尽可能地使用通用的IQueryable 。 The generic IQueryable is the one you use most often in method signatures and the like. The non-generic IQuer ... -
在进行选择之前将集合转换为对象,它应该可以工作。 使用PredicateBuilder 更新以显示过滤和分页以及动态LINQ以进行排序。 var predicate = new PredicateBuilder
.True(); if (!string.IsNullOrEmpty( typeFilter )) { predicate = predicate.And( u => u.Type == typeFilter ); } if (!string.IsNullOrEmpty( name ...