Can I specify the auto-generated property name when using the [ObservableProperty] attribute with CommunityToolkit.MVVM? - community-toolkit-mvvm

Is it possible to specify the name of the auto-generated property for a private field that is decorated with the [ObservableProperty] attribute? For example, if I have a field, string address, that has this attribute, the toolkit will generate a string property, Address. Is there a way to force the toolkit to produce a property name other than Address while keeping the field name as address?

Sergio Pedri responded to my question on Twitter. Short answer: No, not until we get partial properties.

Related

creating Spring JPARepository method for entity variable with name "cId"

I have entity class as
class Policy
{
private String customerId;
private String sicNumber;
private String cId;
/*
more variables can be ignored
*/
}
now inside JPA Repository interface, I want to create method that finds based on sicNumber And cId
following is the attempt i tried
Policy findBySicNumberAndCId(String policyNumber, String cId) ;
above method asks spring to search for "CId" property in entity class
likewise i tried almost all combinations
Policy findBySicNumberAndcId(String policyNumber, String cId) ;
above method caused spring to look for "AndcId".
now the question is what should be the method name that will target right property in entity class.
thanks in advance
The issue is with the way Spring Data JPA forms the query out of the method name. It follows the regular camel casing approach where the field names are inferred. As per the convention followed, if a method name has uppercase letters consecutively, the field name is inferred in uppercase.
So in your case, findBySicNumberAndCId - the field inferred is CId & is failing to find it.
One way to overcome the problem is having the method name with CId in the beginning with 'c' being in lowercase i.e., findBycIdAndSicNumber .
Alternatively, you can consider renaming the variable to avoid these issues or make use of #Query & define your queries if renaming the variables is not possible.
More info about the issue :
https://github.com/spring-projects/spring-data-commons/issues/1996

HTL Access Property Without Getter

I'm writing an AEM component and I have an object being returned that is a type from an SDK. This type has public properties and no getters. For simplicity, it might be defined like this:
class MyItem {
public String prop1;
public String prop2;
}
Now normally, I would need a getter, like so:
class MyItem {
public String prop1;
public String prop2;
public String getProp1() {
return prop1;
}
}
But I do not have this luxury. Right now, I've got a Java implementation that uses another type to resolve this, but I think it's sort of crazy that HTL doesn't allow me to just access prop1 directly (it calls the getter). I've reviewed the documentation and can't see any indication of how this could be done. I'd like to be able to write:
${item.prop1}
And have it access the public property instead of calling getProp1().
Is this possible?
You don't need getters for public fields if those fields were declared by your Java Use-class. There's actually a test in Apache Sling that covers this scenario:
https://github.com/apache/sling/blob/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/repopojo.html
This also applies to Use-classes exported from bundles.
For Sling Models using the adapter pattern [0] I've created https://issues.apache.org/jira/browse/SLING-7075.
[0] - https://sling.apache.org/documentation/bundles/models.html#specifying-an-alternate-adapter-class-since-110
From the official documentation
Once the use-class has initialized, the HTL file is run. During this stage HTL will typically pull in the state of various member variables of the use-class and render them for presentation.
To provide access to these values from within the HTL file you must define custom getter methods in the use-class according to the following naming convention:
A method of the form getXyz will expose within the HTL file an object property called xyz.
For example, in the following example, the methods getTitle and getDescription result in the object properties title and description becoming accessible within the context of the HTL file:
The HTL parser does enumerate all the public properties just like any java enumeration of public fuields which include getters and public memebers.
Although it is questionable on whether you should have public variable but thats not part of this discussion. In essence ot should work as pointed by others.

How does Jackrabbit generate jcr:uuid (in AEM)?

I am trying to create an auto-generated GUID property on all cq:PageContent nodes. This will be similar to the jcr:uuid property, but will be persisted with content promotion/replication/package installs (whereas the jcr:uuid for a content item changes between different environments).
I am trying to determine how AEM/JCR generates the jcr:uuid property on node creation. The CND defining the property is:
[mix:referenceable]
mixin
- jcr:uuid (string) mandatory autocreated protected initialize
I've tried defining my GUID property in a similar manor, specifying the autocreated and initialize attributes, but this did not result in auto-generation of the property.
Could anybody point me to the source of the jcr:uuid's generation?
As an aside, I asked a related question on the Adobe Community Forum: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.5_ciot.html/forum__bnxr-i_am_tryingtocreat.html
You don't mention which version of AEM (so whether you're dealing with Jackrabbit or Oak), but the mechanism turns out to be basically the same.
When assigning a default value, there are a few hard-coded system property names that get special treatment (jcr:uuid being one of them). If the name of the property being assigned a default value doesn't match any of the special cases, it falls back the static list of default values from the property definition (e.g. listed in the CND file).
In summary, it looks like you cannot piggy-back on this mechanism to assign your own dynamic default value for an arbitrary property. You would need to implement your own event listener or something.
Jackrabbit: See the implementation of setDefaultValues and computeSystemGeneratedPropertyValues
Oak: See the implementation of TreeUtil autoCreateProperty

Is it possible to specify a property naming strategy with an annotation?

I have a class defined as:
class Person {
public int age;
public String firstName;
}
Note that I use camel case for the field names. Also, I know that I could have generated getters and setters but I tend to not do that for simple domain objects.
When I deserialize a JSON or XML response in my REST API, it should spit out:
<Person><Age>11</Age><FirstName>Johnson</FirstName></Person>
You will notice that the first letter is upper-cased.
I could use, for example, #JsonPoperty("FirstName") on my POJO to get the output the way I need it, but this doesn't scale when there are too many fields. I'd like to use a custom property naming strategy (as described in How To Use Property Naming Strategy In Jackson). But instead of configuring an ObjectMapper, I was wondering if its possible to specify a naming strategy using annotations?
Thanks

MVC2 controller method that can accept unknown number of variables?

I need to work with the below posted data, or a variant thereof. Basically, I need to post a variable number of key-value pairs that represent a question id and answer string.
How do I write an ASP.NET MVC2 controller method signature to accept an unknown number of key-value pairs?
attachmentId=8809&question_712=&question_713=&question_714=&question_715=&question_716=&question_717=&question_719=&question_720=&question_721=&question_722=&question_723=&question_724=&question_725=&question_726=&question_727=&question_731=&question_738=&question_739=&question_741=&question_742=&question_743=&question_744=&question_745=&question_746=&question_747=&question_748=
Please note that in this example, there are 26 question keys with empty values. There may be more or less keys and they may or may not have a value. I can reformulate the way the data is sent by the client, so if the best solution is to rethink the way it is sent, I'm open to that.
This is basically the data a FormCollection collects. It's used in the automatically generated controllers by default. i.e. public ActionResult Edit(int id, FormCollection collection)
Use an array. The default modelbinder can detect arrays.
model.questions[0].key model.questions[1].value and so on for the html tag names then build an object that follows those conventions.
public class QuestionUpdateModel{
public int attachmentID{get;set;}
public QuestionPair[] Questions{get;set;}
}
public class QuestionPair{
public int key{get;set;}
public string value{get;set;}
}
After that your controller should accept an argument of QuestionUpdateModel type. The modelbinder should take care of the rest. Make sure you index them sequentially so it can create the array without null entries.