method selector on one of many interceptors for a service registered in Castle.Windsor - inversion-of-control

Using Castle.Windsor, how would I go about adding a IProxyGenerationHook or selector for one of several interceptors defined for a specific service. For example consider the following component registration:
container.Register( _
Component.For(Of IDataLoader) _
.ImplementedBy(Of sqlldrDataLoader) _
.Interceptors(Of LoggingInterceptor, FancySchmancyInterceptor))
The IDataLoader service has several methods. I want the following:
a) LoggingInterceptor will intercept every method.
b) FancySchmancyInterceptor should only intercept a subset of methods as defined in a selector of some description.
Many thanks,
Ryan.

.SelectInterceptorsWith(s => s.Service<YourSelectorComponent>())

Related

Multiple selectors using Sling Servlets

Short version: How do I force the most matching on a servlet based on multiple selectors using Felix annotations for sling servlets?
I have a few servlets defined in an OSGI bundle. I'm using the org.apache.felix.scr.annotations.sling.SlingServlet annotations.
#SlingServlet(
methods={GET},
selectors {"v1"}
...
)
public class MyServlet extends SlingAllMethodsServlet {}
...
#SlingServlet(
methods={GET},
selectors {"v1","special"}
...
)
public class MySpecialServlet extends MyServlet {}
My problem is that I can not find a way to force MySpecialServlet to only handle requests when both selectors are present.
GET http://localhost/my/resource.v1.special.json
Sometimes it will handle requests for only the v1 selector.
GET http://localhost/my/resource.v1.json
It seems that after using posting a new jar through the felix webconsole, if I request the double selector resource.v1.special.json before any other resource, then MySpecialServlet will also continue to handle v1 only requests.
Is there a way I can force the more general servlet to handle the more general list of selectors using the current annotations? Am I missing some part annotation? I believe that this system might be using an older annotation. Perhaps it is worth migrating? I'm trying not to be too intrusive for this small task that I've been asked to do.
Bear with me if I've conflated parts of these technologies. I've just walked up to this problem and I'm still sorting it out. Please correct any misalignment of terms.
Register your MySpecialServlet by v1.special, like selectors = {"v1.special"}.
According to the documentation:
... The selectors must be configured as they would be specified in the URL that is as a list of dot-separated strings such as print.a4 ...
I understand that when registering the servlet by a list of selectors, Sling treats them individually (as within OR condition). So, in the case of registering your special servlet by selectors = {"v1","special"}, the doGet method will be called if you request:
http://localhost/my/resource.v1.special.json or
http://localhost/my/resource.special.json or
http://localhost/my/resource.v1.json

Where to register observers when using IoC containers?

In my current application, I am using a 'selfmade' Observable class in order to implement the observer pattern. Observers (implementing an interface) can listen to certain events.
I am switching more and more of the project over to using an IoC container, but I fail to find a good place in the code to register the observers with the observable.
The ways to do this that I see are:
A) Inject the observable into the observer.
The constructor shouldn't do actual work, but even with method- or field injection this couples the observer with the observable. This is also the case if it's the other way around.
B) Create an observable factory and do it there.
In this case creating the observable depends on the implementations of several observers.
C) Create the observers by factory and do it there.
While this seems best to me concerning coupling, it turns out bad concerning DRY. In some cases the factory ends up being a copy of the fields and constructor of the observer, plus the observable and plus the one line of code for the registering.
Is there a way to handle this registering in a clean way? If not, are there compelling arguments to use a certain way over the others?
The solution was discovering the difference between Resolve() and Instantiate() methods.
I was always using Resolve(). With that, I would receive an Observer in the way it has been bound to the container.
However, Instantiate() does not care about how the class is bound to the container. It creates the class the standard way, but still injects the dependencies. So this can be used inside the factory for the observer.

How to avoid multiple instances of a service in Angular Dart

It seems that each component that creates its own instance of [a] service. I don't understand why.
I note this AngularJs 2 - multiple instance of service created, but I'm not clear on the correct solution. Do I create the service instance _myService in main:
void main() {
bootstrap(AppComponent,[MyService]);
}
and then copy it to [child] components (because I also remove MyService from the component providers)? This doesn't seem correct, because the components reference _myService before it's instantiated, and I have to check it for being null.
Thanks
Steve
You can use factory constructor pattern like here.
Creating your service in the bootstrap will make sure there is only one instance of it for the app (if you don't provide it again in some component).
You get multiple copies of it only if you provide it in some #Component - then each instance of the component (and all its children) will have a separate instance of the service.

Scala/Play framework parameter name with a dot in it

Could be a silly question. I am developing a facebook realtime update notification service. For which Facebook first calls my GET service to verify caller. Facebook demands to have a parameter name with a dot in it. e.g. "hub.mode", and theere are 2 more with a dot in parameter name. I'll have provide such parameters available in my Play service. How do I create a parameter with a "dot" in it?
Secondly, can I have one single method serving both "GET" and "POST" requests?
You can get the parameter you need without having to declare it in your route.
Example:
def myAction() = Action { request => // Don't forget this detail
request.getQueryString("hub.mode") match {
case Some(hubMode) => Ok(hubMode)
case None => BadRequest
}
}
And yes, you can have one single method that serves different HTTP methods.
First: For dotted arguments you probably should make a use of Nested values like described in the documentation
Second: yes, you can

How does Castle Windsor respond to a class which implements multiple interfaces?

For example I have two interfaces: ICustomerService and IOrderService which each has a couple of functions like GetCustomer, GetOrder, etc.
I want one class to implement both interfaces: Server.
How does Castle Windsor respond to this?
Is it possible in the first place?
When I resolve the Server object based on one of the two interfaces, will I get the same object?
What happens when I have a constructor that has both interfaces in its parameters? Will there still be one object constructed.
assuming the LifeStyle is left to its default: Singleton.
There's no hard one-to-one mapping between a CLR type and a Windsor service or component (glossary if needed is here).
So you can have any of the following scenarios:
Many Components with different implementation types expose a single Service
container.Register(
Component.For<IFoo>().ImplementedBy<Foo1>(),
Component.For<IFoo>().ImplementedBy<Foo2>()
);
Many Components with same implementation type expose a single Service
container.Register(
Component.For<IFoo>().ImplementedBy<Foo1>(),
Component.For<IFoo>().ImplementedBy<Foo1>().Named("second")
);
Many Components with same implementation type expose different Services
container.Register(
Component.For<IFoo>().ImplementedBy<Foo1>(),
Component.For<IBar>().ImplementedBy<Foo1>().Named("second")
);
Single Component expose different Services
container.Register(
Component.For<IFoo, Bar>().ImplementedBy<Foo1>()
);
As you can see, yes - it is possible, and whether or not you'll get the same instance (assuming singleton) depends on which option you chose - if both Services will be exposed by the same Component, or not.