Class property attribute for intellisense? - visual-studio

Let's say I have this class...
Public Class Person
Public Property Gender As String = "male"
End Class
And I do this...
Dim p As New Person
p.Gender
... and hover the mouse over "Gender", then intellisence shows me ....
Property.Person.Gender As String
Can I somehow add an attribute to the Gender property to show more intellisence help - like...
Property.Person.Gender As String = "male"
Or maybe...
Property.Person.Gender As String
It is default set to 'Male'
I was hoping to do smething like...
Public Class Person
<Intellisence="It is default set to 'male'">
Public Property Gender As String = "male"
End Class
Thanks

You can use the XML comment to achieve that.
All tags are listed in Microsoft website: C# and VB.NET
More example can be found here: https://msdn.microsoft.com/en-us/library/z04awywx.aspx
Back to your question, you can set the summary tag in your property, e.g.:
Public Class Person
/// <summary>
/// It is default set to 'Male'</summary>
Public Property Gender As String = "male"
End Class

Related

Xamarin Forms, Grouping Realm

I am using Xamarin forms (.NET Standard project), Realm & MVVM Light and I need to group a list of objects based on the Initial of the last name so that I can display a jumplist within a listview.
I am having a problem when trying to group a RealmObject. I have a model like so...
public class Participant : RealmObject
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
public string RegistrationCode {get; set;}
//More properties skipped out for brevity
}
Based on this link, I also have a Grouping class like so...
public class Grouping<K, T> : ObservableCollection<T>
{
public K Key { get; private set; }
public Grouping(K key, IEnumerable<T> items)
{
Key = key;
foreach (var item in items)
this.Items.Add(item);
}
}
In my viewmodel, I am able to fetch the Participants (i.e IQueryable<Participant>) like so....
var participants = RealmInstance.All<Participant>();
I would now like to be able to group this by Initials of the last name for which I do the following:
var groupedParticipants = from participant in participants
group participant by participant.LastName.Substring(0, 1) into pGroup
orderby pGroup.Key
select new Grouping<string, Participant>(pGroup.Key, pGroup);
which throws the below exception:
System.TypeInitializationException: The type initializer for 'Realms.RealmCollectionBase' threw an exception. ---> System.ArgumentException: The property type IGrouping cannot be expressed as a Realm schema type
I have looked around but unable to find working examples of grouping Realm sets. Any help would be greatly appreciated.
Realm does not support Linq's GroupBy (or Select-based projections).
A workaround would be to take a Realm-based sorted query to a standard List and then perform your Linq GroupBy.
Example (using James Montemagno's Monkey project):
var realmSort = r.All<Monkey>().OrderBy(m => m.Name).ToList();
var sorted = from monkey in realmSort
orderby monkey.Name
group monkey by monkey.NameSort into monkeyGroup
select new Grouping<string, Monkey>(monkeyGroup.Key, monkeyGroup);

Have one Rest repository json with everything and one with fields excluded

I have two entities: Book and Category and a repository for both. In the controller, I have set up the methods correctly as such:
#RequestMapping(value="/books", method = RequestMethod.GET)
#CrossOrigin
public #ResponseBody List<Book> bookListRest() {
return (List<Book>) bookRepository.findAll();
}
This obviously shows all books and every field in the entity that isn't #JsonIgnore'd. The problem is, I need to have:
One page with Book data (book name, author name, isbn..) without category
One page with Category data (Category name) without books
One page with Everything (book data along with categories where they belong in)
How can one accomplish this?
I somehow need to in a way ignore #jsonignore on some occasions. Should I make a new entity that extends say, Question and also make a repository for that? Surely that can't be the correct way to do this.
As khalid Ahmed Said you can use costum dtos or you can add Filters to ignore specific fields in Jackson. First, we need to define the filter on the java object:
#JsonFilter("myFilterBook")
public class Book{
...
}
#JsonFilter("myFilterCategory")
public class Category{
...
}
Before you return your ResponseBody you try to use ObjectMapper (Jackson):
The case of one page with Book data (book name, author name, isbn..) without category:
ObjectMapper mapper = new ObjectMapper();
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter
.serializeAllExcept("category");
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilterBook", theFilter);
String dtoAsString = mapper.writer(filters).writeValueAsString(book);
You can do the same think by putting what you want o ignore for the other example.
And for more details to ignore field during marshalling with jackson you can check here
What about using DTOs data transfer objects
you can create multiple DTOs to use them in the response of your API
DTO is a pojo class that customize the returning data from your entity
public class BookWithoutCategoryDTO {
private String name;
private String authorName;
.....
/// and make setters and getters for them
}
public class BookWithCategoryDTO {
private String name;
private String authorName;
private String category;
.....
/// and make setters and getters for them
}
and create your custom mapper to convert from Book to BookDTO

