Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to understand the benefit of inheriting the model class with
Dictionary<String, Object> 
in web api project.

Below is the sample of model class.
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace IO.Swagger.Model {

  /// <summary>
  /// 
  /// </summary>
  [DataContract]
  public class ApiErrorResponse : Dictionary<String, Object> {
    /// <summary>
    /// Gets or Sets Errors
    /// </summary>
    [DataMember(Name="Errors", EmitDefaultValue=false)]
    [JsonProperty(PropertyName = "Errors")]
    public List<IntegrationErrorDto> Errors { get; set; }

    /// <summary>
    /// Gets or Sets CorrelationKey
    /// </summary>
    [DataMember(Name="CorrelationKey", EmitDefaultValue=false)]
    [JsonProperty(PropertyName = "CorrelationKey")]
    public string CorrelationKey { get; set; }


    /// <summary>
    /// Get the string presentation of the object
    /// </summary>
    /// <returns>String presentation of the object</returns>
    public override string ToString()  {
      var sb = new StringBuilder();
      sb.Append("class ApiErrorResponse {\n");
      sb.Append("  Errors: ").Append(Errors).Append("\n");
      sb.Append("  CorrelationKey: ").Append(CorrelationKey).Append("\n");
      sb.Append("}\n");
      return sb.ToString();
    }
    /// <summary>
    /// Get the JSON string presentation of the object
    /// </summary>
    /// <returns>JSON string presentation of the object</returns>
    public  new string ToJson() {
      return JsonConvert.SerializeObject(this, Formatting.Indented);
    }

}
}





Each model class are inherited form
Dictionary<String, Object>

Ech model class override the
ToString() method

each model class have
ToJson()
method.

Please help me to figure out the situation in which i can follow the same approach

What I have tried:

What is notice that
ToJson()
METHOD EASILY SERIALIZE THE OBJECT DATA.
Posted
Comments
[no name] 23-Mar-21 12:54pm    
It is what it is. It's a dictionary with some extra methods. The alternative is what?
Siddharth Rai 23-Mar-21 15:27pm    
i am trying to understand what are the extra benefit i can achieve by it.
What if i will create simple model class with all required data annotation and bind the data inside it regather then inheriting the Dictionary class.
[no name] 24-Mar-21 12:26pm    
There is no benefit if you don't need it. "What are the benefits" is like a solution looking for a problem. If you never "serialize", you'll never "benefit" from the "ToJson" method. If you don't need to "debug/dump" your class, you don't need a custom "ToString", etc. They're more like (common) "stubs" for and if you need them. Somebody's "convention"; nothing to lose sleep over.
Siddharth Rai 25-Mar-21 10:02am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900