Personal Inertia

A collection of learnings and opinions.

Wednesday, December 1, 2010

Inversion of Control with Dependency Injection

Dependency Injection (DI) is a way of implementing the general Inversion of Control (IoC) -pattern. We do this to make our classes independent of each other. Combined with a preference for programming against interfaces instead of directly referencing the implementing classes we can achieve what is called loose coupling. The implementation details of one class have minimal impact on the innards classes using it. Additionaly using Dependency Injection makes your classes easier to unit-test, as they expose their dependencies externally and you can pass in mocks and stubs instead of full implementations.

It is easier to understand the pattern by looking at code-examples. We're creating code for a coffee-shop, and we're looking at the implemetation of a barista (the scrawny, wierd guy who makes the coffee). In my examples I will not show the interfaces, and just assume that all methods are defined in them.

Our barista has one method, where he uses his experience, a nice, big coffe-maker and some extras to concoct a potion:

   1:      public class Barista : IBarista {
   2:          private const int _yearsOfExperience = 5;
   3:          private IEducation _major = new LiberalArtsMajor();     
   4:   
   5:          public IBeverage MakeMeAn(IOrder order) {
   6:              var knowledge = new Experience(_yearsOfExperience, _major);
   7:              var plan = knowledge.HowDoIMake(order);
   8:   
   9:              var coffeeMaker = new BigAstra();
  10:   
  11:              var beverage = coffeeMaker.MakeBeverageAccordingTo(plan);
  12:   
  13:              var otherStuff = new AssortedCreamsAndSyrups();
  14:   
  15:              var additives = knowledge.WhatShouldIAddAccordingTo(plan);
  16:              beverage.Add(additives);
  17:   
  18:              return beverage;
  19:          }
  20:      }

This piece of code is pretty readable, so it's not the worst code in the world. However, it is tighlty coupled to several other classes. Firstly it binds to the LiberalArtsMajor -class (which is apt, but still not a good thing) to set its private field _major. Inside the method itself it binds to the classes Experience, BigAstra and AssortedCreamsAndSyrups. All of these object have to be created for the Barista to make a lovely, warm, black and brown elixir of wakefullnes. Thus these direct couplings make our Barista brittle.

Experience tells us that our classes should be as small and simple as possible, and that they should have one and only one reason to change. It is not far-fetched to imagine the BigAstra class acquiring constructor parameters requiring the caller to provide a source of water, a source of power and a repository of coffee-beans (as this fits neatly with what it would need in real-life).

   1:      public class BigAstra : ICoffeeMaker {
   2:          public BigAstra(IWaterSource water, IPowerSourceAt220Hz power,   ICoffeeRepository godlyBeans) {
   3:              // something very clever and difficult...
   4:          }
   5:          // the rest is left as an exercise for the reader
   6:      }

This change means that we will have to change the implementation of our barista because the implementation of the coffee-maker he is using changed. Furthermore, this change in how you make a BigAstra coffe-maker will cascade into all the places where the BigAstra is used. The same is true for all the other classes used by our Barista and in fact all the classes in the system. Good tools can certainly help us deal with this to a certain extent, but we quickly find ourselves in what I call combinatorial hell. Our barista has too many reasons to change - he is too tightly coupled to what he uses. How can we improve on this?

One solution to this problem I've seen used is to forgo object-orientation altogether and make the classes into a simple collection of static methods. By doing this we are binding ourselves tightly to one implementation, and refusing ourselves powerful object-oriented techniques like inheritance and polymorphism. In essence we are creating a Singleton implementation of the object we need. This technique transports us back into imperative programming in a fell swoop. Please, please do not do this! If you want to read more about why this is a bad idea I recomment Steve Yegge's "Singleton Considered Stupid" -post, and for a shorter view you can read Rasmussen's answer on StackOverflow. Seriously, kittens die and hamsters get palsy when we do this.

How can we solve our combinatorial problem without throwing 30 years of object-oriented goodness on the pyre? By reversing the control through dependency injection, that's how (how's that for a tagline?). This is not a new technique, in fact you've already seen it in this post. When our BigAstra changed its constructor to expose what it needed (its dependencies) it enabled Dependency Injection. There are several flavours of Dependency Injection, but common to them all is that they expose what the class depends on to the outside, allowing its behaviour to be modified without changing its innards. This is also known as the open / closed principle, which says the class should be open to extension but closed to modification. By exposing its dependencies BigAstra is also explicitly signalling that while it needs these things to function it does not accept responsibility for creating them (which follows from the single responsibility principle).

