2

I have two httppost method one is taking an a concrete class with is working fine. The other one is taking in an interface. When I use postman I get null for the interface method.Should I be able to send a model class to an interface do I need to use custom model binder and if I do where do I need to add it?

public IHttpActionResult InsertData([FromBody] DataLog _datalog)

public IHttpActionResult Insert([ModelBinder(typeof(IDataLog))] IDataLog _log)
  • Good information about the differences in Interface and class. [Abstract vs defined](http://net-informations.com/faq/general/class-interface.htm) – Jawad Jan 29 '20 at 15:58
  • You are almost there. You need to create a custom model binder and use that with your `ModelBinder` attribute. So `[ModelBinder(typeof(DataLogModelBinder))]` – Kevin Babcock Jan 29 '20 at 16:32
  • Check out this article for help writing a custom model binder: https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api – Kevin Babcock Jan 29 '20 at 16:33
  • 1
    @KevinBabcock would that be a class within the model –  Jan 29 '20 at 16:35
  • @Jefferson Sorry, I misread your comment. It would be a separate class than the model. Where you define it depends on the organization and structure of your application. – Kevin Babcock Jan 29 '20 at 16:38

2 Answers2

1

I think this would answer your question as well.

Interfaces

Reason why you cannot use interface as an object is because there are no public accessors for them. They also cannot have fields. It is just signature of the properties, and methods.

Interfaces

  1. cannot have implementation
  2. cannot have modifiers public
  3. cannot have virtual

Classes

Each class you define have public properties that have getters and setters. Setters are what sets the values of these public properties. You can use the interfaces and extend them to classes and use these classes as objects to receive the data

Good read about defining and implementing interfaces

Custom Model Binding in ASP with examples

Jawad
  • 11,028
  • 3
  • 24
  • 37
  • Thanks @jawad so what is ModelBinder used for? –  Jan 29 '20 at 16:21
  • the ModelBinder attribute specifies the type of IModelBinder that should be used to bind Class's action parameters – Jawad Jan 29 '20 at 16:24
  • would there be another way of doing this? –  Jan 29 '20 at 16:31
  • Instead of copy pasting the information on how to create a binder, use that for class and then implementing that class in your API call, [this page defines how to create custom model binding](https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/custom-model-binding?view=aspnetcore-3.1) – Jawad Jan 29 '20 at 16:39
0

I don´t know if i understand your question. Anyway regarding the code i must say:

  1. Do not use '_' to named your variables, classes, parameters...it is not CLS compliant. https://learn.microsoft.com/en-us/dotnet/api/system.clscompliantattribute?view=netframework-4.8

  2. Maybe you can use the letter 'D' following the SOLID principle. D as Dependency Inversion using the IOC to Dependency Injection. https://en.wikipedia.org/wiki/SOLIDhttps://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1

  3. In case that you have only one return type within your method, instead of using generic ActionResults types you can do something like, just to understand the logic:

Instead of:

public ActionResult Index()
{
    return View();
}

Do this:

public ViewResult Index()
{
    return View();
}

Difference Between ViewResult() and ActionResult()

  1. Do Async await (TAP ) pattern. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

  2. Instead of:

     public IHttpActionResult InsertData([FromBody] DataLog _datalog)
    

    Make the code to compile faster like this, adding the HttpPost attribute, even knowing this is a POST action:

    Do this:

    [HttpPost]
     public IHttpActionResult InsertData([FromBody] DataLog _datalog)
    

Hope those good practices helps you somehow! :)

Sergio Rezende
  • 762
  • 8
  • 25
  • hmmm with regards to point [1] - I always see `_` when using dependency injection. e.g, `private readonly IDataContext _dataContext;` . Where specifically does it say not to use that symbol? – Sean T Jan 29 '20 at 16:31
  • @SeanT follow this : https://stackoverflow.com/a/862796/8207463 to understand the point – Sergio Rezende Jan 29 '20 at 16:34
  • that says nothing about using a leading underscore – Sean T Jan 29 '20 at 16:37
  • That is talking about unsigned integers. It's got nothing to do with an underscore lol – Sean T Jan 29 '20 at 16:39
  • using an underscore is perfectly fine, I can understand avoidance of keywords and things like that obviously. https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines – Sean T Jan 29 '20 at 16:44
  • Please, just use the attribute as the answer within your code and check the results. – Sergio Rezende Jan 29 '20 at 16:45
  • I already have done 1000's of times before and never encountered a problem. That's what I'm saying, your advice for [1] is unfounded. `_` is compliant – Sean T Jan 29 '20 at 16:47
  • @SeanT '_' is not CLS Compliant. CLS-compliant language compilers must follow the rules in Appendix 7 of Technical Report 15 of the Unicode 3.0 standard, which regulates the character set, which begins and is contained in identifiers. This pattern is available on the Unicode Consortium website.https://www.generacodice.com/de/articolo/384240/Why-is-this-name-with-an-underscore-not-CLS-Compliant – Sergio Rezende Jan 29 '20 at 17:44
  • Ah ok, this only applies to `public`/`protected` members though. It's fine for `private` properties - so is ok in the example I provided. In fact it's typically used to signify a variable is private when using certain naming conventions. – Sean T Jan 30 '20 at 10:09