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.
Related
This is similar to How to deserialize from a file to different class. But, I'm developing micro-services using Dropwizard.
I have two services, service A and service B. I have a message queue (RabbitMQ) setup between the two services.
I am trying to send an object of type Class A (defined in service A) from service A to service B. I have not imported class A in service B. However in service B i have Class B defined which is exactly same as Class A.
I am getting a ClassNotFoundException: Class A in service B when service B tries to deserialize and typecast the object to Class B.
I want the two jars to be as independent as possible.
Is there a way to do this.
You can use Apache Avro for serializing (predefined avro schema) your objects between two service or if you want to use a queue bewteen them. On top you will lower the needed network bytes, because apache avro serialization is really good.
You can also use CatainProto or ProtoBuffer, but I like the apache avro method.
This seems like a fundamental Web Services problem. However an elegant solution is elusive based on some research I have been able to do. I guess I am missing something here
I am using a WCF client to connect to a External web service of which I have no control. The external WS is java based. There are a bunch of assemblies which are provided to call the methods in web service. These assemblies have base classes and derived classes. The web service methods can take Base class as param whereas from the WCF Client I instantiate a Derived class and pass it to the method.
To simulate this scenario, I created a small project with one ClassLibrary which has a BaseClass and a DerivedClass with one method.
Then I create an asmx web service and add a HelloWorld method inside it. I add a reference to the ClassLibrary. This method takes a BaseClass type param.
Then I create a Service Reference to the ASMX web service. In the proxy class, I add a XmlSerializerFormatAttribute to the method if it is already not there.
From the WCF client, I call the ASMX web method
BaseClass bc = new Derived();
ServiceReference1.TestService ts = new WCFTest.ServiceReference1.TestService();
lbl1.Text = (c1.HelloWorld(bc));
The call fails with error
The type ClassLib.Derived was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
The only way I could call this web service method was by adding XmlInclude attribute to the BaseClass in the ClassLibrary.
In my scenario, this library is a dll provided by an external vendor. I cannot add attributes to its classes. I have looked a DataContractSerializer and KnownTypes and XmlSerializer ctor. However those solutions do not seem to be applicable in my scenario.
How can I make XMLSerializer see the Derived classes in the assemblies I have referencing in the WCF Client? Is there an elegant solution?
Thanks,
Hem
Including your own type mapping for an XmlSerializerOperationBehavior may just work, but I haven't tried this (see GetXmlMappings).
http://msdn.microsoft.com/en-us/library/system.servicemodel.description.xmlserializeroperationbehavior.aspx
Alternatively, forcing use of the DataContractSerializer via a DataContractSerializerOperationBehavior (as opposed to the XmlSerializerOperationBehavior it's using now) may work too, if you specify your own known types
http://msdn.microsoft.com/en-us/library/ms576751%28v=vs.85%29.aspx
Finally, as a last resort, you can force use of the DataContractSerializer using the DataContractSerializerOperationBehavior, then specify your own DataContractSurrogate to force use of the XmlSerializer where you can pass custom types to its constructor (which circumvents the requirement for the XmlInclude attribute).
http://msdn.microsoft.com/en-us/library/ms751540.aspx
Hope that helps.
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();
I'm new to this IoC and DI business- I feel like I get the concept if you are passing along objects that are of a global scope, but I don't get how it works when you need to pass around an object that is of a specific logical state. So, for instance, if I wanted to inject a person object into a write file command object- how would I be able to choose the correct person object dynamically? From what I have seen, I could default construct the object, but my disconnect is that you wouldn't use a default person object, it would need to be dynamic. I assume that the IoC container may just maintain the state of the object for you as it gets passed around, but then that assuems you are dealing in only one person object because there would be no thread safety, right? I know I am missing something, (maybe something like a factoryclass), but I need a little more information about how that would work.
Well, you can always inject an Abstract Factory into your consumer and use it to create the locally scoped objects.
This is sometimes necessary. See these examples:
MVC, DI (dependency injection) and creating Model instance from Controller
Is there a pattern for initializing objects created via a DI container
Can't combine Factory / DI
However, in general we tend to not use DI for Entities, but mostly for Services. Instead, Entities are usually created through some sort of Repository.
When you construct an service object (e.g. WriteFileService), you inject into it things it needs internally to complete it's job. Perhaps it needs a filesystem object or something.
The Person object in your example should be passed to the service object as a parameter to a method call. e.g. writeFileService.write(person)
I'm trying to hack my own dependency injection container in PHP, based on constructor injection. The container instantiates complex objects and inject them with the required objects based on the type hints in the constructor using reflection.
One thing I obviously stumbled upon is the fact that I can register multiple components that can be injected, of the same type (extending same class / implementing same interface(s)). For example, what if two objects both need distinct objects that both implement an Iterator interface. How do DI Containers typically deal with this? How do you let the container decide which of the objects with the ambiguous interfaces need to be injected in which of the complex objects?
Or is a single DI container only ment to be responsible for creating one type of complex object? In other words: for each complex object instantiate a distinct DI container. I can hardly imagine this is the intent, right?
What you're describing isn't DI, per se, but rather autowiring, a step further than plain DI. Normally with DI you explicitly configure which components get wired into what.
Autowired DI is only really desirable with coarse-grained components, where there's only one sensible implementation of the given type (e.g. DAOs). In cases where the type is ambiguous, then you either mark one of them as the "primary", or mark the others as "not candidates", or explicitly mark the dependency with the name of the component you need.
If it helps, you could read up on how Spring handles autowiring here and here
Depending on what you mean by your question, here's what Guice (a DI framework for Java) uses:
If you simply want a distinct object every time you have an injection request for Foo, then when wiring up your application don't bind Foo.class to a particular instance of Foo, but instead bind a Provider<Foo>, which is an object that creates a Foo on demand. Then, every place where Foo is injected will get a new instance. If you just bind Foo to a class instead of a Foo instance (say, Foo is an interface, and you bound it to RealFoo.class which is a class that implements Foo), you also get the same effect - a new instance is created every time. (This is the default, "unscoped" behavior. The behavior of Guice scopes is beyond the scope of this comment)
If however you need to have two instances of Foo that you construct when wiring up your application and then have the ability to say "This instance of Foo gets used when making a Bar or a Baz, whereas this other instance is used when making a Bumble", what you do is annotate the injection points and then when wiring up your app say something like:
Foo foo1 = new Foo("1");
Foo foo2 = new Foo("2");
bind(Foo.class).annotatedWith(Names.named("Bar")).toInstance(foo1);
bind(Foo.class).annotatedWith(Names.named("Baz")).toInstance(foo1);
bind(Foo.class).annotatedWith(Names.named("Bumble")).toInstance(foo2);
Where I assume that you've annotated the arguments to Bar's constructor with something like:
public Bar(#Named("Bar") Foo foo) { ...
And similarly for Baz and Bumble. Of course, if you annotate the constructor of Bar and Baz with the same thing, you can skip one of the bind lines.
I know there are annotation-processing frameworks for php based on reflection, or perhaps you can use a convention based on argument names.