Some ways to acheive DI are to expose the dependencies using a Factory -pattern, expose them through properties / public fields (property injection), require them as constructor parameters (constructor injection) or we could use a Service Locator to build them up runtime (I'm sure there are other ways that slip my mind right now).

I strongly prefer using constructor injection, and also exposing any dependencies that have to change run-time to property injection. When I set up the object-graph I prefer to use a Service Locator which lets me declare how to construct objects when needed and call this when required. In the best of all possible worlds this should be exactly once in every application's lifetime.

We are going to change our barista to support DI by pulling all of his dependencies out into the class and exposing them through the constructor. The coffee-maker and the condiments may need to be replaced in the barista's lifetime (though baristas tend to seem quite frail, and cooffee-machines always look strong and shiny), so we will expose these to property injection as well as constructor injection.

   1:  public class Barista : IBarista {
   2:      private const int _yearsOfExperience = 5;
   3:      private IEducation _major;
   4:      private IExperience _attitude;
   5:      public ICoffeeMachine ShinyThing { get; set; } // Property Injection
   6:      public ICondiments Flourish { get; set; }      // Property Injection
   7:      
   8:      // Constructor Injection
   9:      public Barista(IEducation educatedAs, IExperience relevantExperience, 
  10:                     ICoffeeMachine toolOfTrade, ICondiments sugarAndSpice) {
  11:          _major = educatedAs;
  12:          _attitude = relevantExperience;
  13:          ShinyThing = toolOfTrade;
  14:          Flourish = sugarAndSpice;
  15:      }
  16:   
  17:      public IBeverage MakeMeAn(IOrder order) {
  18:          var plan = _attitude.HowDoIMake(order);
  19:          var beverage = ShinyThing.MakeBeverageAccordingTo(plan);
  20:          var additives = Flourish.WhatShouldIAddAccordingTo(plan);
  21:          beverage.Add(additives);
  22:          
  23:          return beverage;
  24:      }
  25:  }

It is now apparent what our barista depends upon by just looking at the constructor's parameters. Actually, there's something a bit off here. Is the barista really dependent upon his education? Isn't he merely using it as a part of his experience? When we explicitly expose our dependencies like this we'll often find such overlapping (and even conflicting) dependencies. This is a good opportunity for some cleanup, and we end up with a slightly cleaner barista as follows:

   1:  public class Barista : IBarista {
   2:      private IExperience _attitude;
   3:      public ICoffeeMachine ShinyThing { get; set; }
   4:      public ICondiments Flourish { get; set; }
   5:   
   6:      public Barista(IExperience questionableExperience, ICoffeeMachine toolOfTrade,
   7:                     ICondiments sugarAndSpice) {
   8:          _attitude = questionableExperience;
   9:          ShinyThing = toolOfTrade;
  10:          Flourish = sugarAndSpice;
  11:      }
  12:   
  13:      public IBeverage MakeMeAn(IOrder order) {
  14:          var plan = _attitude.HowDoIMake(order);
  15:          var beverage = ShinyThing.MakeBeverageAccordingTo(plan);
  16:          var additives = Flourish.WhatShouldIAddAccordingTo(plan);
  17:          beverage.Add(additives);
  18:   
  19:          return beverage;
  20:      }
  21:  }  

We are happy with this - our barista is ready to have his dependencies set on creation, two of them can even be changed run-time (this is not thread-safe). The barista has no knowledge of how to set up a coffee-machine, how to get the condiments or how to make the experience he got. He doesn't know or care about you or your band, he just makes killer Cortados.

Haven't we just moved our problem, though? Mustn't whosoever calls the barista now need to know how to set up all the barista's dependencies? In our case the barista is used by a poor, underpaid Cashier. She used to look like this:

   1:  public class Cashier : ICashier {
   2:      public IBeverage GetDrinkForCustomer(ICustomer currentCustomer){
   3:          var order = ConstructAnOrderFrom(currenCustomer.Demands);
   4:          
   5:          var jerk = new Barista();
   6:          var drink = jerk.MakeMeAn(order);
   7:          
   8:          return drink;
   9:      }
  10:  }

After we've meddled with the Barista's constructor to appease our false gods of object-orientation she now has to look like this abomination?

   1:  public class Cashier : ICashier {
   2:      private const int _yearsOfExperience = 5;
   3:      
   4:      public IBeverage GetDrinkForCustomer(ICustomer currentCustomer){
   5:          var order = ConstructAnOrderFrom(currenCustomer.Demands);
   6:          
   7:          var experience = new Experience(_yearsOfExperience, 
   8:                                          new LiberalArtsMajor());
   9:          
  10:          /probably has some municipality and plumber dependencies
  11:          var waterTap = new WaterTap(...);
  12:          //probably some municipality and electrician dependencies
  13:          var powerSource = new PowerSource(...); 
  14:          //probably depenent on suppliers and service getting container from stock
  15:          var coffeeBeans = new CoffeStore(...); 
  16:          var coffeMakerForJerk = new BigAstra(waterTap,
  17:                                  powerSource,
  18:                                  coffeeBeans);
  19:          
  20:          // dependent on suppliers I guess?
  21:          var condiments = new AssortedCreamsAndSyrups(...); 
  22:          
  23:          var jerk = new Barista(experience, 
  24:                                 coffeMakerForJerk,
  25:                                 condiments);
  26:          var drink = jerk.MakeMeAn(order);
  27:          
  28:          return drink;
  29:      }
  30:  }

Ouch! The answer to this is obviously to push the dependencies of our cashier into her constructor, making the next level up the chain responsible for the instantiation. But this seems like a reductio-ad-absurdum, as we'll have to have a god-class on top setting up our entire object-graph. That can't be good, can it? Well, yes and no. What we want is a Service Locator, or in our case an IoC container. I like StructureMap, as it's easy to work with and lets me define in a declarative way how it should work. It is one of the oldest of its kind in the .Net world, and time-proven.

To get StructureMap's help we have to tell it how to make the objects we are going to require. StructureMap has the concept of a Registry, which lets us declare what to do when asked for types in a declarative way, like "When asked for A you should return B". StructureMap is smart enough to inspect the constructors of the Bs it can return, and if the constructor requires something it knows how to make it puts that into it. This works well as long as you have defined all the types you need (the configuration is valid), and there are no circle-dependencies (ie. Foo requires a Bar, which requires a Foo). A registry for our coffee-shop might look like this:

   1:  public CoffeeShopRegistry : Registry {
   2:      //could be gotten from a config-file or somesuch
   3:      private const int _yearsOfExperience = 5; 
   4:      
   5:      public CoffeShopRegistry() {
   6:          ForRequestedType<ICoffeeShop>()
   7:              .TheDefaultIsConcreteType<CoffeShop>();
   8:          ForRequestedType<ICashier>()
   9:              .TheDefaultIsConcreteType<Cashier>();
  10:          ForRequestedType<IBarista>()
  11:              .TheDefaultIsConcreteType<Barista>();
  12:          
  13:          ForRequestedType<IEducation>()
  14:              .TheDefaultIsConcreteType<LiberalArtsMajor>();
  15:          ForRequestedType<ICoffeeMaker>()
  16:              .TheDefaultIsConcreteType<BigAstra>();
  17:          ForRequestedType<IWaterSource>()
  18:              .TheDefaultIsConcreteType<WaterTap>();
  19:          ForRequestedType<IPowerSourceAt220Hz>()
  20:              .TheDefaultIsConcreteType<MainsLine>();
  21:          ForRequestedType<ICoffeeRepository>()
  22:              .TheDefaultIsConcreteType<SomeCoffeStorage>();
  23:          
  24:          ForRequestedType<IExperience>()
  25:              .TheDefault.Is
  26:              .ConstructedBy(context => 
  27:                  new Experience(_yearsOfExperience, 
  28:                                 context.GetInstance<IEducation>()));
  29:      }
  30:  }

StructureMap won't initialize any objects until asked to, so defining the types our barista depend on after the barista itself is not a problem as long as the internal graph of StructureMap contains all it needs when it is called upon to provide an IBarista. StructureMap can also do other, cool things - like managing the lifetime of the objects, but that's a subject for another post.

We want to kick off StructureMap as one of the very first things in our application, and preferrably we'd only like to call upon it to construct something once (or as few times as possible). A possible Main would be:

   1:  public CoffeShopProgram {
   2:      public static void Main(string[] args) {
   3:          ObjectFactory.Initialize(context => 
   4:              context.AddRegistry(new CoffeeShopRegistry()));
   5:          
   6:          // Exception if we've missed anything
   7:          ObjectFactory.AssertConfigurationIsValid();
   8:          
   9:          // depends on having a cashier and a barista - MAGIC!
  10:          var shop = ObjectFactory.GetInstance<ICoffeShop>(); 
  11:          
  12:          shop.Open();
  13:          while (shop.IsOpen) {
  14:              var customer = ObjectFactory.GetInstance<ICustomer>();
  15:              shop.ServeCustomer(customer);
  16:          }
  17:      }
  18:  }

We're setting up ObjectFactory by adding the registry we made, in effect telling it what types it should know about, and how to return something when called upon. We haven't seen any ICoffeeShop implementors, but let's just say that is a class depending on an ICashier for its constructor. StructureMap knows how to make an ICashier - that's an instance of the Cashier -class. But Cashier depends on an IBarista! No problem, StructureMap knows that's a Barista instance, etc.

Implementing Dependency Injection loosens our classes' coupling against other classes, in effect delegating that to somewhere else. The actual calling of constructors is done by our IoC -container, freeing us up to simply declare that we want an IBarista, and assuming one will be created for us. So, instead of needing to know the intimate details of the implementing classes' constructors and dependencies to use an IBarista we just have to ask for one.

And, that's all kinds of cool!

Thursday, May 20, 2010

Getting a WeekNumber from a Date in SSRS 2005 and 2008

Many reports have to deal with week numbers. Often you'll have a DateTime to work with, either from a dataset or a parameter.

So, how do we convert this lovely little DateTime to a WeekNumber? Search for that online and you'll get some solutions suggesting you change your query to give you a weeknumber, some solutions will do some fancy (and mostly erroneous) math to calculate the WeekNumber and yet others suggest you just don't do that.

Getting the WeekNumber from your DataSet really is what you want, but then again you don't really want your DataSource to calculate the WeekNumber in all instances. The reason for that is related to the reason why DateTime does not have a WeekNumber:

What, really, is a weeknumber? Why, silly, it's the number of the week the day is a part of.

That's true, but how are the weeks numbered? Do they start with week 0 or week 1? If the week numbering starts at week 1 the last week should be week 53, or should it be week 52 (a week with more than 7 days)? How about if the year starts on a non-monday, what week are the first days in (they could be in the last week of the previous year, in the first (0th or 1st) of the new year, or perhaps even week-less).

How about weeks, do they start on Mondays, Sundays, Saturdays, Fridays or some other day? That's kind of important when trying to calculate the first week of the year and counting the weeks until a date.

Calendaring issues get complicated very fast, and the fun thing about them is that there are so many ways people keep track of their measured days. The year could start at wildly differing dates, it's silly to assume that the first of January is the first day of a year, and if it's not you'd better not be DateDiffing that modulus 7 to get a WeekNumber.

I'll just happily ignore systems with weeks of more or less than 7 days. That would get hard.

With all this in mind also consider that your datasource may not be using the same calendaring as your reporting system. If your report may be run in Tehran and your DataSource is a database of some sort in Tel-Aviv they may not be using the same calendar, but it'd be an admirable instance of Persian/Semittic cooperation.

To get your week number you really should defer all the international issues to the people who've researched it and been paid to do a good job of it. Luckilly the .Net framework is produced by just such people, and I just love it when I can let it do the work for me.

The .Net framework (in which you report is running) knows about the locale it's running in, and this information can be used to derive the WeekNumber without ever adding together two days.

You just add this code snippet to your Code tab in your report properties:

Function getWeekNumber(ByVal d As DateTime) As Integer
    dim culture as System.Globalization.CultureInfo
    culture = system.Globalization.CultureInfo.CurrentCulture
 
    dim weekRule as System.Globalization.CalendarWeekRule
    weekRule = culture.DateTimeFormat.CalendarWeekRule
 
    dim firstDayOfWeek as System.DayOfWeek
    firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek
 
    return culture.Calendar.GetWeekOfYear(d, weekRule, firstDayOfWeek)
End Function

Explanation:
First we get the current culture the report is running under (this would be the culture of the locale of the server running SSRS in our case). From this culture you extract the rule for starting weeks relative to the year and which day of the week the weeks start on. These two values, together with the date you have are passed into the Calendar of the current culture to get the WeekNumber of the date in that context.

It really is harder than it should be, but with this code (which you can just copy/paste) you make a new Calculated Field on your dataset with the following expression (I assume the Date field you want to convert to a WeekNumber is called Date in this instance):

=Code.getWeekNumber(Fields!Date.Value)

And, as they say, Robert is your mother's brother.

Addendum:

If you don't want to go through adding the WeekNumber to your DataSet you could of course just use the above expression straight in your textboxen or the like.

If you don't want the code you can inline it all as such (this is UGLY, don't do it!):
=System.globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(Fields!Date.Value, System.globalization.CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, System.globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)

