Skip to main content

Posts

Showing posts from 2013

Web API Routing

The diagram below tries do describe the decision process behind selecting the method of the controller that will get executed. I've added a little extra documentation through the annotations on the diagram. 1 Action Based If the route defines the action parameter then the method selection will be based on the value of that parameter. The matching will be based on the method name or the method alias (A method in the controller may be aliased using the ActionName attribute). All the matching methods are then filtered by verb. The filtering is done by attributes applied to the method (such as HttpGet). Only the methods that match the verb of the incoming request will be returned. Methods with no attributes will also be considered valid as they don’t have any attributes to allow filtration. 2 Verb Based When the method selection is verb based we need to get the Http verb used in the request. The controller methods are then filtered in two ways: Get all the methods a

Covariance and Contravariance in C#

Today: what do we mean by “covariance” and “contravariance”? The first thing to understand is that for any two types T and U, exactly one of the following statements is true: T is bigger than U. T is smaller than U. T is equal to U. T is not related to U. For example, consider a type hierarchy consisting of Animal, Mammal, Reptile, Giraffe, Tiger, Snake and Turtle, with the obvious relationships. ( Mammal is a subclass of Animal, etc.) Mammal is a bigger type than Giraffe and smaller thanAnimal, and obviously equal to Mammal. But Mammal is neither bigger than, smaller than, nor equal to Reptile, it’s just different. Why is this relevant? Suppose you have a variable, that is, a storage location. Storage locations in C# all have a type associated with them.  At runtime you can store an object which is an instance of an equal or smaller type in that storage location.  That is, a variable of type Mammal can have an instance of Giraffe stored in it, but no