unity-container

How to Inject Log4Net ILog implementations using Unity 2.0

依然范特西╮ 提交于 2019-11-27 02:09:41
问题 Ultimately this has to do with setting up log4Net but generically the problem is not logging specific. Generically what I am trying to figure out is how to do, in Microsoft Unity 2.0, something equivalent to what one gets with the Castle.Facilities.Logging.LoggingFacility. Namely the ability to declare a dependency on a logger and have the logger initialized with the Type of the object into which it is being injected. In the spirit of a test is worth a thousand words, here is what I need:

Unity 2.0 registering generic types via XML

≯℡__Kan透↙ 提交于 2019-11-27 01:57:42
问题 I am trying to register a generic type in a config file for Unity 2.0 but can't seem to get it right. I have been referring to the MS documentation here : http://msdn.microsoft.com/en-us/library/ff660933%28v=PandP.20%29.aspx#_Generic_Types The code looks like this: public interface IRepository<T> where T : class { ... } public class GenericRepository<T> : IRepository<T> where T : class { ... } public class BlogRepository : GenericRepository<BlogRepository> { ... } The XML config i have at the

Mvc 3/Unity 2 inject dependencies into a Filter?

十年热恋 提交于 2019-11-27 01:56:56
问题 How can i inject the following dependencies ?? public class Authenticate : AuthorizeAttribute { [Dependency] public IAuthenticate AuthenticateLibrary { get; set; } [Dependency] public ILibrary BaseLibrary { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { } } I am using Unity 2 to inject all the controllers. Is there a tutorial for Unity 2 and injecting dependencies into filters? 回答1: Brad Wilson has a good series on Service Location which includes how to

How do I inject a connection string into an instance of IDbContextFactory<T>?

强颜欢笑 提交于 2019-11-27 01:51:03
问题 I'm using Entity Framework 5 with Code First Migrations. I have a DataStore class which derives from DbContext : public class DataStore : DbContext, IDataStore { public int UserID { get; private set; } public DataStore(int userId, string connectionString) : base(connectionString) { UserID = userId; } public virtual IDbSet<User> Users { get; set; } // Rest of code here } And a factory class which creates instances of the DataStore class: public class DataStoreFactory : Disposable,

Unity auto-factory with params

瘦欲@ 提交于 2019-11-27 01:41:31
问题 I'm trying to figure out the correct way to inject an auto-factory which takes params, or even if this is possible with Unity. For example I know I can do this: public class TestLog { private Func<ILog> logFactory; public TestLog(Func<ILog> logFactory) { this.logFactory = logFactory; } public ILog CreateLog() { return logFactory(); } } Container.RegisterType<ILog, Log>(); TestLog test = Container.Resolve<TestLog>(); ILog log = test.CreateLog(); Now what I'll like to be able to do is: public

How to create objects using a static factory method?

你离开我真会死。 提交于 2019-11-27 01:35:29
问题 I know Unity can be configured to use a class' constructor to create an instance of a class (like below) but that's not what I want. container.RegisterType<IAuthoringRepository, AuthoringRepository>(); I would like to configure Unity to use a factory method with the windows identity passed as a parameter (ie: RepositoryFactory.CreateAuthoringRepository(WindowsIdentity.GetCurrent()) ) when resolving a type of IAuthoringRepository . How do i do this? 回答1: One way is to have RepositoryFactory

With Unity how do I inject a named dependency into a constructor?

你离开我真会死。 提交于 2019-11-27 00:34:20
I have the IRespository registered twice (with names) in the following code: // Setup the Client Repository IOC.Container.RegisterType<ClientEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository> ("Client", new InjectionConstructor(typeof(ClientEntities))); // Setup the Customer Repository IOC.Container.RegisterType<CustomerEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository> ("Customer", new InjectionConstructor(typeof(CustomerEntities))); IOC.Container.RegisterType<IClientModel, ClientModel>(); IOC

Unity with ASP.NET Core and MVC6 (Core)

谁说我不能喝 提交于 2019-11-27 00:23:49
问题 Update 09.08.2018 Unity is being developed here but I haven't had the time to test how it playes with the ASP.NET Core framework. Update 15.03.2018 This solution is for the specific problem of using ASP.NET Core v1 with Unity while using the .NET Framework 4.5.2 NOT the .NET Core Framework. I had to use this setup since I needed some .Net 4.5.2 DLLs but for anyone starting afresh I would not recommend this approach. Also Unity is not being developed any further (to my knowlage) so I would

Can someone explain Microsoft Unity?

旧巷老猫 提交于 2019-11-26 23:45:43
问题 I've been reading the articles on MSDN about Unity (Dependency Injection, Inversion of Control), but I think I need it explained in simple terms (or simple examples). I'm familiar with the MVPC pattern (we use it here), but I just can't really grasp this Unity thing yet, and I think it's the next step in our application design. 回答1: Unity is just an IoC "container". Google StructureMap and try it out instead. A bit easier to grok, I think, when the IoC stuff is new to you. Basically, if you

Register IAuthenticationManager with Unity

一世执手 提交于 2019-11-26 22:39:32
问题 I'm using Unity for Dependencies Injection and using Identiy Provider to manage the user login, register, email confirmation, etc. When I try to register a user, I have this problem: The current type, Microsoft.Owin.Security.IAuthenticationManager, is an interface and cannot be constructed. Are you missing a type mapping? I have no idea how to register this Interface (IAuthenticationManager) in my Unity container. I tried registering the interface with this code, but if I put it, I have other