Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2759

Specify elements of SortDirection enum with appropriate values of MongoDB

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Won't Do
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None

      Description

      Now the elements of enum `SortDirection` does not have int numbers:

      ```csharp

      public enum SortDirection

      {     Ascending,     Descending }

      ```

      Because of this, a lot of logic in the code of `MongoDB.Driver` library to verify compliance with the elements and numbers (-1 and 1) like this:

      ```csharp

      switch (_direction)

      {     case SortDirection.Ascending:         value = 1;         break;     case SortDirection.Descending:         value = -1;         break;     default:         throw new InvalidOperationException("Unknown value for " + typeof(SortDirection) + "."); }

      ```

      This is unnecessary logic, because the C# has built-in casting enum to Int32 via setting numbers for appropriate enum elements. So the enum `SortDirection` might be refactored like this:

      ```csharp

      public enum SortDirection

      {     Ascending = 1,     Descending = -1 }

      ```

      In this case no more additional logic needed. And in the future, if this numbers will be changed (for example, `100` for `Ascending` and `200` for `Descending`), there is one place to change code - `SortDirection` enum.

      Any string representation of `SortDirection` enum elements looks like this:

      ```csharp

      $"ascending is: {(int) SortDirection.Ascending}"

      ```

        Attachments

          Activity

            People

            Assignee:
            rstam Robert Stam
            Reporter:
            av-1991@yandex.ru Valeriy Abakumov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: