Tag: 统一容器

反映程序集会导致Unity需要Microsoft.Practices.ServiceLocation

我们通常只在我们的应用程序中引用Microsoft.Practices.Unity.dll。 我们只使用基本function,这很好用。 在一个应用程序中,使用reflection的行为导致Unity需要另一个DLL。 例如,创建一个控制台应用程序并仅引用Microsoft.Practices.Unity(文件版本2.0.414.0)。 输入以下代码并运行它: class Program { static void Main() { using (var container = new UnityContainer()) { container.RegisterType(); var thing = container.Resolve(); thing.DoSomething(); Console.WriteLine(); LoadSchemaLoaders(); } } public static void LoadSchemaLoaders() { var type = typeof(ISchemaLoader); try { // Get all loaded assemblies, including Unity. // Get all of the types. // Filter […]

只有在使用Unity的运行时值为非null时,如何注入属性?

我有一个要解析的接口,其中一个映射对象的依赖项有一个属性,我想设置一个值,当我解析顶级对象时,我只能使用它。 该属性没有有效的默认值。 如果未设置,则它应为null,并且只有在解析时可用的值不为空时才应设置它。 这种有条件的财产注入可能吗? 我试过这个…… container.RegisterType(“CartItemPurchaseTypeValidator”, new InjectionProperty(“AccountEntity”, null); …但它说我不能使用空值! 我也试过这个决心…… container.Resolve(new PropertyOverride(“AccountEntity”, value)); …但是当值为null时抛出exception。 它说, 参数类型推断不适用于空值。 使用正确配置的InjectionParameter或InjectionParameter类实例显式指示参数类型。 参数名称:parameterValue 基本上我想要注册一个仅使用覆盖设置的属性,然后仅在覆盖值为非null时才注册。 有任何想法吗? 当然,从语义的角度来看,属性注入应该是可选的。 干杯,伊恩。

Unity:在xml配置文件中将参数传递给自定义生命周期构造函数

我写了这样的CustomLifetimeManager: public class CustomLifetimeManager : LifetimeManager { private readonly string _arg; public CustomLifetimeManager(string arg) { _arg = arg; } } 现在,它可以通过编程方式轻松配置容器,但如何将其添加到配置文件中,如下所示?

关于generics类的Unity 2.0 IOC配置

我想要一些Repository类扩展一个常见的generics类来执行一些常见的操作,问题是:如何在配置文件中配置UserExRepository类型 。 public class UserExRepository : Repository, IUserEx { public UserExRepository(Context context):base(context){ } } public abstract class Repository : IRepository where TObject : class { protected Context Context = null; public Repository(Context context) { Context = context; } // do some common operation about entity, like create, delete… }

使用Unity IoC进行MVC集成测试

在使用基于构造函数的DI之后,我正在尝试使用Unity IoC。 问题是试图让集成测试工作。 http://patrick.lioi.net/2013/06/20/streamlined-integration-tests/ “运行集成测试应尽可能多地运用真实系统” 上面的Patrick描述了在MVCunit testing项目中设置IoC ..但我仍然坚持如何实现 public class HomeController : Controller { readonly IWinterDb db; // Unity knows that if IWinterDb interface is asked for, it will inject in a new WinterDb() public HomeController(IWinterDb db) { this.db = db; } public ActionResult Index() { var stories = db.Query() .OrderByDescending(s => s.Rating) .Include(s => […]

如何在下游生成新的Windows窗体时使用DI?

我有Unity DI容器最初使用我的Windows窗体应用程序。 在Program.cs我有以下内容: static void Main() { var container = BuildUnityContainer(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(container.Resolve()); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType(); container.RegisterType(); return container; } 在我的MainForm构造函数中,我有以下工作 : private readonly ITest test; public MainForm(ITest test) { this.test = test; InitializeComponent(); } 容器已解析,代码工作正常。 问题/问题是,我如何从MainForm实例化一个新表单,比如具有以下构造函数的Form2 : private readonly ISomeOtherTest someOtherTest; public Form2(ISomeOtherTest someOtherTest) […]

如何配置unity容器来提供字符串构造函数值?

这是我dad课 public class Dad { public string Name { get;set; } public Dad(string name) { Name = name; } } 这是我的测试方法 public void TestDad() { UnityContainer DadContainer= new UnityContainer(); Dad newdad = DadContainer.Resolve(); newdad.Name = “chris”; Assert.AreEqual(newdad.Name,”chris”); } 这是我得到的错误 “InvalidOperationException – the type String cannot be constructed. You must configure the container to supply this […]

如何使用Unity Dependecy Injection Web API实现Strategy / Facade模式

如何告诉Unity.WebApidependency injection框架,在正确的控制器中注入正确的类? DI项目容器 public class UnityContainerConfig { private static IUnityContainer _unityContainer = null; public static IUnityContainer Initialize() { if (_unityContainer == null) { _unityContainer = new Microsoft.Practices.Unity.UnityContainer() .RegisterType(“MyClass1”) .RegisterType(“MyClass2”) } } -MVC PROJECT- public static class UnityConfig { public static void RegisterComponents() { GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(DependencyInjection.UnityContainer.UnityContainerConfig.Initialize()); } } 控制器1: private IMyInterface _myInterface ///MyClass1 public […]

Microsoft Unity-解决问题

我希望这样做: container.Resolve(); 当它执行此操作时,它将向基础实体对象注入IDependency。 但是,存储在容器中的依赖项需要一个DependencyValue类型的对象,该对象从DependencyFactory提供一个值。 长话短说,我遇到的问题是,当Unity创建实现IDependency的类实例时,我需要接管并插入不在容器中的自定义值。 这是可能的,或者我最好在容器上使用RegisterInstance并手动实例化对象? 我更喜欢使用配置文件来存储映射,然后为参数实例化添加一些逻辑。 这是ParameterOverrides的意图吗? 或者我是否需要为此添加扩展名,或者什么? 谢谢。

如何将Unity与内部类一起使用?

我有一个Web API应用程序,并使用Unity进行dependency injection。 该应用程序使用包含Interface IDoStuff的库和实现该接口的类: internal interface IDoStuff { void DoSomething(); } internal class DoStuff : IDoStuff { public void DoSomething() { // Do some stuff } } 该库还有一个需要做的事情的公共类: public class NeedToDoStuff { private IDoStuff doStuff; public NeedToDoStuff() { this.doStuff = new DoStuff(); } internal NeedToDoStuff(IDoStuff doStuff) { this.doStuff = doStuff; } void PleaseDoStuff() { […]