How to indexing and searching nested properties of Interface type in NEST - elasticsearch

I have a following document index entity:
[ElasticType(Name = "Document", IdProperty = "Id")]
public class Document
{
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
public string Id { get; set; }
[ElasticProperty(Type = FieldType.Nested)]
public ICustomer Customer { get; set; }
}
where ICustomer can be different types:
public interface ICustomer
{
}
public class Supplier : ICustomer
{
public string Name { get; set; }
//another properties
}
public class Vendor : ICustomer
{
public string Name { get; set; }
//another properties
}
My mapping is:
Client.CreateIndex("Document", c => c
.AddMapping<Document>(m => m
.SearchAnalyzer("standard")
.IndexAnalyzer("standard")
.MapFromAttributes()
.NumericDetection()
.DateDetection();
When I save document to index it saves also nested objects (Supplier or Vendor) serialized correctly.
But I have problem when I'm searching data. I'm getting following exception from newtonsoft:
Type is an interface or abstract class and cannot be instantiated.
I was trying to create custom json converter
public class CustomJsonConvertor : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (objectType == typeof(Supplier))
{
return serializer.Deserialize(reader, typeof (Supplier));
}
if (objectType == typeof(Vendor))
{
return serializer.Deserialize(reader, typeof(Vendor));
}
throw new NotSupportedException(string.Format("Type {0} unexpected.", objectType));
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof (Supplier) || objectType == typeof (Vendor);
}
}
and register it as:
settings.AddContractJsonConverters(t => typeof(ICustomer).IsAssignableFrom(t) ? new CustomJsonConvertor() : null);
But then I'm receiving exception in ReadJson method, because objectType is of type ICustomer and the conditions if (objectType == typeof(Supplier)) are never true. Parameter existingValue is null in this method, so I have no option how to determine
the correct type.
NOTE: my entities (Vendor, Supplier) are in separated dll (plugin) and I have no direct access to the them while defining Document.
Can you advise what I'm doing wrong or give me some best practice advice how to deal with interface or abstract class inside document index and how to deal with polymorphism?
thanks a lot!

That information is lost, you'll need to inspect the JSON to provide a differentiator.
For hits themselves NEST is able to use _type as a differentiator. For collections inside your document you will have to either write your jsonconverter to instantiate the right type based on a property or instruct Json.NET to write that information for you when serializing automatically.

Related

HotChocolate GraphQL query to page/filter/sort on nested array fields

HotChocolate Version=12.3.2.0
I want to be able to page/filter/sort on nested fields. For example, where user id = 1234, get the user's 1st document set, then the 1st docFile in the document set, ordered by docFile createdDate.
public class User
{
public int Id {get;set}
[UsePaging]
[UseFiltering]
[UseSorting]
public List<Document> Documents { get; set; }
}
public class Document
{
[UsePaging]
[UseFiltering]
[UseSorting]
public List<DocFile> DocFiles { get; set; }
public User User {get;set;}
}
public class DocFile
{
public string Name {get;set}
public DateTime CreatedDate {get;set;}
public Document Document {get;set;}
}
[UseAppDbContext]
[UsePaging]
[UseProjection]
[UseFiltering]
[UseSorting]
public async Task<Connection<User>> GetUsersAsync(
IResolverContext context,
[ScopedService] DbContext dbContext,
CancellationToken cancellationToken
)
{
var dbResult = dbContext.Users.Filter(context).Sort(context).Project(context).ToArray();
var result = await dbResult.ApplyCursorPaginationAsync(context, cancellationToken);
return result;
}
GraphQL Query
users(
where: {id: {eq: 1234}}
) {
nodes {
documents(first:1){
id
files(first:1 order:{createdDate: DESC}) {
nodes {
name
createdDate
}
}
}
}
}
But when I execute the GraphQL query I currently get the following error:
"exceptionType": "InvalidOperationException",
"message": "No generic method 'OrderByDescending' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. "
Any idea on how to do this?
If the [UseSorting] annotation comes from HotChocolate.Types it's the old way of filtering (uses a different syntax). Then it should be order_by in your query.
Try [HotChocolate.Data.UseSorting] to match your query.

How to access property of parent in validation attribute

