An initialization pattern to avoid problems calling virtual method in constructor

You are not supposed to call virtual methods in a constructor. The reason being that at the point you call a virtual method in the base class, the derived class has not been initialized. For example, class Base { public Base(int input) { VirtualMethod(); } public virtual void VirtualMethod() { } } class Derived :…

5

To mock or not to mock?

In Wikipedia’s definition of Unit test, it says “Ideally, each test case is independent from the others; mock or fake objects as well as test harnesses can be used to assist testing a module in isolation.” If we follow this definition strictly, whenever the class under test collaborates with other classes, we need to mock…

2

Abstract Class or Interface II

In previous post, I was convinced that abstract class was the right choice for the work I am doing. However, when I got deeper into the work, I had to switch to use interface simply because abstract class does not support multiple inheritance. I am creating a hierarchy of pure abstract classes to be accessed…

1

Abstract Class or Interface I

Recently I’m working on designing a set of .Net APIs for external clients to use. One of the decisions I had to make is whether I start with abstract class or interface. For example, should it be: public abstract class Foo { public abstract void Bar(); }Or should it be: public interface IFoo { void…

2