unity-container

How to setup a Unity Registration Convention?

独自空忆成欢 提交于 2019-12-09 15:18:20
问题 With structure map, you can register a convention that lets you not just tweak the type, but also intervene during object creation. How can I do this with Unity. public class SettingsRegistration : IRegistrationConvention { public void Process(Type type, Registry registry) { if (!type.IsAbstract && typeof(ISettings).IsAssignableFrom(type)) { registry.For(type).Use(x => { var svc = x.GetInstance<ISettingService>(); return svc.LoadSetting(type); }); } } } 回答1: You can do this with a combination

How to inject dependency property using Ioc Unity

百般思念 提交于 2019-12-09 15:06:50
问题 I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { return "MethodA1() " +serviceB.MethodB1(); } } public class ServiceB : IServiceB { public string MethodB1() { return "MethodB1() "; } } I use Unity for IoC, my registration looks like this: container.RegisterType<IServiceA, ServiceA>(); container.RegisterType<IServiceB,

How can I pass in constructor arguments when I register a type in Unity?

久未见 提交于 2019-12-09 14:36:54
问题 I have the following type being registered in Unity: container.RegisterType<IAzureTable<Account>, AzureTable<Account>>(); The definition and constructors for AzureTable are as follows: public class AzureTable<T> : AzureTableBase<T>, IInitializer where T : TableServiceEntity { public AzureTable() : this(CloudConfiguration.GetStorageAccount()) { } public AzureTable(CloudStorageAccount account) : this(account, null) { } public AzureTable(CloudStorageAccount account, string tableName) : base

The type String cannot be constructed

限于喜欢 提交于 2019-12-09 14:09:14
问题 I'm using Web.api and Unity and I am getting the following error when trying to open the default "help" area: [InvalidOperationException: The type String cannot be constructed. You must configure the container to supply this value.] Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor) +280 Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext

Dependency Injection composition root and decorator pattern

雨燕双飞 提交于 2019-12-09 08:28:29
问题 I'm getting StackoverflowException 's in my implementation of the decorator pattern when using dependency injection. I think it is because I'm "missing" something from my understanding of DI/IoC. For example, I currently have CustomerService and CustomerServiceLoggingDecorator . Both classes implement ICustomerService , and all the decorator class does is use an injected ICustomerService but adds some simple NLog logging so that I can use logging without affecting the code in CustomerService

Make sure that the controller has a parameterless public constructor in Unity

有些话、适合烂在心里 提交于 2019-12-09 03:11:19
问题 I got this problem with the Controller: An error occurred when trying to create a controller of type ' * .WebMvc.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor. Find the solution for the ApiController, but didn't find anything about normal Controller. Created new MVC 4 project from scratch. HomeController.cs: public class HomeController : Controller { private IAccountingUow _uow; public HomeController(IAccountingUow uow) { _uow = uow; }

Getting Unity to Resolve views in XAML

◇◆丶佛笑我妖孽 提交于 2019-12-09 00:48:25
问题 I'm starting out with MVVM, and I'm starting to understand things. I'm currently experimenting with the Cinch framework, though I'm not committed to it as of yet. I was injecting the ViewModels into the Views using by having a reference to the ViewModel in the codebehind of the view, with the property having a [Dependency] on it, and in the setter it sets the DataContext to the right view, using Unity. Neat trick, I thought. I'm trying to get my app to work as a single Window, with injected

Unity (dependency injection): How to pass in a parameter to the constructor in RegisterType

别来无恙 提交于 2019-12-08 19:49:23
问题 Can anyone help? I have a wpf app (shouldn't matter) and in the Onstart i have my bootstrap stuff.. Its like this.. // Create unity container my service and repository container = new UnityContainer() .RegisterType<ISecurityRepository, SecurityRepository>() .RegisterType<ISecurityService, SecurityService>(); Basically ISecurityService expects me to pass in a ISecurityRepository, hence the above fails. But i am little confused, do i have to create a new IsecurityRespository and then pass it in

How to inject my dbContext with Unity

安稳与你 提交于 2019-12-08 19:14:20
问题 How do I inject my dbContext class using Unity? I can't just create a Interface like for my other "normal" classes? What should I do with my RequestContext class and what should my UnityConfig look like? public class RequestContext : IdentityDbContext<User> { public RequestContext() : base("DefaultConnection", throwIfV1Schema: false) { Database.SetInitializer<RequestContext>(new CreateDatabaseIfNotExists<RequestContext>()); } public DbSet<Request> Requests { get; set; } public DbSet<Record>

Unity to dispose of object

微笑、不失礼 提交于 2019-12-08 18:14:02
问题 Is there a way to make Unity dispose property-injected objects as part of the Teardown? The background is that I am working on an application that uses ASP.NET MVC 2, Unity and WCF. We have written our own MVC controller factory that uses unity to instantiate the controller and WCF proxies are injected using the [Dependency] attribute on public properties of the controller. At the end of the page life cycle the ReleaseController method of the controller factory is called and we call