Code snippet update : using dependency container

by sven 28 November 2008 21:39

The new version of my snippet will not directly instantiate the dependency via the constructor anymore, but get an instance from a DI-container. The main advantage is that the constructors don't have to be littered changed to add a new external dependency to a class. Just type 'exdep', enter the appropriate name, et voila, the dependency can be used in the current class (Provided ofcourse that the DI container is correctly initialized)

To repeat the previous example, when you type 'exdep' the snippet will expand to :

image

Typing the correct type name will yield the code :

   1: private IMyDataAccessLayerClass _MyDataAccessLayerClass;
   2: private IMyDataAccessLayerClass MyDataAccessLayerClass
   3: {
   4:     get
   5:     {
   6:         if (_MyDataAccessLayerClass == null)
   7:             _MyDataAccessLayerClass = ServiceLocator.Instance().Resolve<IMyDataAccessLayerClass>();
   8:         return _MyDataAccessLayerClass;
   9:     }
  10: }

 

Just a small change, but it can boost productivity significantly. For the record, we used Unity as our container.

exdep.snippet (1,13 kb)

Currently rated 2.8 by 5 people

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

Tags:

C# | Design Patterns

Comments

Comments are closed
(c) 2008 Qsoft.be