I would like to preface that I have not used Dynamic Objects very often, and only recently came across this problem. I have a specific scenario that I will explain below, but I was wondering what exactly were the advantages of implementing a dynamic object compared to creating a class for that object.
I have a method that takes a dynamic object as a parameter. For the sake of length I will not post much code just enough to get the point across:
public static Tax GetEmployeeTax(string Id, Employee employee, dynamic inputObject)
{
var temp = new Employee();
//use the dynamic object properties
return temp;
}
In this case, inputObject
will have properties that help identify the employee taxes without directly being related to the employee class. Primarily I have given the inputObject
the following properties:
dynamic dynamicEmployeeTax = new ExpandoObject();
dynamicEmployeeTax.FederalTaxInfo = "Some information";
dynamicEmployeeTax.StateTaxInfo = "Some other information";
Are there any advantages to making this it's own class versus using a dynamic object? Are there any benefits one way or the other?
There are several reasons why you want to create a class:
The list goes on ad infinitum. I think the consensus here is to avoid it. There are extreme rare cases where this is useful. For most, stick to contracts and coding to abstractions not implementation details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With