Obsolete Handling on field name in c #

I have been struggling with obsolete handling, I know what is the usage of obsolete in c#. But my question is not the usage of obsolete is managing.

I have a class such as salestransactionstep that contains name, description, and quantity, like the below:

public class salestransactionstep
{
  string name;
  string description;
  int quantity;

}

I want to change the quantity field data type to float. Because the int field does not meet my demand anymore. But some customers may use the int field but others don't. So, I don't want to change the field and I added a new field which data type is float like this and I add obsolete property to my old field.

public class salestransactionstep
{
  string name;
  string description;
  [Obsolete]
  int quantity;
  float quantity_float;

}

So some of the old customers use the int field, others use the float field. I use SQL query for taking the data which sometimes calculate int field for meeting the old customer demand, and sometimes calculate float field for meeting new customer. Like;

select count(quantity) from salestransactionstep

OR

select count(quantity_float) from salestransactionstep

But in my vision, I don't manage this usage properly. So how can I manage this, handle the obsolete field properly?

2 answers

  • answered 2021-06-10 11:14 NewGuy

    The way you use it currently, will only trigger a warning in your code when you try to reach the property of the class.

    If you want trigger an error instead you could do this:

    [Obsolete("Obsolete prop - Use quantity_float", true)]
    

    Change to false, if you just only want to change the warning message.

  • answered 2021-06-10 11:21 Mike

    Note sure about your setup, if your exposing only internal methods or web services - - but I try to avoid making changes to existing contracts since it becomes a mess to maintain over time.

    Consider adding versioning for better readability, modularization, separation of concern etc.

    You would also get the options for "legacy clients" to use the new version at some point when ready, which makes it possible to remove the legacy code all together.