DryIoc cannot resolve my boolean instance? - dryioc

I try to register a class which constructor requires a few string, a ILog and a bool. All in my container is registered as a singleton (set as the default Reuse).
But whenever I try to get an instance of that class container.Resolve<AzmanAccess>(), an exception gets thrown.
Unable to resolve Boolean as parameter "accessAll" in
Company.Common.Util.Authentication.AzmanAccess:
Company.Common.Util.Authentication.IAzmanAccess
{RequiredServiceType=Company.Common.Util.Authentication.AzmanAccess}
as parameter "azmanAccess" in
Company.Common.Util.Authentication.AzmanCustomAccess:
Company.Common.Util.Authentication.IAccess as parameter "access" in
Company.Common.Util.Authentication.User:
Company.Common.Util.Authentication.IUser Where CurrentScope: null and
ResolutionScope: null and Found registrations: skipAuthz,{ID=53,
ImplType=Boolean, Reuse=SingletonReuse {Lifespan=1000}}}
System.Object,{ID=25, ImplType=Boolean, Reuse=SingletonReuse
{Lifespan=1000}}}
I register my class like this:
container.RegisterInstance(_accessAll, serviceKey: AccessAll);
container.RegisterInstance(_activeDirectoryDomain, serviceKey: ActiveDirectoryDomain);
container.RegisterInstance(_azmanConnString, serviceKey: AzmanConnString);
container.RegisterInstance(_azmanStore, serviceKey: AzmanStore);
container.Register(Made.Of(() => new AzmanAccess(
Arg.Of<bool>(AccessAll),
Arg.Of<ILog>(),
Arg.Of<string>(ActiveDirectoryDomain),
Arg.Of<string>(AzmanConnString),
Arg.Of<string>(AzmanStore)
)));
and the constructor is:
public AzmanAccess(bool accessAll, ILog logger, string activeDirectoryDomain, string azmanConnString, string azmanStore)
Where servicekeys (AccessAll, ...) are unique (tried with objects, then string).
What's going wrong?

The problem caused by the bug in DryIoc: Arg.Of does not recognize service key of non-primitive type.
Until bug is fixed the workaround is to change object service key to e.g. string, Enum type, or int.
Update:
The fix is available in DryIoc v2.4.3.

Related

Use a method on a StateNotifier Riverpod for changing a bool [duplicate]

In the context of a Flutter 2.0.5 app whose state I'd like to manage with Riverpod, I thought I can declare a StateNotifierProvider like this:
import 'package:flutter_riverpod/flutter_riverpod.dart';
final counterProvider = StateNotifierProvider<CounterStateNotifier>((ref) => CounterStateNotifier());
class CounterStateNotifier extends StateNotifier<int> {
CounterStateNotifier([int count = 0]) : super(count);
void increment() => state++;
}
But Android Studio (and later the Dart compiler as well) complains about the line where I declare the counterProvider variable:
The type 'StateNotifierProvider' is declared with 2 type parameters, but 1 type arguments were given.
Removing the <CounterStateNotifier> type parameter in StateNotifierProvider<CounterStateNotifier> removes the error. However, attempting to read the provider and call its increment method (setting () => context.read(counterProvider).increment() as the onPressed of an ElevatedButton, then pressing the button) gives the following runtime error:
'increment'
method not found
Receiver: 0
Arguments: []
Why is context.read(counterProvider) returning the int state instead of the notifier? And what is the reason behind the type parameter error mentioned in the first part of my question?
I should mention that I'm running my app on the web (with flutter run -d Chrome).
As of Riverpod 0.14.0, State is the default value exposed by StateNotifierProvider.
The syntax for declaring your StateNotifierProvider is now as follows:
final counterProvider = StateNotifierProvider<CounterStateNotifier, int>((ref) => CounterStateNotifier());
Accessing functions now requires adding .notifier (accessing the StateNotifier itself):
context.read(counterProvider.notifier).increment();
And like you've noticed, you now access the state like so:
final count = context.read(counterProvider);
More on the changes here.
You may also use dynamic to accept any type if value for the StateNotifierProvider
final modelProvider =
StateNotifierProvider.autoDispose<ModelClassName, dynamic>(
(ref) => ModelClassName());

Zend framework 3 form issue with binding entities that use return type hinting

