unity-container

Unity.wcf and InstanceContextMode.Single

二次信任 提交于 2019-11-28 07:52:15
问题 I am using Unity.WCF to inject dependencies for WCF service. Problem occurs when I set my service to InstanceContextMode.Single . I found on Google that when InstanceContextMode is set to Single , InstanceProvider is not called. There is also a workaround for this but I was wondering if there is some built-in support for this in Unity.WCF because apparently this is a well known problem. I found the information here: Enabling InstanceProvider for singleton services. 回答1: I will cite Paul Hiles

Unity: Implicit ResolvedParameter for unnamed registrations

房东的猫 提交于 2019-11-28 07:50:38
The UserService constructor has two parameters, a IUnitOfWork and a IUserRepository : public UserService(IUnitOfWork unitofWork, IUserRepository userRepository) { ... } I am using named registrations to differentiate between multiple instances of IUnitOfWork , so when registering the UserService with the Unity container, I need to explicitly specify the parameters using an InjectionConstructor : container.RegisterType<IUserService, UserService>( new InjectionConstructor( new ResolvedParameter<IUnitOfWork>("someContext"), new ResolvedParameter<IUserRepository>() ) ); Is it possible for new

Mvc 3/Unity 2 inject dependencies into a Filter?

六月ゝ 毕业季﹏ 提交于 2019-11-28 07:41:25
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? Brad Wilson has a good series on Service Location which includes how to create your own filter provider that can support dependency injection: http://bradwilson.typepad.com/blog/2010/07

Way to fill collection with Unity

依然范特西╮ 提交于 2019-11-28 07:37:41
I have two example classes class ClassToResolve { private List<CollectionItem> _coll; public ClassToResolve(List<CollectionItem> coll) { _coll = coll; } } class CollectionItem { //... } and I need to resolve ClassToResolve var classToResolve = new ClassToResolve( new List<CollectionItem>() { new CollectionItem(), new CollectionItem(), new CollectionItem() } ); Now I resolve it in a way var classToResolve = new ClassToResolve( new List<CollectionItem>() { unity.Resolve<CollectionItem>(), unity.Resolve<CollectionItem>(), unity.Resolve<CollectionItem>() } ); Is there a way to resolve

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

拈花ヽ惹草 提交于 2019-11-28 07:32:22
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, IDataStoreFactory { private DataStore _database; private int _userId; private string _connectionString; public

How to create objects using a static factory method?

此生再无相见时 提交于 2019-11-28 07:15:26
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? One way is to have RepositoryFactory implement IRepositoryFactory, then register that. Resolved types can get a factory, then call its

Unity auto-factory with params

♀尐吖头ヾ 提交于 2019-11-28 06:56:32
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 class TestLog { private Func<string, ILog> logFactory; public TestLog(Func<string, ILog> logFactory) {

How to pass Owin context to a Repo being injected into Api controller

落花浮王杯 提交于 2019-11-28 06:53:55
I've got a MVC WebApi owin (soft hosted) project, that uses Unity for resolving controller dependencies which look like this public class PacientaiController : ODataController { private readonly IEntityRepo<Ent.Pacientas> repo; public PacientaiController(IEntityRepo<Ent.Pacientas> repo) { this.repo = repo; } the problem I'm trying to solve - is how do I pass 'OwinContex' into a Repo. public class PacientasEntityRepo:IEntityRepo<Pacientas>,IDisposable { public PacientasEntityRepo(IOwinContext ctx) { ......... If I try to register it like this in the Startup.cs Container.RegisterType

Breaking SOLID Principles in multiple implementation of an Interface

一世执手 提交于 2019-11-28 06:46:36
问题 I am facing a problem with dependency inversion in a factory method and it is also breaking Open Closed principle. My code looks like below codes public interface IWriter { void WriteToStorage(string data); } public class FileWriter : IWriter { public void WriteToStorage(string data) { //write to file } } public class DBWriter : IWriter { public void WriteToStorage(string data) { //write to DB } } Now I an using a factory class to solve the object creation. It look like below code public

How do I correctly use Unity to pass a ConnectionString to my repository classes?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 05:27:18
I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I've come unstuck. This is my IoC class that'll handle the instantiation of my concrete classes to their interface types (so I don't have to keep called Resolve on the IoC container each time I want a repository in my controller): public class IoC { public static void Intialise(UnityConfigurationSection section, string connectionString) { _connectionString = connectionString; _container = new UnityContainer(); section.Configure(_container); } private static IUnityContainer _container