In my code below I want to check with AttributeValidation if a field is given dependent on a property of its parent element. The comment in the class
RequiredIfParentState1
describes my question best.
public class ChildModel()
{
[RequiredIfParentState1]
public string ImRequired { get; set; }
}
public class ParentViewModel()
{
public int state { get; set; }
public ChildModel child = new ChildModel();
}
public class RequiredIfParentState1: ValidationAttribute, IClientModelValidator
{
RequiredIfParentState1()
{
}
void AddValidation(ClientModelValidationContext context)
{
}
protected override ValidationResult IsValid(object i_value, ValidationContext i_context)
{
var element = i_context.ObjectInstance;
if(i_value == null && //what do i have to put here to check if the state is 1?)
{
return new ValidationResult($"Field is Required in state 1.");
}
return ValidationResult.Success;
}
}
I feel this is the wrong approach.
An object being in a valid state is one thing (required fields and type checking), but handling business logic is a separate concern.
You could write a validation service, that examines the model in detail, checking business logic concerns, and build up a list of errors.
Where errors are found you can return these in your response.

.net core custom model binding

I have a model such as
public class MyModel
{
public MyObject myObject {get;set;}
}
public class MyObject
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
With out using a custom model binder everything works great. I am trying to implement a model binder and not getting anywhere -- the resources that I have come from are
https://www.youtube.com/watch?v=qDRORgoZxZU (returns null model to the controller)
http://intellitect.com/custom-model-binding-in-asp-net-core-1-0/ (controller dies on the constructor)
http://hotzblog.com/asp-net-vnext-defaultmodelbinder-and-automatic-viewmodel-string-trim/ (can not even find MutableObjectModelBinder in the .net-core namespace)
Ideally what I want is to track which properties where set by the ModelBinder.
public class MyObject
{
public string FirstName {get;set;}
public string LastName {get;set;}
public List<String> ModifiedProperties {get;set;}
}
when the object is created by the ModelBinder for each property that is being set it adds it to the ModifiedProperties list.
This is solution. You need to implement IModelBinderProvider and IModelBinder
public class EntityFrameworkModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
//We only want to invoke the CustomeBinder on IBaseEntity classes
if (context.Metadata.ContainerType != null && context.Metadata.ContainerType.GetInterfaces().Contains(typeof(SurgeOne.Core.IBaseEntity)))
{
//We only create the custom binder on value types. E.g. string, guid, etc
if (context.Metadata.ModelType.GetTypeInfo().IsValueType ||
context.Metadata.ModelType == typeof(System.String))
{
return new EntityFrameworkModelBinder();
}
}
return null;
}
}
And IModelBinder
public class EntityFrameworkModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
//Get the value
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult == ValueProviderResult.None)
{
// no entry
return Task.CompletedTask;
}
//Set the value -- not sure what this does
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
//Set the value -- this has to match the property type.
System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(bindingContext.ModelType);
object propValue = typeConverter.ConvertFromString(valueProviderResult.FirstValue);
bindingContext.Result = ModelBindingResult.Success(propValue);
//Code to track changes.
return Task.CompletedTask;
} //BindModelAsync
}

Retrieve model name in custom display name attribute

Here is my development requirement,
My label values are stored in the database, and I still want to use the data annotation in a declarative way, this is to make my model more readable.
And here is my approach,
I decided to write custom DisplayNameAttribute, where the default value provided by my model will be overwritten by the value retrieved from the database.
Here is the property defined in the model,
[CustomDisplay(Name: "First Name")]
[CustomRequired(ErrorMessage: "{0} is required")]
public String FirstName { get; set; }
Here is the custom display name attribute class,
public class CustomDisplayAttribute : DisplayNameAttribute
{
private string _defaultName;
private string _displayName;
public CustomDisplayAttribute(string Name)
{
_defaultName = Name;
}
public override string DisplayName
{
get
{
if (String.IsNullOrEmpty(_displayName))
{
_displayName = DAO.RetrieveValue(**ModelName**, _defaultName);
}
return _displayName;
}
}
}
Now, you can see in the above code, ModelName is something I need, but I don't have!!
While debugging, I dig into ModelMetadataProviders.Current and can see the availability of the current model in action. But, as it is part of non-public static members I am unable to access it through my code.
I have written the below method to retrieve the model name through reflection,
private static string GetModelName()
{
var modelName = String.Empty;
FieldInfo info = typeof(CachedAssociatedMetadataProvider<CachedDataAnnotationsModelMetadata>)
.GetField("_typeIds", BindingFlags.NonPublic | BindingFlags.Static);
var types = (ConcurrentDictionary<Type, string>)info.GetValue(null);
modelName = types.FirstOrDefault().Key.Name;
return modelName;
}
But the problem is, the types collection provides me entries for all the models (visited at least once by the user). And there is no clue to know, which is currently in action!!
IMHO Attributes should not be used to make database calls. Attributes should be used to add metadata to Classes/Properties etc...
So If you're willing to change your code to be more like the Microsoft architecture for MVC then you'd have your custom Attribute and a custom ModelMetadataProvider:
public class CustomDisplayAttribute : Attribute
{
public CustomDisplayAttribute(string name)
{
Name = name;
}
public string Name { get; private set; }
}
Then a new ModelMetadataProvider:
public class DatabaseModelMetadataProvider : DataAnnotationsModelMetadataProvider
{
public DatabaseModelMetadataProvider()
{
}
protected override ModelMetadata CreateMetadata(
IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor,
Type modelType,
string propertyName)
{
var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
var displayAttribute = containerType == null
? null as CustomDisplayAttribute
: containerType.GetProperty(propertyName)
.GetCustomAttributes(false)
.OfType<CustomDisplayAttribute>()
.FirstOrDefault();
if (displayAttribute != null)
{
var displayValue = DAO.RetrieveValue(containerType.ToString(), displayAttribute.Name)
metadata.DisplayName = displayValue;
}
return metadata;
}
}
Where
public class MyViewModel
{
public MyPropertyType PropertyName { get; set; }
}
containerType = MyViewModel
modelType = MyPropertyType
propertyName = PropertyName
Then register the provider (global.asax or whatever):
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider();
Also you can take a look at the ModelMetadata it has a few other things you might want to change in the future.

Deleting from a generic list

I have this problem with lists and I can't seem to fix it
I have this class that implement a interface that has the following method.
public List<T> CalculateWad<T, TH>(
List<T> outputList,
List<TH> inputList,
bool flag)
{
...
}
Now, I have a outputlist and a inputlist with a common field Contract. I need to delete in outputlist all contracts that exist in inputlist.
It has to be as generic as possible. I can't seem to be able to get the fields of the lists.
Any ideas?
In order to access the Contract property, the generics T and TH must implement an interface with the Contract property.
Documentation : where (generic type constraint) (C# Reference)
interface IContractable { string Contract { get; } }
Then your class containing the CalculateWad method must be define as follow :
class MyClass<T, TH>
where T : IContractable
where TH : IContractable
{
public List<T> CalculateWad(List<T> outputList, List<TH> inputList, bool flag)
{
return outputList
.Where(o =>
inputList.Select(i => i.Contract).Contains(o.Contract) == false)
.ToList();
}
}
This should to the job, by adding a common IHasContract interface that both T and TH must implement:
class Program
{
static void Main(string[] args)
{
}
private IList<T> CalculateWad<T, TH>(IList<T> output,
IList<TH> input, bool flag)
where T : IHasContract
where TH : IHasContract
{
var contracts = new HashSet<string >(input.Select(i => i.Contract));
var qry = from o in output
where !contracts.Contains(o.Contract)
select o;
return qry.ToList();
}
private sealed class Contract
{
}
private interface IHasContract
{
string Contract { get; }
}
private sealed class Foo : IHasContract
{
public string Contract { get; set; }
}
private sealed class Bar : IHasContract
{
public string Contract { get; set; }
}
}
Note that is does not modify output, which you mention in the text. It does, however, return a new altered copy of the list, which may be rather what the method signature describes.
So this is your interface:
public List CalculateWad( List outputList, List inputList, bool flag) {
...
}
And you need to do this? Assumes that the objects in each list can be compared by their equals method.
public List<T> CalculateWad<T, TH>( List<T> outputList, List<TH> inputList, bool flag) {
// cast list to be regular object lists
List out = (List) outputList;
List in = (List) inputList;
for(Object object : in){
out.remove(object); // this looks for an object that matches using contract.equals(object)
}
}
What is flag variable for?

Resources