我正在尝试使用Spring IoC这样的接口:
public interface ISimpleService<T> {
void someOp(T t);
T otherOp();
}
Run Code Online (Sandbox Code Playgroud)
Spring可以根据泛型类型参数T提供IoC吗?我的意思是,像这样:
public class SpringIocTest {
@Autowired
ISimpleService<Long> longSvc;
@Autowired
ISimpleService<String> strSvc;
//...
}
Run Code Online (Sandbox Code Playgroud)
当然,我上面的例子不起作用:
expected single matching bean but found 2: [serviceLong, serviceString]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:957)
Run Code Online (Sandbox Code Playgroud)
我的问题:是否可以提供类似的功能,只需对接口或实现类进行最少的修改?我知道我可以使用@Qualifiers,但我希望尽可能简单.
我一直在环顾四周寻找有关在Android开发中使用依赖注入容器的一些信息.具体来说,如何以Activity某种方式覆盖a的创建,这种方式在被杀(无论出于何种原因)时也会起作用.
有没有人在这方面有经验?
可能重复:
控制反转<依赖注入
我总是在相同的上下文中读取IoC(控制反转)和DI(依赖注入).IoC和DI有什么区别?IoC与DI有何不同?
标准新手免责声明:我是IoC的新手并且正在获得混合信号.我正在寻找有关以下情况的一些指导.
假设我有以下接口和实现:
public interface IImageFileGenerator
{
void RenameFiles();
void CopyFiles();
}
public class ImageFileGenerator : IImageFileGenerator
{
private readonly IList<IImageLink> _links;
private readonly string _sourceFolder;
private readonly string _destinationFolder;
private readonly int _folderPrefixLength;
public ImageFileGenerator(IList<IImageLink> links, string sourceFolder, string destinationFolder)
{
_links = links;
_sourceFolder = sourceFolder;
_destinationFolder = destinationFolder;
_folderPrefixLength = 4;
}
public void RenameFiles()
{
// Do stuff, uses all the class fields except destination folder
}
public void CopyFiles()
{
// Do stuff, also uses the …Run Code Online (Sandbox Code Playgroud) 我熟悉这些模式,但仍然不知道如何处理以下情况:
public class CarFactory
{
public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6)
{
}
public ICar CreateCar(type)
{
switch(type)
{
case A:
return new Car1(Dep1,Dep2,Dep3);
break;
case B:
return new Car2(Dep4,Dep5,Dep6);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
通常,问题在于需要注入的引用量.当有更多的汽车时会更糟.
我想到的第一种方法是在工厂构造函数中注入Car1和Car2,但它违反工厂方法,因为工厂将始终返回相同的对象.第二种方法是注入servicelocator,但它的反模式到处都是.怎么解决?
替代方式1:
public class CarFactory
{
public CarFactory(IContainer container)
{
_container = container;
}
public ICar CreateCar(type)
{
switch(type)
{
case A:
return _container.Resolve<ICar1>();
break;
case B:
return _container.Resolve<ICar2>();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
替代方式2(由于树中的依赖性过多而难以使用):
public class CarFactory
{
public CarFactory()
{
}
public ICar CreateCar(type)
{
switch(type)
{ …Run Code Online (Sandbox Code Playgroud) c# dependency-injection inversion-of-control factory-pattern
我想在我的WPF应用程序中开始使用依赖注入,主要是为了更好的单元可测试性.我的应用程序主要是按照MV-VM模式构建的.我正在为我的IoC容器看autofac,但我认为这对于这个讨论来说并不重要.
将服务注入启动窗口似乎很简单,因为我可以在App.xaml.cs中创建容器并从中解析它.
我正在努力的是如何将DI ViewModels和服务转化为用户控件?用户控件通过XAML标记实例化,因此没有机会Resolve().
我能想到的最好的方法是将容器放在Singleton中,让用户控件从全局容器中解析它们的ViewModel.这感觉就像是一个中途解决方案,充其量,因为它仍然需要我的组件依赖于ServiceLocator.
WPF可以完全使用IoC吗?
[编辑] - 有人建议使用Prism,但即使是对Prism进行评估也似乎是一笔巨大的投资,我希望能有更小的东西
[编辑]这是我停止的代码片段
//setup IoC container (in app.xaml.cs)
var builder = new ContainerBuilder();
builder.Register<NewsSource>().As<INewsSource>();
builder.Register<AViewModel>().FactoryScoped();
var container = builder.Build();
// in user control ctor -
// this doesn't work, where do I get the container from
VM = container.Resolve<AViewModel>();
// in app.xaml.cs
// this compiles, but I can't use this uc,
//as the one I want in created via xaml in the primary window
SomeUserControl uc = new SomeUserControl(); …Run Code Online (Sandbox Code Playgroud) wpf design-patterns dependency-injection inversion-of-control autofac
我一直在研究城堡项目,特别是温莎.我对这项技术的可能性印象深刻,拥有这种松散耦合系统的好处显而易见.我唯一不确定的是,如果使用这种方法有任何缺点,特别是在asp.net?例如性能命中等
我试图让这个方法的好处在这里对我的开发人员可见,并且受到以下回击的打击:
这是使用反射,每次从容器调用一个对象时,必须使用反射,因此性能会很糟糕.(这是这种情况吗?它是否在每次通话时使用反射?)
如果我依赖于接口; 如何处理具有已添加到类中的额外方法和属性的对象?(通过继承)
.net asp.net dependency-injection castle-windsor inversion-of-control
Castle Windsor是否允许注册开放的通用接口,还是需要单独注册每个可能的类型实例?
示例 - 下面的类型为T,Z在编译时失败,除非我单独指定强类型的T,Z.
container.Register(Component
.For<IAdapterFactory<T,Z>>()
.ImplementedBy<AdapterFactory<T,Z>>()
.LifeStyle.PerWebRequest);
Run Code Online (Sandbox Code Playgroud) 我使用Autofac作为我的IoC和一切我已经阅读DI的话题提出使用"构造器注入"明确揭露类的依赖.不过,我也使用日志门面(Common.Logging)与log4net的,并已创建注入它的Autofac模块.现在,在我想要进行一些日志记录的每个类中,我都有额外的构造函数参数(参见示例#1)....
我想知道在使用日志门面时是否需要记录DI?我知道通过构造函数签名显式公开依赖是一个很好的架构. 但是在记录门面的情况下我相信以下是真的:
那么,别人怎么想?注入伐木门面是否过度杀伤?关于这个主题有一些类似的问题,但更笼统地说(基础设施) - 我主要对记录....
// IoC "way"
public class MyController : BaseController
{
private readonly ILog _logger;
public MyController(ILog logger)
{
_logger = logger;
}
public IList<Customers> Get()
{
_logger.Debug("I am injected via constructor using some IoC!");
}
}
// just use the logger "way"
public class MyController : BaseController
{
private static readonly ILog Logger = LogManager.GetCurrentClassLogger();
public IList<Customers> Get()
{
Logger.Debug("Done! I can use it!");
}
}
Run Code Online (Sandbox Code Playgroud) 我试图了解AOP,依赖注入和控制SPRING相关概念的反转,但我很难理解它.
有人能用简单的英文解释一下吗?