inversion-of-control

Is it possible to create a collections with Spring configuration?

被刻印的时光 ゝ 提交于 2019-12-06 16:38:21
Suppose I have a class MyClass which can be instantiated either with String or it have predefined static instances inside a class. Something like this: public class MyClass { public final static MyClass A = new MyClass("A"); public final static MyClass B = new MyClass("B"); public final static MyClass C = new MyClass("C"); ... public MyClass(String name) { ... } } Is it possible to create an ArrayList<MyClass> bean in Spring config somehow? Something like <bean id="sequence" class="...ArrayList"> <member class="...MyClass" value="A"/> <member ... /> .... </bean> UPDATE 1 Is it possible to

Make sure that the controller has a parameterless public constructor in web api?

只愿长相守 提交于 2019-12-06 16:07:27
I am struggling with this problem of dependency injection using untiy . I have implemented according to this link https://www.asp.net/web-api/overview/advanced/dependency-injection But got this error: { "Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'WebAPIController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,

In asp.net-mvc, is there a more elegant way using IOC to inject mutiple repositories into a controller?

与世无争的帅哥 提交于 2019-12-06 15:44:27
I have an asp.net-mvc website and i am using ninject for IOC and nhibernate for my ORM mapping Here is my IOC binding code: internal class ServiceModule : NinjectModule { public override void Load() { Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope(); } } and here is an example of how I am doing IOC into my controller code: public FAQController(IIntKeyedRepository<FAQ> faqRepository, IUnitOfWork unitOfWork) { _faqRepository = faqRepository; _unitOfWork = unitOfWork; } The issue is that up until now, each controller had a single table that it was pointing to so i

How to organize DI Framework usage in an application?

丶灬走出姿态 提交于 2019-12-06 15:29:28
EDIT: I forgot to move the kernel into a non-generic parent class here and supply a virtual method to access it. I do realize that the example below, as is, would create a plethora of kernel instances. I just learned how to do injection this past week and here's how I've got things set up currently: using Ninject; using System.Reflection; namespace Infrastructure { public static class Inject<T> { static bool b = Bootstrap(); static IKernel kernel; static bool Bootstrap() { kernel = new StandardKernel(); kernel.Load(Assembly.GetExecutingAssembly()); return true; } public static T New() { return

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory

狂风中的少年 提交于 2019-12-06 15:05:08
I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface: public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator(Type objectType); } public interface IObjectCreator { T CreateObject<T>(IDataRow data); bool SupportsType(Type type); } Implementation of the factory class could look like this, though I am not sure this is the way to go: public interface ObjectCreatorFactory:IObjectCreatorFactory { IEnumerable

Chaining layers with IoC, setting lower callback to upper and avoid circular reference

我们两清 提交于 2019-12-06 14:36:07
I have a scenario where I need a lower layer to be controlled by an upper layer much like a puppet master pulling on strings. The lower layer also will call back to the upper layer as some internal events are generated from time to time. I am using SimpleInjector, I inject the ILower in to the Upper constructor. I cannot inject the Upper in to the Lower as it would cause a circular reference. Instead I have a register callback function to link the two layers. However, I have to scatter my code with null checks. Are there any nicer ways or different architectures to achieve this linking of

Castle Windsor: How do you add a call to a factory facility not in xml?

只愿长相守 提交于 2019-12-06 14:18:23
问题 I know how to tell Castle Windsor to resolve a reference from a factory's method using XML, but can I do it programmatically via the Container.AddComponent() interface? If not is there any other way to do it from code? EDIT: There seems to be some confusion so let me clarify, I am looking for a way to do the following in code: <facilities> <facility id="factory.support" type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel" /> </facilities> <components> <component

What's the benefit of Spring xml ioc over Java instantiation? [closed]

懵懂的女人 提交于 2019-12-06 14:00:18
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Ok, this question is gonna get a lot of downvotes... I just saw this question where a guy is facing some issue with spring xml beanfactory thing. I would like to understand why this: <bean id="sessionFactory" class="org.springframework

An item with the same key has already been added when registering controller without a specific constructor signature

谁说我不能喝 提交于 2019-12-06 11:58:20
问题 This is really odd. Have an MVC 5 application using Autofac 3.3 that throws the An item with the same key has already been added error when I add a new controller that doesn't have specific constructor signatures. How do I go about debugging this? If the constructor looks like the following then when I navigate to the url for that controller it loads fine: public class ProductController : Controller { private readonly IServiceManager _serviceManager; public ProductController(IServiceManager

Unity: pass parameters to custom lifetime constructor, in xml configuration file

烈酒焚心 提交于 2019-12-06 11:14:35
I wrote my CustomLifetimeManager like this: public class CustomLifetimeManager <T> : LifetimeManager { private readonly string _arg; public CustomLifetimeManager(string arg) { _arg = arg; } } Now, it works easy configuring the container programmatically, but how add it in configuration file like the following? <type type="myTime" mapTo="myImpl"> <lifetime type="CustomLifetimeManager"/> </type> You need to add a second class: A TypeConverter. This class is responsible for taking a string and turning it into whatever type you want. Once you implement it, you can then do something like this in