Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with MEF - mef

In Caliburn.Micro documentation the authors mention such possibility:
documentation link
IHandle inherits from a marker interface IHandle. This allows the use of casting to determine if an object instance subscribes to any events. This enables simple auto-subscribing if you integrate with an IoC container. Most IoC containers (including the SimpleContainer) provide a hook for being called when a new instance is created. Simply wire for your container’s callback, inspect the instance being created to see if it implement IHandle, and if it does, call Subscribe on the event aggregator.
How is it possible to achieve this with MEF?
This question is the same as Caliburn.Micro. Automatically call eventaggregator.Subscribe() for IHandle implementors with Autofac
So how is similar functionality as the described AutoSubscribeHandersModule implemented in MEF?

I blogged about how to do the auto-wiring for the Event Aggregator with MEF in Caliburn Micro here;
http://www.kjetilk.com/2011/10/auto-wiring-eventaggregator.html.
In short; You need to add the MEFContrib (nuget -> Install-Package MefContrib), implement an IExportedValueInterceptor that subscribes any IHandle instances, and plug the interceptor into the MEF creation pipeline using an InterceptionCatalog in the bootstrapper.

Check MEFContrib's InterceptingCatalog. Just put IHandle instead of IStartable as described in the referenced article.

Related

Injecting services into Scala Akka Actors with Google Guice

I have a couple of services that I want to inject into akka actors. There are three different types of actors I am working with, and each type will use different services. Currently I just have a module, instantiate an injector inside of the actor, and do the binding inside of each Crow. The issue is that for each other, they receive a new instance of the service.
I did a little bit of reading and found http://www.typesafe.com/activator/template/activator-akka-scala-guice
but the documentation for akka recommends we not use IndirectActorProducer. What is the best way for me to inject these services into my actors? The #Inject keyword looks promising but I'm not exactly sure how to use this.
Workflow:
Main creates commander, sends it a command, commander creates the three different types of crows, and sends them messages to execute (it is these crows that require the services).
In your module use #Provides methods that accept the services as params and return ActorRefs. In these provider methods you instantiate Props from the injected services, but let the akka actually create the actor, so you don't need a child injector.
See my answer to https://stackoverflow.com/a/30901808/1341546 for an example.
I ended up using http://www.typesafe.com/activator/template/activator-akka-scala-guice as a guide. I found that it works pretty well, and it doesn't actually use IndirectActorProducer but rather another class extending it, which is what they recommend as well.

How to access a registrationService in a CustusX plugin?

In a custom plugin in custusX i use mServices->patientModelService->getPatientLandmarks()->setLandmark to programmatically alter some landmarks. I want to perform the registration with a already present volume.
In LandmarkPatientRegistrationWidget in org.custusx.registration.method.landmark, performRegistration() calls mServices.registrationService->doPatientRegistration().
However, I'm not sure whether my approach to get hold of a registrationService instance is right.
I have so far added org_custusx_registration to the CMakeLists.txt file and added "cxRegistrationService.h" and <cxRegServices.h> as includes.
Now I can define a RegServices mRegServices and initialize it in the constructor with the mContext of the plugin.
But do I create an own registration service or do I get access to the already existing? How can I get access to the already running registration service?
Your method correctly reuses the existing running registration service.
The default setup of CustusX register a single instance (object) implementing the cx::RegistrationService interface. The cx::RegServices helper class contains a cx::RegistrationServiceProxy, which acts as a smart pointer referring the object. Service objects are only created by the plugin that implement them: Users simply get access to these objects.
The RegistrationServiceProxy implements this using a ctkServiceTracker and related classes, see for example this tutorial on OSGi, section 5.4 (in java, but the principles apply).

Implementing Chain of Responsibility with Services

I'm thinking about a platform-neutral (i.e. not .NET MEF) technique of implementing chain-of-responsibliity pattern using web services as the handlers. I want to be able to add more CoR handlers by deploying new services and not compiling new CoR code, just change configuration info. It seems the challenge will be managing the metadata about available handlers and ensuring the handlers are conforming to the interface.
My question: any ideas on how I can safely ensure:
1. The web services are implementing the interface
2. The web services are implementing the base class behavior, like calling the successor
Because, in compiled code, I can have type-safety and therefore know that any handlers have derived from the abstract base class that ensures the interface and behavior I want. That seems to be missing in the world of services.
This seems like a valid question, but a rather simple one.
You are still afforded the protection of the typing system, even if you are loading code later, at runtime, that the original code never saw before.
I would think the preferred approach here would be to have something like a properties file with a list of implementers (your chain). Then in the code, you are going to have to have a way to instantiate an instance of each handler at runtime to construct the chain. When you construct the instance, you will have to check its type. In Java, for instance, that would take the form of instanceof (abomination ordinarily, but you get a pass for loading scenarios), or isAssignableFrom. In Objective C, it's conformsToProtocol.
If it doesn't, it can't be used and you can spit an error out to the console.

how to create singleton instance per thread using MEF

I am new in MEF. I am working on a C# WPF application and using Prism with MEF.
Please tell me how can I create single instance per thread of a class using MEF.
Thanka a lot
DJ
In the current version of MEF, you would need to develop a complex hierarchy of containers in order to accomodate this requirement (singleton per thread). Since you are new to MEF, this would be quite tricky to implement. Here is a link with more information about this:
http://codebetter.com/glennblock/2009/08/16/should-i-use-mef-for-my-general-ioc-needs/
I believe one of the new options in the new version of MEF (currently called MEF2) will be better options for how to handle object instantiation. You can read more about it at the BCL blog or here:
http://mef.codeplex.com/

Understanding FP in an enterprise application context (in Scala)

Most examples (if not all) that I see are the sort of a function that does some sort of computation and finishes. In that aspect, FP shines. However, I have trouble seeing how to apply it in the context of an enterprise application environment where there's not much of algorithms going on and a lot of data transfer and services.
So I'd like to ask how to implement the following problem in FP style.
I want to implement an events bus service. The service has a register method for registering listeners and publish for publishing events.
In an OO setting this is done by creating an EventBus interface with both methods. Then an implementation can use a list to hold listeners that is updated by register and used in publish. Of course this means register has a side effect. Spring can be used to create the class and pass its instance to publishers or subscribers of events.
How to model this in FP, given that clients of the event bus service are independent (e.g., not all are created in a "test" method)? As far as I can see this negates making register return a new instance of EventBus, since other clients already hold a reference to the old instance (and e.g., publishing to it will only publish to the listeners it knows of)
I prefer a solution to be in Scala.
I think you should have a closer look at functional reactive programming techniques. Since you want something in Scala, I suggest reading Deprecating The observer pattern paper by Ingo Maier, Tiark Rompf and Martin Odersky.
The sketch of the solution is that publish should return IO[Unit]. Listeners should be iteratees. Registration also returns IO[Unit].