Multiple notifications with MediatR and Autofac ContravariantRegistrationSource - autofac

I am trying to use MediatR to set up generic notifications, but the notification handler is being called multiple times.
https://github.com/smartaypants/MediatR/blob/master/test/MediatR.Tests/CustomNotificationTests.cs
The test publishes a CustomNotification which implements ICustomNotification. The CustomNotificationHandler is constrained to accept TNotification which must implement ICustomNotification.
I would expect this to only be called once but the handler is being called 3 times - where TNotification is CustomNotification, CustomNotificationBase and ICustomNotification - but they are all the same instance... should that happen?
If I remove the ContravariantRegistrationSource line from the Autofac registration then it works as expected. Similarly if I use the StructureMap registration it works fine.
I don't entirely understand why I need to enable contravariance. I am only using this line because it is included on the MediatR wiki and almost every example I can find.
Please can someone explain why this is happening and if I am doing something wrong, or is it a bug in Autofac. Thanks.

Related

Autofac - Add registration after a registration has been added

I want to subscribe an global event which is invoked after a registration has been added.
I don't want to manually subscribe to an event for every registered service/component, because that's boilerplate code and it's easy to forget it when adding new registrations.
When the event fires I want do some checks on the already registered registrations and if a condition is met, e.q it's a named registration and it's a reference type then I want to add additional/new registrations, this should happen before the container is built.
What's the right way to achieve this?
Can I do it in a CustomModule that derives from Module?
Br
Autofac doesn't support "registration events" or anything like that - only resolution events. What you might be able to do is:
Use the OnlyIf extensions for conditional registration (here are the unit tests showing examples)
Register your conditional thing last so everything else is registered and OnlyIf will work.
Possibly use the Properties dictionary on the ContainerBuilder to your advantage, such that when the important things get registered they add something to the properties dictionary that you can check for.
I think some combination of those things should get you what you're looking for. But, unfortunately, there's no event. The reason is that registrations aren't just a simple collection of objects the way they are in Microsoft.Extensions.DependencyInjection - registrations are all callback methods that don't actually execute until you call Build. The final set of registrations is only really available then; and when Build is called, you can't modify that list of callbacks post-facto to add or change registrations.
That architecture is not likely to change because it's pretty baked into the core of the builder syntax for registrations. The above options are about it.

How should I test functionality accessible only during the lifetime of an async method using #testing-library/react?

I have a React component which allows users to submit potentially long-running queries to a remote service. While the query is running, the component shows a cancel button. I want to test that this button shows up when expected, that its click handler cancels the previous API request, and so on.
Since the button is only present while the async API call is active, the tests I wrote for this purpose make their assertions about the button in the mock implementation of the async API itself. They're not super elegant but I confirmed that they do go red as I expect when I remove parts of the production code.
On upgrading #testing-library/react from 8.0.1 to 9.3.2, although the tests still pass, I now get the following warning several times:
console.error node_modules/#testing-library/react/dist/act-compat.js:52
Warning: You seem to have overlapping act() calls, this is not supported. Be sure to await previous act() calls before making a new one.
I have reproduced the issue in the following CodeSandbox (be sure to selected the "Tests" tab on the right-hand side and then view the "Console" messages at the bottom right).
The final comment on this GitHub issue says that I shouldn't need to worry about act() as long as I'm using the React Testing Library helpers and functions (which I am). What am I missing?
Raising an issue (here) against react-testing-library has helped me reach the conclusion that the best approach seems to be not to worry about it too much. The library's author was kind enough to propose a simpler test pattern of just making assertions directly after the action that causes the component to enter the transient state that you're trying to test. For example:
fireEvent.click(getByText("Submit")); // <-- action which triggers an async API call
expect(getByText("Fetching answers")).toBeInTheDocument();
Previously, I had the same expect statement in my mock implementation of the API call triggered by the click. My reasoning was that this was the only way I could be certain that the assertions would run while the component was in the transient state I was trying to test.
AFAICT, these tests are not strictly correct with respect to asynchronous actions because the promise returned by the mock implementation could resolve before the expectation was checked. In practice though, I have observed that the tests rewritten to the simpler approach:
do not provoke the warning about overlapping act() calls in the OP
can be made to fail as expected if I change the non-test code to break the behaviour under test
have not so far shown any intermittent failures
are far easier to read and understand
This question has never attracted much attention but I hope recording the answer I eventually arrived at myself might help others out in future.

Realm - RJSRealmDelegate.changes_available assert(0) causing crash

We are building a React-Native iOS app that needs to access our realms natively as well as via RN.
We have writing working correctly from our UI and can load the data in our background Swift service, but after the write, The changes_available method in js_realm.cpp is firing which contains assert(0) which is causing the app to crash/hang in debug. What do we need to be doing to prevent that method from firing?
Update: A release was just made (0.11.1) which should prevent this crash from occurring. Note that notifications across bindings have not yet been tested and may not work.
This method gets called when changes are made externally to a Realm from another process or thread. In your case it sounds like writes made from the swift apis are causing this method to get called. The ReactNative binding was written with the assumption that everything would be done from a single thread without considering the use of other language bindings being used simultaneously.
As is the only thing you can to do prevent this is to not make a write in Swift while a Realm is open in JS. One way to do this would be to call Realm.close after every use, although this may perform poorly.
In the near term we can also do a point release to remove the assert(0) - this will prevent the crash/hang, but notifications for changes made in Swift wont work without additional changes. Can't give an estimate of when we can get cross language notifications working properly.

How to use Trampoline IOS

I was looking for this on google and I found some articles on it They say it is used for HigherOrderMessaging and I tried to read the code to but everything was over my head can any body give simple example of it how we can use them? They were saying its used for passing returned object from method to another object. And another question when I develop apps never came situation where I need to use something like this.
In Objective-C, a trampoline is an object returned by a method that exposes some kind of message interface. When messages are received, it bounces the message on to another object.
Example One:
Return a proxy of a service client. When methods are invoked on the proxy, it first checks if the user has permission to proceed.
Example Two:
Make all the objects in an array do something:
[[windowsArray do] setHidesOnDeactivate:YES];

Zend 2 Event Listeners in classes not loaded

Does the Zend 2 event manager have the ability to fire listeners in classes that are not loaded?
If I understand you correctly, then I believe that you can register listeners using the StaticEventManager (see Event Manager Quick Start).
In this case, you do not need to have an instance of the target class (just the name), but you can register listeners for events (typically methods) on future instances of that target class that may occur.
Of course, in order to be useful, the target class should actually compose an EventManager instance (probably via an events() method, as described on the same Quick Start page) and actually fire the events.
I confess that I am still trying to wrap my own head around the ZF2 EventManager, so if I have totally boned it up here, please feel free to correct me.