As I'm upgrading my application to PHP 7.3, I decided to add return type hinting to my entities methods.
An example would be something like this:
class User {
private $username;
public method getUsername(): string { return $this->username}
}
I use this entity in a form (to create users) where I bind it like so:
$user = new User();
$form->bind($user);
$form->setData($data);
However I get this error which is caused by the hydrator (I'm using the DoctrineObject hydrator)
Return value of Admin\Entity\Auth\User::getUsername() must be of the type string, null returned
#0 ...\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(220): Admin\Entity\Auth\User->getUsername()
#1 ...\vendor\doctrine\doctrine-module\src\DoctrineModule\Stdlib\Hydrator\DoctrineObject.php(116): DoctrineModule\Stdlib\Hydrator\DoctrineObject->extractByValue(Object(Admin\Entity\Auth\User))
#2 ...\vendor\zendframework\zend-form\src\Fieldset.php(650): DoctrineModule\Stdlib\Hydrator\DoctrineObject->extract(Object(Admin\Entity\Auth\User))
...
From the snippet above it's obvious that it's calling the getUsername method on an "empty" object, which makes it return null and throw the exception.
This is not logically correct as the entity is supposed to have a username so marking it as nullable (?string) is not correct.
Is there any better solution to this? Because making every single method return type nullable seems like a hack that defeats the purpose.

Unknown database type enum requested,Doctrine

when i want to generate Entity from data base i have this Error:
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it
how can i resolve this issue.
Thanks in advance
You could try to do something like this in the onBootstrap module of your Module.php, to tell Doctrine to treat your enum like a string
$em = $e->getApplication()->getServiceManager()->get('Doctrine\ORM\EntityManager');
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');
Add the following line to your bootstrap.php
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
If you really want to work with enums and don't convert them to strings, you should implement your custom type (it's really not a big deal).
See enter link description here
But also, you have to extend list of types on your platform.
So, simplest way to do that - override useless method \Doctrine\DBAL\Types\Type::getMappedDatabaseTypes with your own so like that:
class EnumType extends Type
{
const NAME = "enum";
// ... (your implemented methods)
public function getMappedDatabaseTypes(AbstractPlatform $platform)
{
return ['enum'];
}
}
Have a fun :)

How to pass the Arraylist of custom Object type from activity to Service using the Parceler library?

I have done this in activity.Here responses variable is the ArrayList of type custom object LeaseDetailResponse.
Intent intent = new Intent(TestActivity.this, AlarmService.class);
intent.putParcelableArrayListExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, (ArrayList<? extends Parcelable>) Parcels.wrap(responses));
startService(intent);
At AlarmService
Parcels.unwrap((Parcelable) intent.getParcelableArrayListExtra(LEASE_DETAIL_RESPONSE_LIST));
Shows the error
java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList
Caused by: java.lang.ClassCastException: org.parceler.NonParcelRepository$ListParcelable cannot be cast to java.util.ArrayList
The issue is with your cast to an ArrayList... Parceler only deals with Parcelable. You need to use putExtra() instead with no cast:
Intent intent = new Intent(TestActivity.this, AlarmService.class);
intent.putExtra(AlarmService.LEASE_DETAIL_RESPONSE_LIST, Parcels.wrap(responses));
startService(intent);
And to de-serialize in your AlarmService:
Parcels.unwrap(intent.getParcelableExtra(LEASE_DETAIL_RESPONSE_LIST));

How do I test rendered views when using T4MVC with TestHelper?

How do I test which view was rendered from a controller action if what I get is a T4MVC_ActionResult? Under normal circumstances I should be able to directly use TestHelper's methods, like in the examples:
pooController.Details().AssertViewRendered().ForView("Details")
...but, since through T4MVC I get a T4MVC_ActionResult instead of a ViewResult, the part AssertViewRendered<>().ForView("Details") fails. What alternative do I have if I want to test which view was invoked?
UPDATE:
Here's the test code:
[TestMethod]
public void Theme_Controller_Details_Action_Returns_Details_View()
{
var builder = new TestControllerBuilder();
var mockThemeRepository = new Mock<IThemeRepository>();
var themeController = builder.CreateController<Evalgrid.Website.Controllers.ThemeController>(mockThemeRepository.Object);
builder.InitializeController(themeController);
var result = themeController.Details();
result.AssertViewRendered().ForView("Details");
}
I used the debugger setting a breakpoint after the result line, and its variable type is T4MVC_ActionResult, while themeController is Evalgrid.Website.controllers.ThemeController. Note that I have used the fully qualified name of the controller.
I get this:
Expected result to be of type
ViewResult. It is actually of type
T4MVC_ActionResult.
I don't know what's going on.
Actually, T4MVC should not make a difference here. If you directly instantiate your controller and call an action method, you'll get the same thing back whether you use T4MVC or not. i.e. you won't get a T4MVC_ActionResult.
It's only when you write MVC.Foo.Details() that you'll get a T4MVC_ActionResult. That's because MVC.Foo returns an instance of a derived class which does special thing, and not directly your controller class.
Does that make sense?
Update: I'm confused, as looking at the sources for TestControllerBuilder.CreateController, it has:
public T CreateController<T>(params object[] constructorArgs) where T : Controller
{
var controller = (Controller)Activator.CreateInstance(typeof(T), constructorArgs);
InitializeController(controller);
return controller as T;
}
So it's directly instantiating the type that you pass in, which should just call your normal action.
One question about your code: does your Details action method take any parameters? If so, that would explain the problem, as you're calling it with no params, which would be a T4MVC method added in the partial class.