inversion-of-control

Register string value for concrete name of parameter

六月ゝ 毕业季﹏ 提交于 2019-12-05 15:33:06
I am using Autofac and I have several classes which ask for parameter of type string and name lang. Is there a way to register a string value to all parameters named "lang", so it resolves automatically? I do not want to edit any of the constructors, since it is not my code (I know accepting e.g. CultureInfo would make registration easy..) Something resulting in short syntax like builder.Register(lang => "en-US").As().Named("lang") would be ideal. Thank you. A fairly simple way to solve this is with a custom Autofac module. First, implement the module and handle the IComponentRegistration

Platform-specific IoC in MVVMCross

╄→尐↘猪︶ㄣ 提交于 2019-12-05 14:25:22
I'm giving a MVVMCross a spin, to see if it will be of use in some bigger projects coming up, and it's great. I like the navigation, viewModel location and general cross-platform approach, which is just what I need. However, I'm a bit stuck on splitting out some of the dependency injection depending on the platform. So, we have the basic application, with a shared portable library, that initialises the service references when starting up: public TwitterSearchApp() { InitaliseServices(); } private void InitaliseServices() { this.RegisterServiceInstance<ITwitterSearchProvider>(new

How to turn this Service Locator pattern into true Dependency Injection pattern?

强颜欢笑 提交于 2019-12-05 13:37:30
I asked a more general question a minute ago: How to organize DI Framework usage in an application? , and the feedback I got was that I was using a Service Locator Pattern rather than true DI as is pointed out by Martin Fowler here: http://martinfowler.com/articles/injection.html Actually, I read that article just the other day, but apparently haven't quite grasped it. So let's say I have the following code: interface ICardReader { string GetInfo(); void SetDebugMode(bool value); void Initialize(string accountToken); void ShowAmount(string amount); void Close(); ICreditCardInfo GetCardInfo();

Ninject conditional binding

廉价感情. 提交于 2019-12-05 12:45:52
I'm playing around with Ninject for a simple test-bed project at home, just to see what I can do with it. As a starting point I'm building a console runner for some service, which accepts a variety of arguments and based on what it gets in, uses the same methods provided for a fluent interface to configure a model to run. As an example, suppose I have a verbosity switch, /o . /o can be passed as /o:quiet , /o:normal , or /o:verbose . The various options are self-explanatory. To satisfy this argument I would like to attach various implementations of ILogger - quiet gets a quiet logger that

How to configure Unity to inject an array for IEnumerable

懵懂的女人 提交于 2019-12-05 12:39:31
I have a class which takes an IEnumerable constructor parameter which I want to resolve with Unity and inject an array of objects. These simple classes illustrate the problem. public interface IThing { int Value { get; } } public class SimpleThing : IThing { public SimpleThing() { this.Value = 1; } public int Value { get; private set; } } public class CompositeThing : IThing { public CompositeThing(IEnumerable<IThing> otherThings) { this.Value = otherThings.Count(); } public int Value { get; private set; } } Say I want to inject four SimpleThing in to CompositeThing . I've tried several

Property Inject an Array with Spring.Net

淺唱寂寞╮ 提交于 2019-12-05 12:35:44
I've been using the Spring.Net IoC container and can use it to inject properties that are of type IList and even IList<T> but I'm a bit stumped as to how to inject a property thats of type string[] . There doesn't seem to be an <array> element defined in the XSD's and using <list> <value> </list> doesn't work either. If anyone could post the xml I need to inject using an array for a property it'd be much appreciated As mentioned here in the documentation you can inject a string array as a comma delimited string (not sure what the syntax is for escaping actual commas in strings if necessary).

Register Generic Type in Unity Based On Concrete Type

泪湿孤枕 提交于 2019-12-05 11:54:33
I'm trying to emulate a behavior that I can configure in Ninject, only using Unity instead. I am attempting to use the Cached Repository Pattern, given the following classes and interface: public interface IRepository<T> { T Get(); } public class SqlRepository<T> : IRepository<T> where T : new() { public T Get() { Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name); return new T(); } } public class CachedRepository<T> : IRepository<T> where T : class { private readonly IRepository<T> repository; public CachedRepository(IRepository<T> repository) { this.repository = repository; }

Define an initialization order of WebActivator.PreApplicationStartMethod classes

风流意气都作罢 提交于 2019-12-05 10:51:03
I have several WebActivator.PreApplicationStartMethod decorated classes. One is for Ninject, another class for AwesomeMVC and a third one is for background task scheduler. The problem is that the scheduler class needs to take advantage of the dependecies, that are resolved by IoC container. My questions are: Can I have several WebActivator.PreApplicationStartMethod classes? Can I define order, in which they are initialized, so that IoC, being the most important, comes first? Can WebActivator.PreApplicationStartMethod static class instances rely on IoC container to resolve their constructor

How to resolve type at run time to avoid multipe if else

一世执手 提交于 2019-12-05 08:29:47
I have my code which makes a webservice Call based on type of request. To do that , I have following code; public class Client { IRequest request; public Client(string requestType) { request = new EnrolmentRequest(); if (requestType == "Enrol") { request.DoEnrolment(); } else if (requestType == "ReEnrol") { request.DoReEnrolment(); } else if (requestType == "DeleteEnrolment") { request.DeleteEnrolment(); } else if (requestType == "UpdateEnrolment") { request.UpdateEnrolment(); } } } So as per open close principle, I can subclass like: Class EnrolmentRequest:IRequest { CallService(); } Class

How do I inject a constructor dependency into a ViewModel using Xamarin and Autofac?

泄露秘密 提交于 2019-12-05 08:19:17
I have a ViewModel and I want to inject another Class into it. I am using Visual Studio with the latest version of Xamarin. I'm using Autofac for registering en resolving dependencies. But I'm new to it and I'm facing a problem which I can't find the solution to, even though it's probably simple. This is the Class in which I want to inject another Class: public IMessagingCenterWrapper MessagingCenterWrapper; public LoginViewModel(IMessagingCenterWrapper messagingCenterWrapper){ MessagingCenterWrapper = messagingCenterWrapper; } Then in entry point of the app I have a function which initializes