8 thoughts on “Web API and Interface Parameters

  1. hfarouk says:

    Tried to run the code provided on GitHub but it is not working as expected. The Service locator will pick up/resolve an interface to the last registered concrete class not based on the given serialized type(i.e json payload).

    • brettedotnet says:

      hfarouk,

      Sorry you had issues with the code. Could you provide to me your container registrations so I can better understand what went wrong? Are you using named registrations?

      Please let me know and I will do what I can to correct.

      BDN

  2. jmartinzone says:

    Thank you for this article. I looked around for several hours yesterday looking at various solutions to this situation. Though I dd not go full “generic” I did adapt it to cover the cases I needed with a single converter implementation which took the Interface Type and Implementation class:

    public class MyCustomConverter : CustomCreationConverter where C: I , new()

    I then registered an instance of the converter for each interface/implementation pair:
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyCustomConverter());
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyCustomConverter());
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyCustomConverter());

    Thanks for the guidance. I’d still be working on this today without it!

  3. Yogesh Soni says:

    Thanks for the nice blog.
    Can you please describe more on to deal with different derived types handling through interface parameter in Web API. I have tried the same thing but not able to create instance of different derived types. Please help me on the same.

    • brettedotnet says:

      In the case multiple derived types, I generally will implement a marker interface called IPolymorphic (just a a marker call it anything you want…). Once marked the custom creation converter can use the presense of the interface as an indicator that it needs to use a factory in order to create the correct concrete type.

      1. In ReadJson inspect the type and see if it of type IPolymorphic

      2. If it is resolve your factory from the container, and ask it for the correct concrete type.

      Brette

  4. kengranderson says:

    Great article, will definitely use this info!

    I had to smile at your comments about the ‘cool kids,’ as I am a committed Windows / PC user since 1988, when I wanted to go GUI and realized that Macs cost twice as much, so I got a $1000 PC clone and Windows 2.03 (and a year later helped a friend set up a desktop publishing business on Windows 2.11, PageMaker 3.01 and an HP LaserJet II for about a third to a half the cost if we had gone Mac.

    My $1000 PC clone was also the last desktop I ever purchased from a store. Been upgrading components and building new ones ever since.

    As far as notebooks, $800 is my maximum spend (before RAM upgrades and SSDs), and I have been using 17″ screens for quite a few years. My bank account loves my PC notebooks!

    And have also love my Windows Phones – after my HTC I got in 2011 dropped and cracked in 2015, an awesome T-Mobile rep turned me onto the fact that I could get a $40 Lumia 435 minute phone from Best Buy and they could cut my SIM card to fit in it, which is what I did. Worked great until the TLS update and I could do my banking anymore, but at a social gathering, when a friend who works at Microsoft learned I was using a WIndows phone, he gave me a demo Lumia 950XL that he had been having trouble giving away, so my total spend on phones since 2015 has been $40!

    While I can’t do some things like FaceTime on my phone, I learned from watching ‘The Jetsons’ that video phone calls are often more trouble than they are worth, and I already know what anyone I call looks like, so I’m not missing it in the least.

    Also, while I think the ‘cool kids’ tend to love coding in languages that IMO hearken back to the 70s, when compilers needed semicolons to know when lines of code ended, overloaded punctuation characters like the curly brace for multiple language constructs, and lauded terseness as a benefit because every byte of RAM was precious, while I started my professional programming career in C and C++, and have forgotten more languages than most folks will ever learn, my language of choice has always been BASIC, have coded primarily in VB.NET since the betas, and neither myself nor others have any problems reading and understanding my VB.NET code that I wrote over a decade ago, some of which has been running without modifications on both online and offline applications.

    Oh, and I hate skinny jeans. Though they looked stupid (on guys) in the 80s, still do.

  5. […] fixing it. I looked at ASP.NET Web API Operation with interfaces instead concrete class and also at https://brettedotnet.wordpress.com/2014/07/16/web-api-and-interface-parameters/ and at ASP.NET Web API Operation with interfaces instead concrete class, but none of the […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: