Can some help me understand why the class is inheriting from a base of same type?
It's not, it's inheriting from Page<T>
, but T
itself is constrained to be parameterized by a type that is derived from BasePage<T>
.
To infer why, you have to look at how the type parameter T
is actually used.
After some digging, as you move up the inheritance chain, you'll come up upon this class:
(github)
public class GenericPage<T> : PageBase where T : GenericPage<T>
{
public bool IsStartPage {
get { return !ParentId.HasValue && SortOrder == 0; }
}
public GenericPage() : base() { }
public static T Create(IApi api, string typeId = null)
{
return api.Pages.Create<T>(typeId);
}
}
As far as I can see, the only purpose for the generic constraint is to make sure that the Create
method returns the least abstract type possible.
Not sure if it's worth it, though, but perhaps there's some good reason behind it, or it could be only for convenience, or maybe there's no too much substance behind it and it's just an overly elaborate way to avoid a cast (BTW, I'm not implying that is the case here, I'm just saying that people do that sometimes).
Note that this doesn't allow them to avoid reflection - the api.Pages
is a repository of pages that obtains typeof(T).Name
, and passes it as the typeId
to the contentService.Create
method (see here).