Tuesday, September 15, 2009

Shortcut to TypeInfo

There are two ways how to get the corresponding TypeInfo instance for a persistent Type.

First way is straightforward:

TypeInfo typeInfo = Domain.Model.Types[typeof (Animal)];

The second is much shorter, it is powered by .Net Framework 3.5 extension methods feature:

TypeInfo typeInfo  = typeof (Animal).GetTypeInfo();

or

TypeInfo typeInfo = typeof (Animal).GetTypeInfo(Domain);

2 comments:

  1. What is the difference between the parameterless method and the one that accepts a domain?

    How could you return type info without a domain?

    ReplyDelete
  2. Hello Ara,

    The main difference is that in parameterless method DO 4 must look for the Domain instance to resolve Type by itself. It does so by calling Domain.Demand() which in turn falls back to Session.Demand() call.

    So if there is not current active Session instance, parameterless method wouldn't work. That is why the overloaded one is introduced.

    ReplyDelete