Skip to content

refactor autocomplete to be flexible and reusable#54

Open
MomchilSt wants to merge 2 commits intoMinkov:mainfrom
MomchilSt:autocomplete-refactoring
Open

refactor autocomplete to be flexible and reusable#54
MomchilSt wants to merge 2 commits intoMinkov:mainfrom
MomchilSt:autocomplete-refactoring

Conversation

@MomchilSt
Copy link

Closes #41

Summary of changes:

  1. Removed hardcoded key "Id" and now keys are collected by GetPrimaryKeyPropertyInfos() so we can have multiple keys.
  2. Refactor comparing logic to work with other then strings.
  3. Add configurations for search property and number of items shown (default 20) in FormControlViewModel.

Comment on lines 243 to 249
var parameter = Expression.Parameter(typeof(TEntity), "e");
var propertyAccess = Expression.Property(parameter, searchedProperty);
var searchTermExpression = Expression.Constant(searchTerm, typeof(string));

Expression body;

if (searchedProperty.PropertyType == typeof(string))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract all the Expression building in ExpressionsBuilder.

{
// If the property type is string, use Contains method
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
if (containsMethod != null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange. Is it possible to have a string without Contains?

if (searchedProperty.PropertyType == typeof(string))
{
// If the property type is string, use Contains method
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Move to TypeExtensions
  2. Use nameof

Comment on lines 260 to 263
else
{
// If the property type is not string, convert it to string and then use Contains method
var toStringMethod = typeof(object).GetMethod("ToString");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be joined with the if above?

Comment on lines 411 to 417
{
var shownFormControls = this.ShownFormControlNames.Concat(this.ShownFormControlNamesOnCreate);
var hiddenFormControls = this.HiddenFormControlNames.Concat(this.HiddenFormControlNamesOnCreate);
formControls = SetFormControlsVisibility(formControls, shownFormControls, hiddenFormControls)
.ToList();
break;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a static analysis for those. Follow it.

.ToList()
.ForEach(property => property.SetValue(existingEntity, property.GetValue(newEntity, null), null));

private IEnumerable<DropDownViewModel> FilterAutocompleteResults(int maxResults, PropertyInfo searchedProperty, IEnumerable<PropertyInfo> keys, Expression<Func<TEntity, bool>> lambda)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename lambda to expression as it is more accurate.

Comment on lines 888 to 889
Value = keys!.Count() == 1
? keys.First().GetValue(x) !.ToString() !
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is an explicit check for a single value needed?

@MomchilSt MomchilSt requested a review from Minkov November 27, 2023 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoComplete must be refactored

2 participants