views:

72

answers:

3

I saw in a projekt many interfaces which just wrapped a generic class.

An example:

public interface IAssertionViewModelMapper : IMapper<Assertion, AssertionViewModel>
{}

public interface ISearchPageViewModelMapper : IMapper<IList<Tag>, SearchPageViewModel>
{}

Could someone explain why they did this ? I mean I can just straight implement the IMapper interface.

+2  A: 

Sometimes teams decide they don't want to rely on built-in implementations that are subject to change and get deprecated. So they wrap everything, classes, interfaces, types and use them in their project. They're hoping that one day when support for some framework components is dropped they will simply adapt their wrappers without having the complete software rewritten.

It is also the case where I work. Absolutely everything got wrapped some ten years ago and we only use our custom wrappers. As you may guess, the time has shown that most of it was never needed. No time was really saved when something got deprecated along the lines. We still had to adapt some code which took lots of time, but wrappers didn't help in any way. Rather the opposite, wrappers are constant obstacles since they didn't wrap things completely back then, so we have to extend wrappers now and then.

Developer Art
+1  A: 

Doing this creates a new interface type. So if you declare a reference:

IAssertionViewModelMapper avmm = null;

You cannot assign an object to that if it implements IMapper<Assertion, AssertionViewModel>. They are different types, and only have assignment compatibility in one direction: derived can be assigned to base.

So if someone tries this trick in order to abbreviate their code, like using a typedef in C, then they're possibly storing up some confusion in the future.

Daniel Earwicker
+2  A: 

Microsoft Code Analysis (a.k.a. FxCop) contains a rule against doing this. However, creating a new interface like this has a few advantages:

  1. The new name can describe the intent of the interface better (however, aliasing in C# can do the same).
  2. This saves you a few minutes when new methods are needed on that interface, because you already defined it.
  3. It can make your code base more consistent when most interfaces that derive from IMapper<T, TViewModel> actually define new methods. It's easy to spot the interfaces, even if they don't define new members.
Steven