Hybrid Dependency Injection : The Code Snippet

by sven 22 October 2008 21:15

Typing those external dependency properties by hand can get real annoying, so today I've made a handy snippet I'd thought I'd share.

With this snippet, when needing an external dependency in a class, just type 'exdep' and press tab, it will be expanded to :

image

Then just type the name of the class you want to use, for instance MyDataAccessLayer class, and the snippet will prefix the interface with Ixxx, and the backing field with an underscore, giving you:

   1: private object _MyDataAccessLayerClass;
   2: private IMyDataAccessLayerClass MyDataAccessLayerClass
   3: {
   4:     get
   5:     {
   6:         if (!(_MyDataAccessLayerClass is IMyDataAccessLayerClass))
   7:             _MyDataAccessLayerClass = new MyDataAccessLayerClass();
   8:         return (IMyDataAccessLayerClass)_MyDataAccessLayerClass;
   9:     }
  10: }

So much for typing all those lines by hand anymore !

Now the good stuff : save the exdep.snippet that is attached to this post somewhere on your filesystem. Then in Visual Studio 200!, go to Tools -> Snippet Manager. Then select Visual C# from the combobox, and click the 'Import' button. Then browse to the exdep.snippet file, et voila, you can access the snippet with the 'exdep' shortcut.

That's all for now, I've got to start packing for the PDC Party

Attached file : exdep.snippet (1,60 kb)

Currently rated 2.6 by 5 people

  • Currently 2.6/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | Design Patterns

Comments

Comments are closed
(c) 2008 Qsoft.be