首页 \ 问答 \ 获得相同类型的多个guice单例(Getting multiple guice singletons of the same type)

获得相同类型的多个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
更新时间:2022-05-01 10:05

最满意答案

我通常会做你在测试中最终做的事情。 在编写我的测试时,我假定.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.

相关问答

更多
  • 我通常会做你在测试中最终做的事情。 在编写我的测试时,我假定.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 ...
  • 这取决于你想要的行为。 返回IList 告诉主叫方他们已收到他们要求的所有数据 返回一个IEnumerable 告诉调用者他们需要迭代结果,并且可能会被延迟加载。 返回一个IQueryable 告诉调用者该结果由Linq提供程序支持,它可以处理某些类的查询,从而使调用者的负担形成一个执行查询。 尽管后者为呼叫者提供了很大的灵活性(假设您的存储库完全支持它),但是测试最难,而且可以说是最不确定性的。 It depends on what behavior you want. Returnin ...
  • 您的代码中存在多个问题: 你在KeyValuePair上调用GetType KeyValuePair ,(因为_gameLookup是一个字典,所以实现了IEnumerable>它是d的类型),显然你想在值上使用它。 之后选择集合( games )的类型为IEnumerable>所以你需要删除KeyValuePair部分 演员AsQuer ...
  • List list = 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 ...
  • 我假设查询的不同部分只在运行时才知道,即不能只使用|| 在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 ...
  • 在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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)