c# - Extension Methods vs Instance Methods vs Static Class -
i'm little bit confused different ways use methods interact objects in c#, particularly major design differences , consequences between following:
- invoking instance method
- using static class on poco
- creating extension method
example:
public class mypoint { public double x { get; set; } public double y { get; set; } public double? distancefrom(mypoint p) { if (p != null) { return math.sqrt(math.pow(this.x - p.x, 2) + math.pow(this.y - p.y, 2)); } return null; } }
if accomplish desired outcome placing method in class definition, why poco combined either static helper class or extension method preferable?
you asked, "if accomplish desired outcome placing method in class definition, why poco combined either static helper class or extension method preferable?"
the answer depends on situation, , if methods in question directly related class' primary concern (see single responsibility principle).
here examples of might idea use each type of approach/method (using code sample starting point).
1. instance methods
//this makes sense instance methods because you're //encapsulating logic mypoint concerned with. public class mypoint { public double x { get; set; } public double y { get; set; } public double? distancefrom(mypoint p) { if (p != null) return math.sqrt(math.pow(this.x - p.x, 2) + math.pow(this.y - p.y, 2)); return null; } }
2. static class methods - error logging example.
//your class doesn't directly concern logging implmentation; //that's better left separate class, perhaps //a "logger" utility class static methods available class. public double? distancefrom(mypoint p) { try { if (p != null) return math.sqrt(math.pow(this.x - p.x, 2) + math.pow(this.y - p.y, 2)); return null; } catch(exception ex) { //**** static helper class can called other classes **** logger.logerror(ex); //note: logger might encapsulate other logging methods like... //logger.loginformation(string s) //...so extension method less natural, since logger //doesn't relate specific base type can create //extension method for. } }
3. extension methods - xml serialization example.
//maybe want make object can xml serialize //using easy-to-use, shared syntax. //your mypoint class isn't directly concerned xml serialization, //so doesn't make sense implement instance method //mypoint can pick capability extension method. public static class xmlserialization { public static string toxml(this object valuetoserialize) { var serializer = new xmlserializer(valuetoserialize.gettype()); var sb = new stringbuilder(); using (var writer = new stringwriter(sb)) serializer.serialize(writer, valuetoserialize); return sb.tostring(); } } //example usage var point = new mypoint(); var pointxml = point.toxml(); //<- extension method
the rule of thumb is:
- if method relates class' primary concern, put in instance method.
- if have generic utility might useful multiple classes, consider putting in static class' method.
- if have situation similar 2, related single base type, or think code cleaner/more concise without having separately reference static class, consider extension method.
Comments
Post a Comment