Using JetBrain's ReSharper 9 to implement (and learn) Design Patterns
One of ReSharper's best kept secrets are Smart Templates. Today, I show you how to master your design patterns using a free download.
If you've been a Visual Studio developer for a long time, you no doubt know that you cannot live without ReSharper and it's productivity tools when it comes to code enhancements.
After doing my 5 ways to take your ASP.NET MVC Development to "Ludicrous Speed," I always try to figure out a way to make it faster.
Usually code automation is key when generating fast and solid code. It's tried and tested code and that it's always worked for you and you can create your own boilerplate templates to generate code at the blink of an eye.
ReSharper Smart Templates
This is where JetBrains ReSharper's Smart Templates come into focus. These templates are common code structures that you build to speed up your development.
Think of Resharper's Smart Templates as code macros on Steroids!
While Resharper already has a number of them available, I'm always creating new templates to increase my productivity.
Patterns, Patterns, and More Patterns
While going through some code one day, I wanted to implement an observable pattern. I was having a "garbage collection/memory dump" day where I had a headache and couldn't think about how to implement one.
Then it hit me.
Why not have a collection of sample design patterns as Smart Templates?
After entering in all 23 design patterns to Resharper as Smart Templates, I thought others may have a need for these.
So I'm packaging them up and sending them to you.
Installation
After you receive your .zip file, unzip the files into a directory. You will see the ASP.NET MVC Guidelines PDF and the DesignPattern.DotSettings file.
- Open your Resharper Templates Explorer.
- Click the Import button and locate your DesignPatterns.dotSettings file.
- You should be ready to start implementing your design patterns in your code.
At this point, you can close the templates explorer and open a code window and start testing your Design Pattern Smart Templates.
How is everything structured?
It's difficult to remember how to create a design pattern unless you've actually seen a piece of code that mimics the pattern.
It's even more difficult to relate certain pieces of code to a pattern which is why I decide to make things a little easier by putting the Smart Templates into naming categories.
To start using your new Smart Templates:
- Enter '_dp' for Design Pattern.
- Then decide on the type of Design Pattern you wish to use:
- Enter 'b' for Behavorial Patterns
- Enter 's' for Structural Patterns
- Enter 'c' for Creational Patterns
- Finally, enter the type of pattern you wish to use.
The chart below describes the design patterns available and their keyword shortcut.
Shortcut | Type | Description |
---|---|---|
_dpbc | Behavorial | Command Pattern |
_dpbcor | Behavorial | Chain of Responsibility |
_dpbint | Behavorial | Interpreter |
_dpbit | Behavorial | Interator (debated as to whether to include this or not) |
_dpbmed | Behavorial | Mediator |
_dpbmem | Behavorial | Memento |
_dpbo | Behavorial | Observer |
_dpbst | Behavorial | State |
_dpbstr | Behavorial | Strategy |
_dpcaf | Creational | Abstract Factory |
_dpcb | Creational | Builder |
_dpcf | Creational | Factory |
_dpcp | Creational | Prototype |
_dpcs | Creational | Singleton |
_dpsa | Structural | Adapter |
_dpsb | Structural | Bridge |
_dpsc | Structural | Composite |
_dpsd | Structural | Decorator |
_dpsf | Structural | Facade |
_dpsfly | Structural | Flyweight |
_dpsp | Structural | Proxy |
So if you wanted to see what a Decorator pattern would look like, enter:
- Enter '_dp' for Design Pattern.
- 's' to learn it's a Structural pattern
- and finally enter 'd' for Decorator.
After entering '_dpsd,' press Tab and you get the entire code structure with assistance as to what you want to call your new class, fields, and/or properties.
A majority of these samples were taken from the DOFactory.com.
Conclusion
I hope these Smart Templates help you in your coding travels. If you want to expand these patterns, please let me know and I will modify them and update the library for everyone.