In the previous post Simple Publication Resolver, I presented a simplistic way of putting generic pages into Publication context by identifying the Publication URL by just applying a regular expression to the URL path.
In this post, I present a proper Publication Resolver algorithm, for DD4T.net, that dynamically maps Publication IDs to Publication URLs and vice versa.
The interface of a Publication Resolver defines 2 methods: GetPublicationUrl and GetPublicationId, which provide retrieval functionality for mapped Publication ID and URL.
The implementing class, PublicationResolver, defines an algorithm that dynamically discovers the mapped ID for a given Publication URL and the other way around. It makes use of the Tridion Content Delivery API PublicationMetaFactory to retrieve the PublicationMeta object containing information about the given Publication:
The interface and class above can easily be put in Ninject as singleton mapping:
When using the Publication Resolver, we can easily retrieve Publication ID for a given URL or the other way around. The code below defines property PublicationResolver that is injected into the current class:
In this post, I present a proper Publication Resolver algorithm, for DD4T.net, that dynamically maps Publication IDs to Publication URLs and vice versa.
The interface of a Publication Resolver defines 2 methods: GetPublicationUrl and GetPublicationId, which provide retrieval functionality for mapped Publication ID and URL.
publicinterface IPublicationResolver
{
stringGetPublicationUrl(int publicationId);
intGetPublicationId(string publicationUrl);
}
The implementing class, PublicationResolver, defines an algorithm that dynamically discovers the mapped ID for a given Publication URL and the other way around. It makes use of the Tridion Content Delivery API PublicationMetaFactory to retrieve the PublicationMeta object containing information about the given Publication:
publicclassPublicationResolver : IPublicationResolver
{
privatereadonly IDictionary<string, int> mapUrlToId =
new Dictionary<string, int>();
privatereadonly IDictionary<int, string> mapIdToUrl =
new Dictionary<int, string>();
publicstringGetPublicationUrl(int publicationId)
{
if (mapIdToUrl.ContainsKey(publicationId))
{
return mapIdToUrl[publicationId];
}
else
{
PublicationMetaFactory factory = new PublicationMetaFactory();
PublicationMeta meta = factory.GetMeta(publicationId);
string url = meta == null ? string.Empty : meta.PublicationUrl;
mapIdToUrl[publicationId] = url;
mapUrlToId[url] = publicationId;
return url;
}
}
publicintGetPublicationId(string publicationUrl)
{
if (mapUrlToId.ContainsKey(publicationUrl))
{
return mapUrlToId[publicationUrl];
}
else
{
PublicationMetaFactory factory = new PublicationMetaFactory();
PublicationMeta meta = factory.GetMetaByPublicationUrl(publicationUrl).FirstOrDefault();
int id = meta == null ? 0 : meta.Id;
mapIdToUrl[id] = publicationUrl;
mapUrlToId[publicationUrl] = id;
return id;
}
}
}
The interface and class above can easily be put in Ninject as singleton mapping:
Bind<IPublicationResolver>().To<PublicationResolver>().InSingletonScope();
When using the Publication Resolver, we can easily retrieve Publication ID for a given URL or the other way around. The code below defines property PublicationResolver that is injected into the current class:
[Inject]
publicvirtual IPublicationResolver PublicationResolver { get; set; }
...
string url = PublicationResolver.GetPublicationUrl(44);
int id = PublicationResolver.GetPublicationId("/zh/cn/");