unity-container

Dependency injection with unity in a class library project

笑着哭i 提交于 2021-02-18 17:35:12
问题 I am new with the dependency injection pattern. I'm a little confused about few things. Scenario: I have a class library project called 'MailCore'. This project has interfaces and classes that perform all email sending stuff. I have an MVC project called 'The site'. It uses the 'MailCore' project to send email. I have Unity in this project and the UnityContainer is registered and things are working fine. I also have another class library library project called 'SiteRepo'. Sometimes, to

how to implement IOC without a global static service (non-service locator solution)?

跟風遠走 提交于 2021-02-17 08:44:16
问题 we want to use Unity for IOC. All i've seen is the implementation that there is one global static service (let's call it the the IOCService) which holds a reference to the Unity container, which registers all interface/class combinations and every class asks that object: give me an implementation for Ithis or IThat. Frequently i see a response that this pattern is not good because it leads to a dependency from ALL classes to the IOCService (not to the Unity container because it is only known

Multiple DirectoryModuleCatalog in a Prism application

倖福魔咒の 提交于 2021-02-10 16:00:56
问题 We have Unity+Prism WPF applications, and there is 2 folders from which we want to load dynamically the modules. I know that to load modules from a directory, I should use a DirectoryModuleCatalog , but the CreateModuleCatalog method allows only one catalog to be returned, so how do I wrap them in one catalog? In my boostrapper, what should I return here: protected override IModuleCatalog CreateModuleCatalog() { new DirectoryModuleCatalog() { ModulePath = @".\Services" }; new

Multiple DirectoryModuleCatalog in a Prism application

北城以北 提交于 2021-02-10 15:58:59
问题 We have Unity+Prism WPF applications, and there is 2 folders from which we want to load dynamically the modules. I know that to load modules from a directory, I should use a DirectoryModuleCatalog , but the CreateModuleCatalog method allows only one catalog to be returned, so how do I wrap them in one catalog? In my boostrapper, what should I return here: protected override IModuleCatalog CreateModuleCatalog() { new DirectoryModuleCatalog() { ModulePath = @".\Services" }; new

Unity DI auto-registration from specific Assembly

冷暖自知 提交于 2021-02-07 20:29:05
问题 I'm trying to use the Unity Registration by Convention feature. I can't figure out how to register from specific assembly/project files with the file ending "Repository". container.RegisterTypes( AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled); I've found only this example from a MSDN blog ( https://blogs.msdn.microsoft.com/agile/2013/03/12/unity-configuration-registration-by-convention/ ) post and as I understand

IoC Container. Inject container

旧街凉风 提交于 2021-02-04 19:56:57
问题 What I want: Resolve object A, and inside object A, I want use same container to resolve object C: public static void Work() { IUnityContainer con = new UnityContainer(); con.RegisterType<IA, A>(); con.RegisterType<IB, B>(); con.RegisterType<IC, C>(); var a = con.Resolve<IA>(); } interface IA { } interface IB { } interface IC { } class A : IA { public A(IB b, IUnityContainer con) { for (int i = 0; i < 10; i++) { var c = con.Resolve<IC>(); } } } class B : IB { }; class C : IC { }; Problem: On

How do I register components at run time with unity DI

萝らか妹 提交于 2021-01-29 08:23:13
问题 I created an Microsoft unity project (my first using Dependency Injection) with my home controller working fine as expected having registered my messaging Class as follows private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<IMessageService, MessageService>(new HierarchicalLifetimeManager()); RegisterTypes(container); return container; } The challenge is I intend making this project a parent to several modules that will be plugins

Prism DI container - dispose unnecessary objects

半城伤御伤魂 提交于 2021-01-28 06:09:02
问题 I am facing problem with disposing unnecessary objects from DI container. I use Prism for Xamarin.Forms with Unity container. Application gets configuration from some database, creates some services using this configuration and registers this services in container using ContainerControlledLifetimeManager. This services are used while resolving views and viewmodels. When configuration changes application retrieves again changed configuration and now problem comes: how can I remove previous

Prism 7. Using PrismApplication.CreateShell() with a non Window control

一世执手 提交于 2021-01-27 20:55:19
问题 I would like to update from 6.3 to 7. I seem to have hit a road block. When using the PrismApplication class in the App.xaml, CreateShell expects a return type of Window instead of the previous BootStrapper which wanted a DependencyObject. My MainShell is a modified Telerik RadWindow which itself is a modified System.Windows.Controls.HeaderedContentControl and casting to Window is not possible. Is there a way around this so I can use the PrismApplication object or do I have to roll back and

Entity Framework Error when using Unity

落花浮王杯 提交于 2021-01-27 16:31:24
问题 On trying to resolve my service class, I'm getting an error that DbContext cannot be constructed because it's an abstract class. The error message is here: Unity.Exceptions.ResolutionFailedException: 'Resolution of the dependency failed, type = 'MyService.MyClass', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, System.Data.Common.DbConnection, is an abstract class and cannot be constructed. Are you missing a type mapping