# Using Interface Derived Classes with NEST

**URL:** <https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150>\
**Category:** Elasticsearch\
**Created:** [April 12, 2016, 4:22pm UTC](https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150 "2016-04-12T16:22:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zorlack](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zorlack/32/42730_2.png) [@Zorlack](https://discuss.elastic.co/u/Zorlack)\
**Post date:** [April 12, 2016, 4:22pm UTC](https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150/1 "2016-04-12T16:22:36Z")

</div>

I'm having trouble using NEST's strongly-type objects with a nested list of Interface-Derived user roles.

I've posted a question about it on StackOverflow: [http://stackoverflow.com/questions/36578801/using-interface-derived-classes-with-nest-and-elasticsearch](http://stackoverflow.com/questions/36578801/using-interface-derived-classes-with-nest-and-elasticsearch)

Specifically, NEST can't figure out the right class to use and throws an exception:

```
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.

```

What is the "NESTy" way to solve this problem?

Many thanks!

-Z

---

<div class="post-metadata">

**Author:** ![KodrAus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kodraus/32/22798_2.png) [@KodrAus](https://discuss.elastic.co/u/KodrAus)\
**Post date:** [April 15, 2016, 6:03am UTC](https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150/2 "2016-04-15T06:03:47Z")

</div>

I'm guessing your SO example is a simplified one? Otherwise you wouldn't need an interface at all.

You may be able to get around this using a custom [JsonConverter](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm) over your `Roles` property. That would need to parse the json to some temp object, switch on the value of `GetRoleType` and hydrate and return the appropriate concrete object.

Or more simply, you could have just the one `MediaDetectionRole` class that you use with Elasticsearch, and expose a method like `AsIMediaDetectionRole` that does the switch for you after the fact.

---

<div class="post-metadata">

**Author:** ![Zorlack](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zorlack/32/42730_2.png) [@Zorlack](https://discuss.elastic.co/u/Zorlack)\
**Post date:** [April 15, 2016, 6:47pm UTC](https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150/3 "2016-04-15T18:47:52Z")

</div>

Indeed this is a simplified example. Long term the various roles will have some differing features.

I documented my fix and updated the Stack Overflow question here: [http://stackoverflow.com/questions/36578801/using-interface-derived-classes-with-nest-and-elasticsearch/36654644#36654644](http://stackoverflow.com/questions/36578801/using-interface-derived-classes-with-nest-and-elasticsearch/36654644#36654644)

Many thanks @KodrAus for your response and for steering me towards [JSON.net](http://JSON.net) for a solution and thanks to @Martijn_Laarman for showing me that I could resolve this issue using a [JsonProperty] attribute to force the rendering of Type information within the nested List.

Cheers!

-Z

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:58pm UTC](https://discuss.elastic.co/t/using-interface-derived-classes-with-nest/47150/4 "2017-07-05T22:58:53Z")

</div>


