This question is in regard to the dependency injection feature of Spring.NET. Assume that I have the following object dependencies:
A depends on B (via the property A.B), and B depends on C (via the property B.C)
In my Spring.NET configuration I can define objects A, B, and C, and use "ref" to wire up the A.B and B.C properties.
Now, assume that I already have an instance of C that I would like to use. Is there a way to have Spring.NET construct A and B from the configuration, but use the existing C? Ideally I would like for Spring.NET to skip the construction of a new C object, and set the B.C property to the existing C instance automatically.
Yes, if you already have an instance of C, you can register that instance under a given name.
((IConfigurableApplicationContext) context).ObjectFactory.RegisterSingleton("C", cInstance);
Related
I am passing the first object of a List myList[] from Class A to a stateful class B that is a separate page in my application. The passed object is therefore created as immutable since flutter prefers immutable classes and immutable classes need const constructors, variables in which must be final. In the B class, I later call a function that modifies the object from the List, and objects in a list aren't immutable. But since I have already passed the object, I can't see the updated value. I solved this issue by making: the value - non-final, the constructor - not const, and therefore the class - mutable. Another way I could solve it is to just use global variables instead.
My question is - Is there a way to reinstantiate class B? I.e., go to class A, navigate to page B by passing the (now updated) first object of List myList[] ?
Perhaps you could try using a State manager like Provider. From my understanding, you're trying to share variables across different widgets. Using a state management library seems to do exactly what you want.
I am trying to understand DI. Since I don't write unit tests (yet), the biggest advantage for me is the decoupling of classes and the management/control of dependencies.
But there's one question: what if I have a class A (controller), that instantiates class B (a listener), and class B will - under certain circumstances - instantiate class C (a mailer)?
According to the DI principle, I have to create C and pass it to B. What if I don't need C during a request? Do I have to create some logic for the Dependency Injection first?
According to the DI principle, I have to create C and pass it to B. What if I don't need C during a request? Do I have to create some logic for the Dependency Injection first?
This shouldn't be a problem because injection constructors should be simple:
An Injection Constructor should do no more than receiving the dependencies.
When you do this, object creation is really fast and reliable, and it doesn't matter whether or not a consumer always uses all of its dependencies.
Let's admit I want to map an object A to an object B with Orika.
One field of the object B is constructed by a call to an external webservice with a parameter I have in object A.
Is it a good practice to create an Orika custom-mapper which uses a service that makes a call to an external service, or should I separate this behaviour in an external class ?
External service
^
|
[A]-----> Custom Mapper ------> [B]
Custom mappers in Orika can be regular Spring Beans there is already a lot of example on how to create Spring based Orika mapper
Take a look of this example:
https://github.com/dlizarra/orika-spring-integration
UPDATE
Yes absolutely.
We have a system which has a lot of model objects (e.g. Car, Pedestrian, Road, ...)
Currently all of them have managers (CarManager, PedestrianManager, RoadManager) that return a singleton of the respective class.
An alternative proposed is to have a ManagerFactory singleton that can return instances of CarManager, PedestrianManager, RoadManager. (e.g. ManagerFactory.getInstance().getCarManager())
We also write test for the project and the concern was that if we will use Dependency Injection we will need an actual instance of an object to inject managers.
Is this alternative a good one? Would you change the singleton into something else in this case?
A singleton directly or a singleton factory are basically the same thing - an opaque reference to something - a hidden dependency. With a global text search you can find these dependencies so neither option makes the situation better or worse.
Dependency injection means that you're publicly declaring that for instance A to work it needs an instance of B (or an instance which conforms to protocol C is a better dependency situation). This requires that you instantiate B somewhere and pass it to A.
From a test point of view dependency injection is far superior, because you generally want to create a mock version of B and use that to test A. The test injects the instance to use. Testing singletons is a pain...
So, ideally, the first class involved would create an instance of B and pass it to the other classes that need it, and that instance gets passed on from there.
I'm putting a project together with Spring.NET and Caliburn v2. I have some objects that I'm trying to instantiate, but not sure how to go about it.
I have been using Caliburn's IoC aspect annotations (Singleton and PerRequest) to get objects into the Spring context. The problem with this is that I have two objects, A and B, where Object B is a subclass of Object A (meaning B is also an A). This means that if I register both, Spring complains of ambiguity when an object of type A is requested. To get around this, I could stop using Caliburn's IoC aspects to register the objects and instead register them in the Spring context XML files. That way I can specify a named object in the Spring context file to use in the constructor of object C, which needs an object of type B injected.
However, this creates a new problem. Object B needs the Caliburn window manager to be injected (which is not available to the Spring container at the time when the objects listed in the context XML files are instantiated, but only later, after Caliburn has loaded and added its own objects to the Spring container).
I could simply remove the inheritance and let some code duplication occur between objects A and B, but then what would be the point of doing OO programming? Otherwise I guess I'm looking for a way to specify objects in Spring.NET context XML, but keep them from being resolved until after Caliburn has loaded.
Any ideas?
I'm not familiar with Caliburn but if you want to delay instantiation then you could mark your objects in xml as lazy-init, like this:<object id="foo" type="..." lazy-init="true"/>
This way they will be instantiated when you first request them
I managed to solve this by maintaining a separate list of caliburn-dependent spring context XML files. I loaded these into the ApplicationContext object by adding the following code at the beginning of the overridden DisplayRootView() method in my application's bootstrapper:
var objectDefinitionReader = new XmlObjectDefinitionReader(applicationContext);
objectDefinitionReader.LoadObjectDefinitions(GetCaliburnDependentContextFiles());
applicationContext.Refresh();