code_junkie

Extension interface patterns

Nov 10th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. public interface IHaveChildren {
  2. string ParentType { get; }
  3. int ParentId { get; }
  4.  
  5. List<IChild> GetChildren()
  6. }
  7.  
  8. public interface IHaveChildren {
  9. string ParentType { get; }
  10. int ParentId { get; }
  11. }
  12.  
  13. public static class HaveChildrenExtension {
  14. public static List<IChild> GetChildren( this IHaveChildren ) {
  15. //logic to get children by parent type and id
  16. //shared for all classes implementing IHaveChildren
  17. }
  18. }
  19.  
  20. public interface INode {
  21. INode Root { get; }
  22. List<INode> GetChildren( );
  23. }
  24.  
  25. public interface IChildNode : INode {
  26. INode Parent { get; }
  27. }
  28.  
  29. public static class NodeExtensions {
  30. public INode GetParent( this INode node ) {
  31. // If the node implements the new interface, call it directly.
  32. var childNode = node as IChildNode;
  33. if( !object.ReferenceEquals( childNode, null ) )
  34. return childNode.Parent;
  35.  
  36. // Otherwise, fall back on a default implementation.
  37. return FindParent( node, node.Root );
  38. }
  39. }
  40.  
  41. public interface ILongMethod {
  42. public bool LongMethod( string s, double d, int i, object o, ... );
  43. }
  44.  
  45. ...
  46. public static LongMethodExtensions {
  47. public bool LongMethod( this ILongMethod lm, string s, double d ) {
  48. lm.LongMethod( s, d, 0, null );
  49. }
  50. ...
  51. }
  52.  
  53. IInterface variable = new ImplementingClass();
  54.  
  55. public interface IHaveChildren
  56. {
  57. string ParentType { get; }
  58. int ParentId { get; }
  59. }
  60.  
  61. public interface IChildIterator
  62. {
  63. IEnumerable<IChild> GetChildren();
  64. }
  65.  
  66. public void DefaultChildIterator : IChildIterator
  67. {
  68. private readonly IHaveChildren _parent;
  69.  
  70. public DefaultChildIterator(IHaveChildren parent)
  71. {
  72. _parent = parent;
  73. }
  74.  
  75. public IEnumerable<IChild> GetChildren()
  76. {
  77. // default child iterator impl
  78. }
  79. }
  80.  
  81. public class Node : IHaveChildren, IChildIterator
  82. {
  83. // *snip*
  84.  
  85. public IEnumerable<IChild> GetChildren()
  86. {
  87. return new DefaultChildIterator(this).GetChildren();
  88. }
  89. }
  90.  
  91. ((IFirst)this).AmbigousMethod()
  92.  
  93. namespace Model
  94. {
  95. class Person
  96. {
  97. public string Title { get; set; }
  98. public string FirstName { get; set; }
  99. public string Surname { get; set; }
  100. }
  101. }
  102.  
  103. namespace View
  104. {
  105. static class PersonExtensions
  106. {
  107. public static string FullName(this Model.Person p)
  108. {
  109. return p.Title + " " + p.FirstName + " " + p.Surname;
  110. }
  111.  
  112. public static string FormalName(this Model.Person p)
  113. {
  114. return p.Title + " " + p.FirstName[0] + ". " + p.Surname;
  115. }
  116. }
  117. }
  118.  
  119. public static List<long> GetIds<T>(this List<T> original){
  120. List<long> ret = new List<long>();
  121. if (original == null)
  122. return ret;
  123.  
  124. try
  125. {
  126. foreach (T t in original)
  127. {
  128. IIDable idable = (IIDable)t;
  129. ret.Add(idable.getId());
  130. }
  131. return ret;
  132. }
  133. catch (Exception)
  134. {
  135. throw new Exception("Class calling this extension must implement IIDable interface");
  136. }
Add Comment
Please, Sign In to add comment