24 April 2007

Jedi Way -- Implementing Members

Object-oriented program is easy to read, if written properly. At every point you deal with appropriate level of abstractness and you don't have to deal with implementation specific details most of the time. However, when you have large type hierarchies and you are going to modify some aspect of a top level interface -- you may be in trouble. You have to thoroughly investigate all implementing types and provide method body for each one. ReSharper can help, of course.

Jedi Trick Level 1
Invoke Type Hierarchy and select Derived Types in the toolbar. You will see hierarchy of types derived from the interface. Use "Go To Next/Previous Occurence" command (Ctrl-Alt-Down/Up) to navigate between types. As soon as you have type which requires implementation in the code editor, Code Analysis will show red squiggly. Invoke Quick Fix to implement member and type in method body.

Jedi Trick Level 2
Use Context Action's power to implement members. Position caret on the newely created member, IsAvailable in this case. You will see the light saber bulb which is activated with Alt-Enter. Select "Implement member" and you will be prompted with the list of types. You can select "All above types" and get method body throwing NotImplementedException for every type.

Jedi Trick Level 3
Use Quick Fix power to implement members. Type in method body right in the interface, as if it were implementation. Code Analysis will show red squiggly, because interface member cannot have body. But you will also get two Quick Fixes via red light bulb - remove method body or use body for implementations. As soon as you select second one, body will be copied to appropriate implementations and removed from interface.

Jedi Trick Level 4
Available to Yoda only.

Note: Some or all of the features mentioned in this article may be available only in latest EAP versions of ReSharper.

No comments: