Windsor - how to register against two interfaces but separate registration lines - castle-windsor

I currently register a large number of classes using this one-line syntax:
container.Register(Classes.FromThisAssembly().BasedOn<IWidgetViewModel>().WithService.Base().LifestyleSingleton());
One of these classes implements both IWidgetViewModel and a second interface that I would like to register, as I need to be able to inject the latter elsewhere. However adding a second registration line like the following obviously fails:
container.Register(Component.For<ISpecialViewModel>().ImplementedBy<MySpecialViewModel>().LifeStyle.Singleton);
(Windsor complains that MySpecialViewModel, which also implements IWidgetViewModel, is already registered).
How can I solve this? I'm using Castle Windsor 3.3 btw.

Solved. I found that if I include that second line and add a name:
container.Register(
Component.For<ISpecialViewModel>().ImplementedBy<MySpecialViewModel>().LifeStyle.Singleton.Named("xyz");
It presumably makes the registration "unique" to prevent it from failing, and I'm still able to inject ISpecialViewModel where it's needed. (I'm a little surprised at this as I expected I'd always have to resolve it by name).

Related

How to fix the order of methods inside a test case generated by Randoop?

I want to use Randoop to generate unit test cases for an application which consist of many API functions. I can specify which methods to use to form the tests using methodlist option. But what if this method uses the class fields which have to be set using some setter functions first. Right now, these setter functions are being called after the API call in almost all the tests generated by Randoop. Can I specify the order in which the methods are called in my test?
Not currently. I determined this by reading the Randoop manual.
If you want to suggest the feature, you can do so at the issue tracker or on the mailing list.

Custom JSON renderer in AEM/Sling

I've been playing around with this for a while now, and I think, I've - almost - cracked it, but I am still not fully satisfied with my solution.
So, what I want to do, is having a piece of content, a list of items, which would have two views: The standard HTML one, so people can view and edit it; and then a JSON endpoint for other services to consume.
First I thought it's a simple matter of creating two JSP scripts to render the content:
/apps/my-stuff/components/list-page/html.jsp
/apps/my-stuff/components/list-page/json.jsp
However the Apache Sling DefaultServlet seems to be rather ignorant of the json.jsp script.
As a second attempt, I created another script, in /apps/foundation/components/primary/cq/Page/json.jsp which will be actually called, and renders the page, as I expected. However there are a couple of worries/questions regarding this:
First of all, why is this being honoured by the system, and not the one in the more specific place?
The documentation states, that to find the appropriate renderer, first sling:resourceType will be inspected, then sling:resourceSuperType and then, only as a fallback will jcr:PrimaryType checked. However I think this is rather: jcr:PrimaryType, then the DefaultServlet, and then all the other things.
Most worryingly however, I have to admit, this is rather generic, so it'll break all the contnet with jcr:PrmaryType = Page, so that could have some side-effects.
A solution could be creating a new type: ListPage extends Page; and then create a renderer for that in /apps/foundation.... However I have this bad feeling, that might introduce other problems.
So my question is two fold: What is the proper way of doing this, and/or what am I missing from the way the URL -> script resolution is working in AEM/Sling. (Because it seems to be slightly different that described here and here.)
(Obviously I am trying to keep the default JSON renderer for other pages, as that might be needed for other things in the page. I am not even sure, changing this one page won't break the UI for this particular page...)
However the Apache Sling DefaultServlet seems to be rather ignorant of the json.jsp script.
Have you tried renaming your JSP like so: "list-page.json.jsp"?
If you're using AEM 6.3, you should look at Sling model Exporters. They allow you to automatically register a servlet against your Sling Model (that you can create to model your list content). That servlet can generate a JSON representation of the model for you using Jackson.
If you're not using AEM 6.3, I would suggest you create a servlet registered against your resource type and use an additional selector.
#SlingServlet(
selectors = "json",
resourceType = "my-stuff/components/list-page",
methods = "GET")
More information on Sling Servlets can be found here.

How to log in Cakephp vendor Table class?

I try to debug a EmailQueueTable (extends Cake\ORM\Table) located in vendor folder, but I can't find how to get logs.
I tried $this->out() (the method is called by SenderShell extends Cake\Console\Shell)
I tried $this->log
I tried Log::write
The only solution I found is to throw an Exception...
I would like to say that $this->log works well in other classes of the app.
If someone as an idea of the problem ?
Thanks a lot !
$this->log works in classes that have included the LogTrait. This includes Cake's core View, Controller and Component classes. I'm not sure why Table doesn't also include this, but you can get the same functionality with \Cake\Log\Log::write(LogLevel::ERROR, $xxx);

Adobe Flex compiler include classes

I'm trying to create and instance of an object by reference the class object of the class using
getDefinitionByName()
the problem is that if I don't create at least one instance of the class before when try to use getDefinitionByName() it say
ReferenceError: Error #1065: Variable XXXXXX is not defined.
how can I include the class in the binary without using and instance first??, also I had tried to juts leave in the import but still don't include the class, it could be a compiler param I can pass??
I'm using Flex SDK 4.6 thanks!!!!!
As described in the documentation:
-includes class Links one or more classes to the resulting application SWF file, whether or not those classes are required at compile time
There is a bunch of compiler options which allow you to include classes, but they aren't very scalable / require some manual labour. For example, there's -includes option, but you must know what symbols to include. There's -include-libraries, but again, you'd have to compile a SWC library with the classes you need. -include-namespace - you'd need to write the namespace definition, listing all classes that you want to include.
Since I imagine that the task in the end will get automated one way or another, it would make more sense to just generate an AS file of the following format:
package your.app {
import fully.qualified.class.Name;Name; // it is enough to just mention it
. . .
}
And then include only this this class.
Well I think I found the solution, just add to the compiler the argument -includes like thised
-includes com.example.Myclass
that will include the class object in the binary even though u haven't used and after tried to load it with getDefinitionByName()
hopes this help to someone else, also here is a complete list of arguments for the compiler
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7a92.html

Fluently choosing which component to inject

I have some components implementing the same interface and I would like to chose which one gets injected to my Repository.
Component.For<IRepository>().ImplementedBy<Repository>().<whatShouldGoHere>()
I thought I had this working with DependsOn but now I saw that DependsOn are for static dependecies such as strings. Is the IHandlerSelector the only way forward? I would rather have the declaration inline with the component registration. Maybe a factory method? Are there any recommendations?
Edit
Example Constructor
public PersitentRepository(Func<ISession,string> sessionFactory)
Digging around I realize that the delegate is an artifact from the TypedFactoryFacility. There seems to have been some change so it now resolves by type only. In older Castle versions the string argument was used to select component by name.
A factory would to the trick.
You need to add the FactorySupportFacility to your container for this to work.
For much more detail, see the Castle Windsor documentation at http://docs.castleproject.org/Default.aspx?Page=Factory-Support-Facility&NS=Windsor&AspxAutoDetectCookieSupport=1.
See also http://www.mail-archive.com/castle-project-users#googlegroups.com/msg04463.html.
DependsOn does work for other things than statics, the problem is that the injected delegate does not resolve the way it used to. I ended up registering my own component for handling this specific delegate
container.Register(Component.for<Func<ISession,string>>().ImplementedBy(sessionName => container.resolve<ISession>(sessionName));
(I typed the above from memory so please excuse any typos)