unity-container

Unity .NET: List of dependencies

允我心安 提交于 2019-12-04 03:41:56
Is it possible to inject a list of dependencies like this in Unity or other kind of IoC libraries? public class Crawler { public Crawler(IEnumerable<IParser> parsers) { // init here... } } In this way I can register multiple IParser in my container and then resolve them. Is it possible? Thanks It is possible but you need to apply some workarounds. First you need to register each IParser with a name in the Unity Container. Second you need to register a mapping from IParser[] to IEnumerable<IParser> in the container. Otherwise the container cannot inject the parsers to the constructor. Here´s

Unity framework DependencyAttribute only works for public properties?

断了今生、忘了曾经 提交于 2019-12-04 02:37:43
I was trying to clean up some accessability stuff in my code, and inadvertently broke Unity dependency injection. After a while I realized that I marked some public properties that I didn't really want exposed outside my DLLs to internal. Then I started getting exceptions. So it seems that using the [Dependency] attribute in Unity only works for public properties. I suppose that makes sense since the internal and private props wouldnt be visible to the Unity assembly, but feels really dirty to have a bunch of public properties that you never want anyone to set or be able to set, other than

How to setup a Unity Registration Convention?

可紊 提交于 2019-12-04 02:29:00
With structure map, you can register a convention that lets you not just tweak the type, but also intervene during object creation. How can I do this with Unity. public class SettingsRegistration : IRegistrationConvention { public void Process(Type type, Registry registry) { if (!type.IsAbstract && typeof(ISettings).IsAssignableFrom(type)) { registry.For(type).Use(x => { var svc = x.GetInstance<ISettingService>(); return svc.LoadSetting(type); }); } } } You can do this with a combination of Unity's Registration by Convention and an InjectionFactory . I see three common options for

ASP.Net MVC 4 Web API controller dosn't work with Unity.WebApi

☆樱花仙子☆ 提交于 2019-12-04 02:11:22
My ASP.Net MVC 4 Web API controller dosn't work with Unity.WebApi. In the same project simple controllers works with Unity.Mvc3 properly. But when I run Web API controller derived from ApiController I'm getting a message: {"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Type 'ElectricTests.Controllers.Api.DocumentsController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http

How to inject dependency property using Ioc Unity

筅森魡賤 提交于 2019-12-04 01:47:17
I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB, ServiceB>(); When I resolve a ServiceA instance, serviceB will be null . How can I resolve this? You have at

Unity.WebApi | Make sure that the controller has a parameterless public constructor

青春壹個敷衍的年華 提交于 2019-12-04 01:42:52
问题 I am using the Unity.WebApi NuGet package (Unity 4.0.1 and Unity.WebApi 5.2.3) in an ASP.NET WebApi solution. The issue I am facing is that when attempting to run the code, I get the error: Make sure that the controller has a parameterless public constructor . I've searched similar threads here, but I couldn't find any thread that matched my issue. Please don't just say "Add a parameterless constructor" because it shows you obviously have no clue what IoC is and why that statement makes

Unity Framework IoC with default constructor

限于喜欢 提交于 2019-12-04 00:38:16
问题 I'm trying to inject a dependency to my MVC controllers like this private static void RegisterContainer(IUnityContainer container) { container .RegisterType<IUserService, UserService>() .RegisterType<IFacebookService, FacebookService>(); } The UserService class has a constructor like this... public UserService(): this(new UserRepository(), new FacebookService()) { //this a parameterless constructor... why doesnt it get picked up by unity? } public UserService(IUserRepository repository,

Creating objects using Unity Resolve with extra parameters

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 00:18:21
I'm using Prism, which gives be the nice Unity IoC container too. I'm new to the concept, so I haven't gotten my hands all around it yet. What I want to do now is to create an object using the IoC container, but passing an extra parameter too. Allow me to explain with an example..: I have a class that takes a commands object. This is registered in the IoC container, so it will handle it nicely: public class Person { public Person(IApplicationCommands commands) { .. } .. } Person person = _container.Resolve<Person>(); Now - I want to pass in another argument - e.g. the name of the person.

Unity Container - Dependency Injection of Dynamic DbContext into Service

北城余情 提交于 2019-12-03 21:26:45
I'm building a .Net Web API which uses a Service+Repository pattern w/ Entity Framework. Each controller's CRUD actions relay data retrieved by calls to a Service. I have a SomeContext that extends DbContext: public class SomeContext : DbContext { public SomeContext(string connString) : base(connString) { } // DbSets ... } The Service is initialized w/ a constructor that accepts an ISomeContext: public class Service : IService { public Service(ISomeContext ctx) : base(ctx) { _alpha = new AlphaRepository(ctx); ... } GetAllAlpha() { return _alpha.Get(); } ... } I want to use (Unity Container)

Unity: Register and resolve class with generic type

社会主义新天地 提交于 2019-12-03 20:54:11
问题 I'm using Unity and try to follow to SOLID-principles as far as possible. Therefore all implementations only have dependencies to interfaces. I have a collectionwrapper which looks like this: public interface ICollectionWrapper<TModel> { int TotalCount { get; set; } IEnumerable<TModel> Items { get; set; } } Now I want to create the instance of ICollectionFactory<T> with a factory. This is what I got so far: public interface ICollectionWrapperFactory { ICollectionWrapper<T> CreateCollection<T>