Custom names for MvvmCross converters - naming-conventions

Coming from a WPF background I am used to naming all value converters with the postfix word "Converter". I can do the same in MvvmCross however the usage style in the Android Axml is without the converter postfix.
Is it possible to still include the postfix word Converter without manually registering?

By default, MvvmCross registers the value converters using this filler - https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding/Binders/MvxValueConverterRegistryFiller.cs#L20
name = RemoveTail(name, "ValueConverter");
name = RemoveTail(name, "Converter");
If you want to replace this registry filler, then you can:
create a new filler class without the RemoveTail rules
create a new MyCustomBindingBuilder which inherits from MvxAndroidBindingBuilderand override protected virtual IMvxValueConverterRegistryFiller CreateValueConverterRegistryFiller() in https://github.com/MvvmCross/MvvmCross/blob/e6d10972b5c28e00e80acc3d9e8910961aa813d6/Cirrious/Cirrious.MvvmCross.Binding/MvxCoreBindingBuilder.cs#L74 - this can return your custom filler
you would return this custom binding builder in an overridden protected virtual MvxAndroidBindingBuilder CreateBindingBuilder() in your Setup class for your app.

Related

Jackson private constructors, JDK 9+, Lombok

I'm looking for documentation on how Jackson works with private constructors on immutable types. Using Jackson 2.9.6 and the default object mapper provided by spring boot two running with jdk-10.0.1
Given JSON:
{"a":"test"}
and given a class like:
public class ExampleValue {
private final String a;
private ExampleValue() {
this.a = null;
}
public String getA() {
return this.a;
}
}
Deserialisation (surprisingly, at least to me) seems to work.
Whereas this does not:
public class ExampleValue {
private final String a;
private ExampleValue(final String a) {
this.a = a;
}
public String getA() {
return this.a;
}
}
And this does:
public class ExampleValue {
private final String a;
#java.beans.ConstructorProperties({"a"})
private ExampleValue(final String a) {
this.a = a;
}
public String getA() {
return this.a;
}
}
My assumption is that the only way the first example can work is by using reflection to set the value of the final field (which I presume it does by java.lang.reflect.AccessibleObject.setAccessible(true).
Question 1: am I right that this is how Jackson works in this case? I presume this would have the potential to fail under a security manager which does not allow this operation?
My personal preference, therefore, would be the last code example above, since it involves less "magic" and works under a security manager. However, I have been slightly confused by various threads I've found about Lombok and constructor generation which used to generate by default #java.beans.ConstructorProperties(...) but then changed default to no longer do this and now allows one to configure it optionally using lombok.anyConstructor.addConstructorProperties=true
Some people (including in the lombok release notes for v1.16.20) suggest that:
Oracle more or less broke this annotation with the release of JDK9, necessitating this breaking change.
but I'm not precisely clear on what is meant by this, what did Oracle break? For me using JDK 10 with jackson 2.9.6 it seems to work ok.
Question 2: Is any one able to shed any light on how this annotation was broken in JDK 9 and why lombok now considers it undesirable to generate this annotation by default anymore.
Answer 1: This is exactly how it works (also to my surprise). According to the Jackson documentation on Mapper Features, the properties INFER_PROPERTY_MUTATORS, ALLOW_FINAL_FIELDS_AS_MUTATORS, and CAN_OVERRIDE_ACCESS_MODIFIERS all default to true. Therefore, in your first example, Jackson
creates an instance using the private constructor with the help of AccessibleObject#setAccessible (CAN_OVERRIDE_ACCESS_MODIFIERS),
detects a fully-accessable getter method for a (private) field, and considers the field as mutable property (INFER_PROPERTY_MUTATORS),
ignores the final on the field due to ALLOW_FINAL_FIELDS_AS_MUTATORS, and
gains access to that field using AccessibleObject#setAccessible (CAN_OVERRIDE_ACCESS_MODIFIERS).
However, I agree that one should not rely on that, because as you said a security manager could prohibit it, or Jackson's defaults may change. Furthermore, it feels "not right" to me, as I would expect that class to be immutable and the field to be unsettable.
Example 2 does not work because Jackson does not find a usable constructor (because it cannot map the field names to the parameter names of the only existing constructor, as these names are not present at runtime). #java.beans.ConstructorProperties in your third example bypasses this problem, as Jackson explicitly looks for that annotation at runtime.
Answer 2:
My interpretation is that #java.beans.ConstructorProperties is not really broken, but just cannot be assumed to be present any more with Java 9+. This is due to its membership in the java.desktop module (see, e.g., this thread for a discussion on this topic). As modularized Java applications may have a module path without this module, lombok would break such applications if it would generate this annotation by default. (Furthermore, this annotation is not available in general on the Android SDK.)
So if you have a non-modularized application or a modularized application with java.desktop on the module path, it's perfectly fine to let lombok generate the annotation by setting lombok.anyConstructor.addConstructorProperties=true, or to add the annotation manually if you are not using lombok.

Structural Search to match method call with generic parameter

Let's say that I have a class class Foo : Base and I want to perform a certain method call with signature
public void someStuf(Iterable<? extends Base> param)
For the search template I just take as starting point the pre-existing one
$Instance$.$MethodCall$($Parameter$)
Is it possible to match a parameter of any kind Iterable of a specific Base subclass (Foo in this example)??
List<Foo> fooList = new ArrayList<>();
fooList.add(new Foo("A"));
List<Bar> barList = new ArrayList<>();
barList.add(new Bar(1));
someStuff(fooList); // find this!
someStuff(barList); // don't find this one
someStuff(Collections.singletonList(new Foo("B"))); // also match this one
I've tried several combinations without any luck, is it even possible to do?
This was previously not possible without resorting to a hack. See further below for that. Currently, the code can be found using a Type modifier.
search template
$Instance$.$MethodCall$($Parameter$)
variables
$Instance$ Count=[0,1]
$MethodCall Text=someStuff
$Parameter$ Type=Iterable<Foo>, within type hierarchy
hack
The hack previously needed used a Script modifier and a simple Type modifier on the $Parameter$ variable:
$Parameter$
Script=__context__.type.parameters[0].presentableText.contains("Foo")
Type=Iterable
The related bug report is fixed since IntelliJ IDEA 2017.3.

Entity Framework 6 outputting related tables

I just setup a new EF6 project. In my database I have 2 tables:
- languages
- langugesDescriptions
(with relation)
the context lazyLoadingEnabled is set to false.
(both on edmx as in code)
When getting data from languages:
return context.languages
gives me on FIRST RUN, correct ouput, all language records.
But, when running context.languageDescriptions, and then again context.languages, in the output are also descriptions included.
any ideas? caching ?
Language class is auto generated: (under the .tt file)
Partial Public Class Language
Public Property Lang_ID As Integer
Public Property Lang_Name As String
Public Property Lang_Code As String
Public Overridable Property LanguageDescription As ICollection(Of LanguageDescription) = New HashSet(Of LanguageDescription)
End Class
Internally, Entity Framework leverages the Identity Map pattern which
Ensures that each object gets loaded only once by keeping every loaded
object in a map. Looks up objects using the map when referring to
them.
When you're getting Languages from the context, then LangaugesDescriptions, EF knows that LanguageDescriptions is a Navigation-Property of Language so even if your Lazy-Loading is turned off, each loaded Langauge will contain its associated LanguageDescriptions because it's already loaded.
However, some will argue that LanguageDescriptions is actually a Value-Object and should not be exposed directly on your context, it should only be accessible as part of its (root entity) Language.
Update (as per your comment):
If you want to explicitly disable auto-filling of the LanguageDescription property, you can try to clear the local cache using:
context.LanguageDescription.Local.Clear()
You can also get the list of LanguageDescription using the AsNoTracking() method to prevent the entities from tracking each other:
context.LanguageDescription.AsNoTracking();
Or project the existing Language collection into a new collection that doesn't fill the LanguageDescription property:
From Lang In context.Languages
Select New Language With {
.Lang_ID = Lang.Lang_ID,
.Lang_Name = Lang.Lang_Name,
.Lang_Code = Lang.Lang_Code,
}

Swap an AMD Module Dependency in Dojo

Here is a widget class declared in a MyWidget.js module.
define(["dojo/Foo","myapp/Bar"], function(Foo, Bar) { return declare('MyWidget', [], {
postCreate:function() {
var bar = new Bar();
bar.sayHello();
}
})});
In this theoretical example, "myapp/Bar" is a class defined similarly by returning a declare call. Now let's assume that I have created "myapp/SpecialBar" by extending "myapp/Bar".
In another widget, I want to tell MyWidget to use "myapp/SpecialBar" instead of "myapp/Bar" like so:
require(["myapp/MyWidget","myapp/SpecialBar"], function(Foo, SpecialBar) {
//Now swap "myapp/Bar" module dependency of "myapp/MyWidget" to "myapp/SpecialBar"
var myWidget = new MyWidget();
});
I know ways to do this. For example, I could add a Bar attribute to "myapp/MyWidget" and assign the value of the Bar module. This would allow me to instantiate like this: new MyWidget({ Bar:SpecialBar }). However, this seems like too much ceremony. Is there a clean way to just swap an AMD dependency without any special treatment to the module definition?
That is the clean way. You cannot change the modules that a widget/module depends on, well, you could map them, but that's done globally, so then it always maps to a specific module.
If you could do that, you could break a lot of stuff as well, besides, such a feature does not exist in any language. Comparing require() with imports in Java and .NET, you will see a similar trend.
The only way to change the module, is by changing the behavior/state of the module, which means by overriding properties or functions. When a module is "swappable" it's often used as a property, examples where this occur:
The dojo/dnd/Moveable class allows you to set a custom dojo/dnd/Mover through the mover property. In this case the constructor is added as a property to the Moveable (reference guide)
All widgets with a dropdown inherit from dijit/_HasDropDown, which adds the dropdown widget itself as a property to the parent widget

Reusing property editors for Blend 4

I have a custom silverlight control, which exposes a property with DataGridLength type. Now I want that property to have the same editor as a common DataGridColumn's Width property, with the combobox and everything, like this:
instead, I only get a simple TextBox, with "Auto" written in, with no way to set to SizeToCells and so on.
I assume I need a DesignTime attribute, but none of the ones I found in ComponentModel namespace even came close...
I guess you just have to create an Enum with the all the values autorized (Pixels, SizeToCells, etc ...), you that Enum as the type of your property DataGridLength and then, in the code of your control, take the corresponding action regarding the value sent.