unity-container

Is there a good way to use events with Unity?

Deadly 提交于 2019-12-01 04:33:05
programAction = UnityContainer.Resolve<LoaderDriver>(); (programAction as LoaderDriver).LoadComplete += new EventHandler(Program_LoadComplete); Is there a configuration that let's me resolve my objects having already been wired to an event? Alternately, is there a preferred way to achive the same result? I've noticed that sometimes when I don't see a "feature" its because a pattern I don't know about is preferred. Yes there is a way. You would have to write an extension that adds a custom BuilderStrategy to the PostInitialization stage of the Unity BuildPipeline. The code for extension and

Using Unity to inject objects into IValueConverter instance

不羁岁月 提交于 2019-12-01 04:27:20
I have an instance of IValueConverter in a Silverlight 5 project, which converts custom data into different colors. I need to read the actual color values from a database (as these can be edited by the user). Since Silverlight uses asynchronous calls to load the data through Entity Framework from the database, I created a simple repository, which holds the values from the db. The interface: public interface IConfigurationsRepository { string this[string key] { get; } } The implementation: public class ConfigurationRepository : IConfigurationsRepository { private readonly TdTerminalService

How to declare the Unity InjectionFactory in XML configuration

谁都会走 提交于 2019-12-01 04:22:53
I'm in the process of moving our Unity configuration to the web.config file. I'm stuck on how to migrate the following code config to the xml format: var container = new UnityContainer(); container.RegisterType<IPrincipal>(new InjectionFactory(x=> HttpContext.Current.User)); return container; Here are the XML declartion: <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="IRepository" type="Model.IRepository, Model" /> <alias alias="Repository" type="Data.Repository, Data" /> <container> <register type="IRepository" mapTo="Repository" /> </container> </unity>

MVC3, Unity Framework and Per Session Lifetime Manager Issue

百般思念 提交于 2019-12-01 04:21:32
问题 In a simple word I try to create Lifetime manager for Unity framework by using Http Session in my MVC3 project. My sample implementation of lifetime manager is: public class UnityPerSessionLifetimeManager : LifetimeManager { private string sessionKey; private HttpContext ctx; public UnityPerSessionLifetimeManager(string sessionKey) { this.sessionKey = sessionKey; this.ctx = HttpContext.Current; } public override object GetValue() { return this.ctx.Session[this.sessionKey]; } public override

Resolving named dependencies with Unity

六眼飞鱼酱① 提交于 2019-12-01 04:14:27
I have a service with 2 dependencies: a Repository and a Gateway (sms). I need to resolve 2 different versions of the service that only differ on one of the parameters passed to the gateway. The code is simplified to as follows public interface IService { string DoSomething(); } public interface IServiceFoo { string DoSomething(); } public interface IServiceBar { string DoSomething(); } public interface IRepository { } public class Repository : IRepository { } public interface IGateway { string Name { get; set; } } public class Gateway : IGateway { public string Name { get; set; } public

Aspect Oriented Logging with Unity\\T4\\anything else

孤街醉人 提交于 2019-12-01 04:07:45
In my application we have a trace logger. We have log statements added at the beginning and end of most of the important methods tracing the method name and the parameter values. Now these trace statements are bloating the code and it is a bit of a pain to read through them. I am considering how can I separate this aspect of the code from my business logic. Today I was reading about Unity's interception framework. I had a passing thought if it is possible to intercept my method calls with a generic logger and log the method name and parameter values. I am not sure if it is possible to read

Unity resolving cyclic dependency

穿精又带淫゛_ 提交于 2019-12-01 03:42:25
While learning Unity (DI framework in C# ) I came across a situation where one class has a setter injection of ClassB class ClassA : IClassA { [Dependency] public IClassB ClassB { get { return _classB; } set { if (value == null) throw new ArgumentNullException("value"); _classB = value; } } and the other has a constructor injection of the ClassA class ClassB : IClassB { [InjectionConstructor] public ClassB(IClassA classA) { _classA = classA; } } I am not able to resolve both the classes correctly within the container. var container = new UnityContainer(); container.RegisterType<IClassB, ClassB

Is there a good way to use events with Unity?

南笙酒味 提交于 2019-12-01 03:24:43
问题 programAction = UnityContainer.Resolve<LoaderDriver>(); (programAction as LoaderDriver).LoadComplete += new EventHandler(Program_LoadComplete); Is there a configuration that let's me resolve my objects having already been wired to an event? Alternately, is there a preferred way to achive the same result? I've noticed that sometimes when I don't see a "feature" its because a pattern I don't know about is preferred. 回答1: Yes there is a way. You would have to write an extension that adds a

How to conditionally bind a instance depending on the injected type using unity?

删除回忆录丶 提交于 2019-12-01 02:25:58
问题 I'm used to Ninject, and for a specific project I'm asked to learn Unity. There is one thing i can't find information on how to do. In Ninject I can state: Bind<IWarrior>().To<Samurai>().Named("Samurai"); Bind<IWarrior>().To<Ninja>().Named("Ninja"); Bind<IWeapon>().To<Katana>().WhenInjectedInto(typeof(Samurai)); Bind<IWeapon>().To<Shuriken>().WhenInjectedInto(typeof(Ninja)); And then when one asks for the warrior named samurai, the samurai comes with kanana and the ninja comes with shurikens.

Unity Container and Support for dotnet Core / netstandard [closed]

坚强是说给别人听的谎言 提交于 2019-12-01 01:25:30
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . So MS dropped (free sourced) Unity. Future of Unity The github repository appears inactive Unity on GitHub Anybody know what is going on with Unity ? Is there a IOC Dependency Injection tool for dotNet Core, if Unity is a dead end under dotnet core? EDIT: Still waiting on news about Unity.