Spring Jpa Query by Example collection

Let's say I have an entity
public class Person {
private String id;
private String firstname;
private String lastname;
private Set<Car> ownedCars;
}
Is there a way I can use query by example to find any person named James having both a Ferrari and Lamborghini?
If I use:
Person p = new Person();
p.setName("James");
p.getOwnedCars.addCar(new Car("Lamborgnihi"));
p.getOwnedCars.addCar(new Car("Ferrari"));
Example<Person> exampleOfPerson = Example.of(p);
List<Person> foundPersons = personRepository.finaAll(exampleOfPerson);
it seems it queries only on person's attributes and ignores any child collections.
You can use a query method for that. Let's say your Car has a property name that can be "Lamborghini" or "Ferrari"
interface PersonRepository extends JpaRepository<Person, String> {
List<Person> findByOwnedCarsNameIn(Collection<String> names);
}
Then you use it like this:
personRepository.findByOwnedCarsNameIn(Arrays.asList("Ferrari","Lamborghini"));
Some gotchas:
The method parameter can take any subclass of Collection, or an array.
The property names on Person and Car must match the method signature and the parameter name as shown above for spring to know how to generate the query, i.e. Person must have a property called "cars", and Car must have a property called "name".
I used JpaRepository, but this works with any of the Repository interfaces provided with spring data JPA

How to properly apply PrePersist like logic using jpa/spring-boot

I have a very simple use case for the following model
#Entity
#Table(name='Foo')
class Foo {
#Id
String id = UUID.randomUUID()
String bar
Date foo_updated
}
I'd like to set the foo_updated value to new Date() when I see the incoming json payload has a value for "bar" (ie- this is a new value included in the POST/ a part of the PATCH update / included and proven to be different in a PUT)
I was hoping to simply apply the #PrePersist annotation on this model and add a simple conditional asking if "bar" was valid ...but quickly realized I wouldn't know if the value was "different" from what was in the db already (for the PATCH/PUT scenario).
I'm starting down the road of "add my own RestController" and apply this logic on the way in using the spring 4 ResponseEntity approach but ... I feel this might end up being a lot more work/more code to maintain.
As I'm new to spring-boot/spring-mvc/jpa I'm curious what other options I have and what the preferred approach would be for this seemingly "simple" requirement
Thanks for the help!
I came across this approach recently which involves recording the previous state on load. You now have access to the previous state after the new values are bound.
#Entity
#Table(name='Foo')
class Foo {
#Id
String id = UUID.randomUUID()
String bar
Date foo_updated
#Transient
private Foo previousState;
#PostLoad
private void setPreviousState(){
previousState = new Foo();
//copy the fields
}
}
However in your case can't you just do:
#Entity
#Table(name='Foo')
class Foo {
#Id
private String id = UUID.randomUUID()
private String bar
private Date lastUpdated;
public void setBar(String bar){
if(! this.bar.equals(bar){
lastUpdated = new Date();
}
}
}

Class diagrams to show relation between classes?

Are class diagrams in visual studio supposed to show relation between classes, for eg. 1 : many.
public class User
{
public string UserId { get; set; }
public List<Role> Roles { get; set; }
}
public class Role {
public int RoleId {get; set;}
public User User {get; set;}
public string UserId {get; set; }
}
Edit:
To clarify.. Class diagram generally isn't supposed to show relations (1:n, m:n etc.) because those relations are for database tables (entities). Classic class diagram is more suited for analysis and design, so it shows associations instead of relations.
Original answer:
Yes you can show 'relations', but you wont see any numbers afaik. It has its own way of showing. Numbers are shown in Entity Framework model for example.
How to show associations:
Move classes into diagram, and then right click on property Roles and pick Show as collection association.
If you want to show more advanced associations here is an addin for Visual studio from CodePlex.

Resources