unity-container

How to use Unity.RegisterType with Moq?

痞子三分冷 提交于 2019-12-12 07:44:52
问题 I have a running code with unity. Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following: IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo>(new InjectionConstructor("xxx")); Now I wrote testcode with Moq: IUnityContainer container = new UnityContainer(); var mockFoo = new Mock<IFoo>(); container.RegisterType<IFoo, mockFoo) >(new InjectionConstructor("xxx")); but this don't work. Error: The type 'Moq.Mock'

Unregister service from unity

六眼飞鱼酱① 提交于 2019-12-12 06:17:28
问题 I'm using Unity within my application. For one integration test, we want to change one of the service registered to the application(because it would require some hardware). So, I'm able to Register the new "mock" type, but I'm unable to remove the other implementation registered for this interface. Also to mention, currently we register a "list" of this interface(it's some kind of driver) and we would like to remove all the others instance. Any idea how I could do this? 回答1: You can simply

C# Dependency Injection passing additional parameter

此生再无相见时 提交于 2019-12-12 06:12:39
问题 I have a class similar to the following: public class CarAttributes { private readonly ICarRepository _carRepository; private readonly int _carId; public CarAttributes(ICarRepository carRepository, int carId) { _carRepository = carRepository; _carId = carId; } public bool IsRegistered { get { return _carRepository.IsRegistered(_carId); } } public bool IsStolen { get { return _carRepository.IsStolen(_carId); } } } I also have the following method (which is syntactically incorrect) public

How would I pass a constructor parameter at runtime to my dbContext and also register them with Unity as such

两盒软妹~` 提交于 2019-12-12 05:44:33
问题 I am trying to implement all sorts of good stuff like UnitOfWork, Repository, DI. I am using Unity for DI. Here is my dilemma. I have a few (currently 3) databases with identical schema but obviously with different data for business reasons (I will call them GroupDB1, GroupDB2 and GroupDB3). I also have a Master Database (DifferentDB) that has a different schema. My dbcontext need to use different databases for different scenarios at runtime. I have no clue how to put them all to work

Using Unity to create a singleton that is used in my Class

寵の児 提交于 2019-12-12 05:28:38
问题 I have a class that needs an instance of a HttpClient class, which I want to be instantiated only once i.e. Singleton public interface IMyClass {} public class MyClass : IMyClass { private HttpClient client; public MyClass(HttpClient client) { this.client = client; } } IUnityContainer container = new UnityContainer(); container.RegisterType<IMyClass, MyClass>(); var httpClient = new HttpClient(); How can I register the httpClient instance as a singleton to my MyClass can use it? 回答1: Have you

Injecting Dependency into Web API Controller

耗尽温柔 提交于 2019-12-12 05:25:23
问题 I want to inject unity container into WebController. I have UnityDependencyResolver: public class UnityDependencyResolver : IDependencyResolver { readonly IUnityContainer _container; public UnityDependencyResolver(IUnityContainer container) { this._container = container; } public object GetService(Type serviceType) { try { return _container.Resolve(serviceType); } catch { return null; } } public IEnumerable<object> GetServices(Type serviceType) { try { return _container.ResolveAll(serviceType

Entity Framework 6 - Dependency Injection with Unity - Repository pattern - Add or Update exception for many to many relationship

回眸只為那壹抹淺笑 提交于 2019-12-12 04:55:23
问题 I have a problem when adding new values with a many to many mapping in Entity Framework. I know about the unit of work pattern but in our solution we would like to keep a simple repository pattern and not a unit of work class that contains everything. Is this possible or should I just implement Unit of Work right away? If I don't use iSupplierRepository below a supplier will be added, but it will always add a new one even though there already exists one with that name. Error: The relationship

Unity get Type of the service where the dependecy gets injected

旧时模样 提交于 2019-12-12 04:44:45
问题 Is it possible to get, in Unity, the Type of the service where the dependecy gets injected? In Ninject you can do it like this: kernel.Bind<ILogger>().ToMethod((context) => { ILogger logger = HttpContextLoggerFactory.GetInstance(); // Eg: MyApplication.PeopleRepository string memberType = context?.Request?.Target?.Member?.DeclaringType?.FullName; return new LoggerMemberTypeDecorator(logger, memberType); }); How can the same thing be implemented in Unity ? Here is what i have, but i do not

Connection String errors in C# Web Api

会有一股神秘感。 提交于 2019-12-12 04:44:34
问题 I am creating a Web Api in C#.Net. I have implemented Dependency Injection in it using Unity.Mvc5 and using Db First approach. Since then i'm facing some issues in connection strings. There is a default AccountController in it and I have created a TestController to test my Api's. There are two connection strings in my Web.config file (one of them is commented but just to show here i have uncommented it). <add name="DDEXEntities" connectionString="Data Source=DESKTOP-CSB6551;initial catalog

Accessing a static variable from different assemblies

十年热恋 提交于 2019-12-12 03:24:53
问题 I have an ASP.NET application and a Windows Service. I am using Unity as the IoC container. I placed the Composition Root in a seperate class library, because both applications are supposed to use the same DI container. DI bootstrapper: namespace CompositionRoot { public static class DiBootstrapper { private static IUnityContainer _container; public static IUnityContainer Initialize() { return _container ?? (_container = BuildUnityContainer()); } private static IUnityContainer