unity-container

Injecting arrays with Unity

一笑奈何 提交于 2019-11-29 06:24:53
My goal is to constructor inject an array of objects implementing an interface. The following is the way I currently have it. Container .RegisterInstance<Company>(ParseCompany(args[1]) .RegisterInstance<eTargets>(ParseTargets(args[2])) .RegisterInstance<ILoader[]>(new ILoader[] { Container.Resolve<CustomerLoader>(), Container.Resolve<PaymentLoader(), Container.Resolve<InvoiceLoader() }); Is it typical to call Resolve in container configuration this way or is there a more standard way to accomplish the same thing? Unity natively understands arrays , so there's no reason to make it so

How to inject ApplicationUserManager with unity

妖精的绣舞 提交于 2019-11-29 04:14:43
I have ApplicationUserManager defined like this: public class ApplicationUserManager : UserManager<ApplicationUser, int> { public ApplicationUserManager(IUserStore<ApplicationUser, int> store) : base(store) { } public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password) { var result = base.CreateAsync(user, password); //... Repository.DoSomething(...); //Repository is null here //.. } [Dependency] public IRepository Repository { get; set; } } For some reason my repository in not getting injected. Repository is always null I also have this line in my unity

Unity Lifetime Managers & EF Data Context --> Best Practice

和自甴很熟 提交于 2019-11-29 02:01:44
All, There has been a lot of posts about Unity Lifetime Managers but I have yet to find someone state a good rule of thumb for "in these cases you should always use X". Let me describe my application, I have an ASP.NET MVC 4 Web Application. I have a Visual Studio solution containing 3 projects, my 'Core' project which has all of my EF stuff, a testing project, and the MVC Web Project. I am using Unity for dependency injection and have the following code right now: // Context container.RegisterType<IDatabaseFactory, DatabaseFactory>( new ContainerControlledLifetimeManager(); container

Using Unity Dependency Injection with WCF services

非 Y 不嫁゛ 提交于 2019-11-28 23:33:25
I have the following after doing some research on other questions: MyServiceHost: public class MyServiceHost : ServiceHost { public MyServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { if (container == null) { throw new ArgumentNullException("container"); } foreach (var cd in this.ImplementedContracts.Values) { cd.Behaviors.Add(new DependencyInjectionInstanceProvider(container)); } } } DependencyInjectionInstanceProvider: public class DependencyInjectionInstanceProvider : IInstanceProvider, IContractBehavior { private

Inject same DataContext instance across several types with Unity

无人久伴 提交于 2019-11-28 23:26:05
问题 Suppose I have IRepository interface and its implementation SqlRepository that takes as an argument LINQ to SQL DataContext. Suppose as well that I have IService interface and its implementation Services that takes three IRepository, IRepository and IRepository. Demo code is below: public interface IRepository<T> { } public class SqlRepository<T> : IRepository<T> { public SqlRepository(DataContext dc) { ... } } public interface IService<T> { } public class Service<T,T1,T2,T3> : IService<T> {

Specify constructor for the Unity IoC container to use

北城以北 提交于 2019-11-28 22:23:30
I'm using the Unity IoC container for resolving my objects. However, I've run into an issue. When I have more than one constructor - how does Unity know which one to use? It seems to use the one with parameters when I have one with and one without. Can I explicitly tell it which constructor to use? Specifically I had a case similar to the following Person class with two constructors. In this case I want the IoC container to use the default constructor - without parameters - but it chooses the one with parameters. public class SomeValueObject { public SomeValueObject(string name) { Name = name;

Cannot get rid of “physical connection is not usable” exception

戏子无情 提交于 2019-11-28 21:13:04
I am about to shoot myself. Spent few weeks now trying to solve this issue. We have an ASP.NET MVC 4 web app that uses SQL Server 2012 and Entity Framework as ORM and Unity for IoC. Web app is hosted on Amazon EC2. I started getting "Physical connection is not usable" exception. It happens few times a day. I searched many articles and forums and tried all the possible suggestions: Tried removing pooling from connection string "Polling=False" Tried limiting pool size and connection lifetime Tried changing LifetimeManager of Unity to HierarchicalLifetimeManager, PerRequestLifetimeManager. Also

How does Unity.Resolve know which constructor to use?

霸气de小男生 提交于 2019-11-28 18:36:43
Given a class with several constructors - how can I tell Resolve which constructor to use? Consider the following example class: public class Foo { public Foo() { } public Foo(IBar bar) { Bar = bar; } public Foo(string name, IBar bar) { Bar = bar; Name = name; } public IBar Bar { get; set; } public string Name { get; set; } } If I want to create an object of type Foo using Resolve how will Resolve know which constructor to use? And how can I tell it to use the right one? Let's say I have a container with an IBar registered - will it understand that it should favor the constructor taking IBar?

Is MEF a dependency injection framework?

人走茶凉 提交于 2019-11-28 18:13:58
The recently announced managed extensibility framework (MEF) of .NET 4.0 - is it a dependency injection framework? Will Microsoft Unity from Patterns and Practices be obsolete in 4.0 ? How does MEF compare to a framework like Unity? Specifically addressed in the PDC 2008 2nd Keynote by Scott Guthrie, MEF has a lot more to do with things like extending Visual Studio 2008 and other applications, without having to use all the COM and older technologies... A very good demonstration of extending the text edition in VS2008 was shown among other things. Start about an hour and 15 minutes into the 2nd

Best Practices for IOC Container

拈花ヽ惹草 提交于 2019-11-28 17:31:49
I'm using the Unity IOC container and I'm just wondering what is the best best way to access the container for multiple classes. Should every class have an IUnityContainer member and then pass the container in by constructor? Should there be a singleton class with an IOC container? How about asp.net development? Could somebody guide me in the right direction? Thanks. IMHO it is not advisable to inject the entire container into a class or to have an application wide static IoC service locator. You want to be able to see from the constructor of a class (lets call it Foo), what kind of services