How can I refactor a simple class member to a more complex type?

Loofer

I have a little class

public class ExcitingResults
{
    public int A { get; set; }
    public string B { get; set; }
    public DateTime C { get; set; }

    public string ComplexD
    {
        get { return SomeMethod("A", "B"); }
    }
}

Currently I do some processing with results a List<ExcitingResults> that creates JSON or Excel files. This processing relies strongly on the name of each property and the order that things appear in the output is defined by the order they appear in the class. Though in fact this is undefined behaviour and so could change if the compiler decides it wants things in a different order.

For this reason I would like to alter my class to something like this.

public class ExcitingResults
{
    public column<int> A { get; set; }
    public column<string> B { get; set; }
    public column<DateTime> C { get; set; }

    public column<string> ComplexD
    {
        get { return SomeMethod(A.Name, B.Name); }
    }
}

public class column<T>
{
    public T Value { get; set; }
    public string Name { get; set; }
}

Clearly this is not a drop in replacement... is there some clever strategy I can take to make this change as painless as possible. Then in the future add complexity to the Column class such as order, friendly names and other attributes as they occur to me.

Or is this a crazy way forwards and I should be looking at another strategy to be able to add this extra information to the columns?

eoinmullan

You could add property attributes, with the advantage that you don't touch the API of the class. For example, add the class:

[AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute : System.Attribute {
    public string FriendlyName { get; set; }
    public int Rank { get; set; }
}

You can now annotate your ExcitingResults class like this:

public class ExcitingResults {
    [Column(FriendlyName = "A", Rank = 1)]
    public int A { get; set; }

    [Column(FriendlyName = "B", Rank = 4)]
    public string B { get; set; }

    [Column(FriendlyName = "C", Rank = 3)]
    public DateTime C { get; set; }


    [Column(FriendlyName = "D", Rank = 2)]
    public string ComplexD {
        get { return SomeMethod("A", "B"); }
    }
}

Your processor class can then access the property attributes using reflection. For example, to print out an ExcitingResult's properties in order of Rank:

public static void Process(ExcitingResults result) {
    var propertiesByRank = typeof(ExcitingResults)
        .GetProperties()
        .OrderBy(x => x.GetCustomAttribute<ColumnAttribute>().Rank);

    foreach (var propertyInfo in propertiesByRank) {
        var property = result.GetType().GetProperty(propertyInfo.Name);
        var propertysFriendlyName = property.GetCustomAttribute<ColumnAttribute>().FriendlyName;
        var propertysValue = property.GetValue(result, null);
        Console.WriteLine($"{propertysFriendlyName} = {propertysValue}");
    }
}

You'll want to do more error checking in your processing class, but you get the idea. See MSDN documentation and tutorial for more info.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I refactor these simple functions and make them more DRY?

From Dev

How can I refactor this code to be more concise?

From Dev

How can I declare a templated class type as a member method, *not* a property?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I refactor / make this code more pythonic?

From Dev

How can I refactor this method using anonymous projection to be more generic?

From Dev

How can i register an open generic type with a complex type constraint in Simple Injector?

From Dev

How can i define in xsd that a simple/complex type contain another XML?

From Dev

How can I tell PHPStorm to refactor namespaces and class names?

From Dev

How can I tell PHPStorm to refactor namespaces and class names?

From Dev

How can I define dynamic class member?

From Dev

How can I add programmatically a more complex column in an gridview

From Dev

How can I initialize a static data member of a class template with type traits?

From Dev

How can I initialize a static data member of a class template with type traits?

From Dev

Do I need one more simple class for searching a complex business entity?

From Dev

Why can't I pass a type member of a class as a template parameter?

From Dev

How to Compare Member of Array of Complex Type to a SELECT?

From Dev

How can I AJAX POST this complex type to my MVC action?

From Java

How can I use typescript to destructure from a complex type?

From Dev

How can I print a complex dictionary data type in Python?

From Dev

How can I use Automapper to map this complex type

From Dev

How to use inheritance in a more complex type in Julia

From Java

how can i write more simple code without new select?

From Dev

Simple, but can't find: syntax to work with member variables of STL queues with type class

From Dev

How can I refactor a set of ugly if statements?

From Dev

How can i refactor these lines of code?

From Dev

How Can I Refactor To Avoid Duplication?

From Dev

Swift refactor with generics: how can I imply runtime values from the generic type?

Related Related

  1. 1

    How can I refactor these simple functions and make them more DRY?

  2. 2

    How can I refactor these simple functions and make them more DRY?

  3. 3

    How can I refactor this code to be more concise?

  4. 4

    How can I declare a templated class type as a member method, *not* a property?

  5. 5

    How can I refactor my kotlin code and make it more clear

  6. 6

    How can I refactor / make this code more pythonic?

  7. 7

    How can I refactor this method using anonymous projection to be more generic?

  8. 8

    How can i register an open generic type with a complex type constraint in Simple Injector?

  9. 9

    How can i define in xsd that a simple/complex type contain another XML?

  10. 10

    How can I tell PHPStorm to refactor namespaces and class names?

  11. 11

    How can I tell PHPStorm to refactor namespaces and class names?

  12. 12

    How can I define dynamic class member?

  13. 13

    How can I add programmatically a more complex column in an gridview

  14. 14

    How can I initialize a static data member of a class template with type traits?

  15. 15

    How can I initialize a static data member of a class template with type traits?

  16. 16

    Do I need one more simple class for searching a complex business entity?

  17. 17

    Why can't I pass a type member of a class as a template parameter?

  18. 18

    How to Compare Member of Array of Complex Type to a SELECT?

  19. 19

    How can I AJAX POST this complex type to my MVC action?

  20. 20

    How can I use typescript to destructure from a complex type?

  21. 21

    How can I print a complex dictionary data type in Python?

  22. 22

    How can I use Automapper to map this complex type

  23. 23

    How to use inheritance in a more complex type in Julia

  24. 24

    how can i write more simple code without new select?

  25. 25

    Simple, but can't find: syntax to work with member variables of STL queues with type class

  26. 26

    How can I refactor a set of ugly if statements?

  27. 27

    How can i refactor these lines of code?

  28. 28

    How Can I Refactor To Avoid Duplication?

  29. 29

    Swift refactor with generics: how can I imply runtime values from the generic type?

HotTag

Archive