Kullanıcıların kalıcı olarak saklanması için NEST 2.X ve Elastic Search kullanmaya çalışıyorum. Her Kullanıcı, platformumuzdaki izinlerini tanımlayan bir Rol listesi içerir. Ben ElasticSearch verileri depolamak herhangi bir sorun yokNEST ve ElasticSearch ile Arabirim Türetilmiş Sınıflarını Kullanma
public class MediaDetectionUser
{
public string Username { get; set; }
public ICollection<IMediaDetectionRole> Roles { get; set; }
public MediaDetectionUser()
{
Roles = new List<IMediaDetectionRole>();
}
}
public interface IMediaDetectionRole
{
string Name { get; }
string GetDescription();
string GetRoleType {get;}
}
[ElasticsearchType(Name="MediaDetectionAdminRole")]
public class MediaDetectionAdminRole : IMediaDetectionRole
{
public string Name { get { return "Admin"; } }
public string GetDescription() { return "Admin users can create other users within the account"; }
public string GetRoleType { get { return this.GetType().Name; } }
}
[ElasticsearchType(Name = "MediaDetectionManagerRole")]
public class MediaDetectionManagerRole : IMediaDetectionRole
{
public string Name { get { return "Manager"; } }
public string GetDescription() { return "Managers can modify account-level properties"; }
public string GetRoleType { get { return this.GetType().Name; } }
}
[ElasticsearchType(Name = "MediaDetectionCreatorRole")]
public class MediaDetectionCreatorRole : IMediaDetectionRole
{
public string Name { get { return "Creator"; } }
public string GetDescription() { return "Creators can create new Media Detection Profiles"; }
public string GetRoleType { get { return this.GetType().Name; } }
}
ama veriler, veri ANDAE sorgulamak giderken can: Orada her bir IMediaDetectionRole arayüzünden kaynaklanan Rollerin birkaç farklı türleri vardır
Bu rollerin türünü anlamaya. Ben olsun:
Could not create an instance of type IMediaDetectionRole.
Type is an interface or abstract class and cannot be instantiated.
Path 'hits.hits[0]._source.roles[0].name', line 1, position 343.
geri doğru sınıf türüne yuvalanmış nesne harita verilerinde doğru yolu nedir?
Çok teşekkürler!
-Z
Bu tam kullanım durumu belgelerinde açıklanmıştır. Https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/covariant-search-results.html :) –
Teşekkürler Martijn, I Bunun en üst düzey bir nesne için nasıl işe yaradığını görün. Ancak bu örnekler MediaDetectionUser nesnesindeki bir listenin parçasıdır. İç içe bir nesnenin türlerini çözmek için .Types() veya ConcreteTypeSelector'ı nasıl kullanacağım açık değil. – zorlack
DTO içindeki koleksiyonunuz için [JsonPropery (ItemTypeHandling = TypeHandling.Objects] kullanabilirsiniz, bu seri hale getirilmiş sonuca $ türünü ekleyecektir ve json.net bunu doğru sınıfa geri gönderebilecektir –