Monday, October 5, 2009

Simpler reference equality on mocks

When mocking with MoQ I find myself regularly setting up the .Equals() method on the mocks. It just makes for more readable code to be able to ask a collection whether it .Contains() the mock than writing out the iteration myself.

But it does bloat my test-cases somewhat. Here's an example setting up a mock of a class with the equality set that returns true if it's compared to itself:
[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 mocker.Setup(x => x.Equals(mocker.Object)).Returns(true);

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

That's fine if it's only in one test, but I don't like to repeat that all over my code. My first thought was to make a convenience method which set up the equality for me:
public static Mock<T> ReferenceEquals<T>(Mock<T> mock) where T : class {
 mock.Setup(m => m.Equals(mock.Object)).Returns(true);
 return mock;
}

Sticking this in some public place now allows me to set up my test from before like this:

[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 ReferenceEquals(mocker);

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

That's certainly better. But, we can do better than that, can't we? Let's make it an extension-method, for great success! To use this code the class containing the extension-method must be static.

public static void HasReferenceEquality<T>(this Mock<T> mock) where T : class {
 mock.Setup(m => m.Equals(mock.Object)).Returns(true);
}

And with that our test has been transferred to the (in my opinion) much more readable:
[Test(Description = "Mock of equals on an instance succeeds")]
public void MockingEquals_SameInstance_AreEqual() {
 var mocker = new Mock<SomeClass>();
 mocker.HasReferenceEquality();

 var mockedObject = mocker.Object;

 Assert.That(mockedObject.Equals(mockedObject));
}

Great success!

Friday, September 25, 2009

MoQ with Collections, or equality woes

Still working with the fine mocking-framework MoQ, I came across a somewhat surprising facet (and I don't like surprises).
I'm mocking out a class which should be added to a collection after a method-call, like so:

public class SomeClass{

}

public class Container {
private List<SomeClass>; classes = new List<SomeClass>();

public IEnumerable<SomeClass> Classes {
get {
return classes;
}
}

public void addSomeClass(SomeClass instance) {
classes.Add(instance);
}
}

[Test]
public void ContainerContainsAddedClassAfterAdd() {
var mockSomeClass = new Mock<SomeClass>();
mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

var Container = new Container();
Container.addSomeClass(mockSomeClass.Object);

Assert(Container.Classes.Contains(mockSomeClass.Object));
}

This works well, the mock is added to the Container's collection and the setup of the Equals method on the mock makes sure the IEnumerable.Contains() return true.
However there's always some complication. The class I'm really mocking out is not as simple as our SomeClass. It's something like this:

public class SomeClassOverridingEquals{
public virtual Equals(SomeClassOverridingEquals other) {
return false;
}

public override Equals(object obj) {
var other = obj as SomeClassOverridingEquals;

if (other != null) return Equals(other); // calls the override
return false;
}
}

[Test]
public void ContainerContainsAddedClassOverridingEqualsAfterAdd() {
var mockSomeClass = new Mock<SomeClassOverridingEquals>();
mockSomeClass.Setup(c => c.Equals(mockSomeClass.Object)).Return(true);

var Container = new Container();
Container.addSomeClass(mockSomeClass.Object);

Assert(Container.Classes.Contains(mockSomeClass.Object)); // fails
}

The class contains an override for the Equals method for its own specific type, and the Setup method for the mock does not seem to be able to mock out that specific method (only overriding the more general Equals(object)). Thus the test fails.

I have so far found no way of working around this quite common pattern, other than rewriting the class not to use the overriding equals.

I don't like that.

Anyone have any ideas?

Thursday, September 24, 2009

Mocking expectations on Property setters with MoQ

When mocking out your classes using the fabulous MoQ mocking-framework you may sometimes need to mock out an expectation that a property on your mocked object should be set.

It took me some time to figure out how to do this. The documentation on the MoQ quickstart wiki says to use the following pattern:

// expects an invocation to set the value to "foo"
mock.SetupSet(foo => foo.Name = "foo");


Well, I tried this with an object:

public class SomeClass {
public string Something { get; set; }
}

[Test]
public void PropertySetupOnMock() {
const string someString = "Should be set";
var mocker = new Mock<SomeClass>();

mocker.SetupSet(x => x.Something = someString); //exception here
mocker.Object.Something = someString;
mocker.VerifySet(x => x.Something);
}

This only gave me an error on the line with the SetupSet:
System.ArgumentException: Invalid expectation on a non-overridable member.

Well, that's strange. After some looking around it turns out that MoQ does its magic by extending the class that's being mocked. It cannot extend non-virtual properties, so this Something property cannot be extended and thus cannot be Setup.
What can be done about this? The easiest immediate solution would be to mark the property virtual, like so:

public class SomeClass {
public virtual string Something { get; set; }
}

But do we really want to decorate every property on the objects as virtual? Not really. A better solution is to push the property up to an interface and mock that interface instead, like so:

public interface IWithProperty {
public string Something { get; set; }
}

public class SomeClass : IWithProperty {
public string Something { get; set; }
}

[Test]
public void PropertySetupOnInterfaceMock() {
const string someString = "Should be set";
var mocker = new Mock<IWithProperty>();

mocker.SetupSet(x => x.Something = someString); //no exception here
mocker.Object.Something = someString;
mocker.VerifySet(x => x.Something);
}

Thus allowing me to mock it out and test that the property setter was indeed called. Yay!
If anyone has any better ways of doing this I'd love to hear from you!

Tuesday, February 10, 2009

Listing the properties of an object

Sometimes you just want to print out the state of some reasonably rich object (for logging or GUI mock-up or whatever). This code gives you a PropertyGetter class with two easy-to-use static methods. listProperties returns a name: value string listing of the properties on the input object, for easy logging.

If you want the object's properties as a list of properties you can get that as well.

NB: This simple class does not recurse through complex objects, so you will only ever get the top-level of a property.

Code:

using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Code_Examples {
class PropertyGetter {
public static string listProperties(object someObject) {
StringBuilder sb = new StringBuilder();

List<pair> props = getProperties(someObject);

foreach (Pair prop in props) {
sb.AppendLine(prop.Name + ": " + prop.Value);
}
return sb.ToString();
}

public static List<pair> getProperties(object someObject) {
List<pair> dic = new List<pair>();

PropertyInfo[] props = someObject.GetType().GetProperties();
foreach (PropertyInfo prop in props) {
Pair t = new Pair {
Name = prop.Name,
Value = prop.GetValue(someObject, null)
};
dic.Add(t);
}
return dic;
}
}

public class Pair {
public string Name {
get;
set;
}
public object Value {
get;
set;
}
}
}

Monday, January 26, 2009

Logging snippets ready to go

My last post was some simple intro to how you can make your own snippets - here are some easy ones I made to make logging with log4net easier.

This first one is one I use simply to get a logger with the name of the current class. I put this in most every class. Nothing fancy here, just type log and press tab to get a logger named log.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Logging
</Title>
<Author>Tomas Ekeli</Author>
<Description>Initializes a logger with the name of the current class</Description>
<HelpUrl />
<SnippetTypes />
<Keywords />
<Shortcut>log</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net.dll</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations />
<Code Language="CSharp" Kind="" Delimiter="$"><![CDATA[protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); ]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Now you've got a logger up and running you'll be doing a lot of calls to log.debug("somestring") and even more with formatting on that string. So here's a snippet for that:


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


Finally a very nice thing about logging is when you can push the context on a stack for inclusion. I use this all the time. This snippet can be used as a surrounding snippet as well (mark the text you want to surround in a context and right-click -> Surround-with


<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>logFormattedString</Title>
<Author>Tomas Ekeli</Author>
<Description>log formatted string</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>lsf</Shortcut>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>log4net</Assembly>
<Url />
</Reference>
</References>
<Imports>
<Import>
<Namespace>log4net</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>Level</ID>
<Type />
<ToolTip>Log level</ToolTip>
<Default>Debug</Default>
<Function />
</Literal>
<Literal Editable="true">
<ID>Message</ID>
<Type />
<ToolTip />
<Default>field value: {0}</Default>
<Function />
</Literal>
<div class="youtube-video"><object Editable="true">
<ID>Field</ID>
<Type>Object</Type>
<ToolTip>The field to log</ToolTip>
<Default></Default>
<Function />
</object></div>
</Declarations>
<Code Language="csharp" Kind="method body" Delimiter="$"><![CDATA[log.$Level$(string.Format("$Message$", $Field$));]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>