14 November, 2014

Dynamic Publication Resolving in DD4T

DD4T is a great framework which would give you everything in place what you basically needed. Here we will see how we could resolve the publication based on the URL,  when we work with multiple publications accessing same web site, our DD4T would need to query dynamically to different publications.

Many times it can be based on URL, based on the domain or language we could resolve the publication. It’s obviously up to us how we implement it.

By default the DD4T framework queries the publication that we have mapped in the key: <add key="DD4T.PublicationId" value="7" /> or keep it 0 to query all publications

We can make it dynamic simply by implementing the IPublicationResolver class

Step 1: To resolve publication dynamically, we can write a class that implement DD4T.ContentModel.Contracts.Resolvers.IPublicationResolver


namespace Indivirtual.DD4T.Mvc.Resolver
{
    public class SitePublicationResolver : IPublicationResolver
    {
        public int ResolvePublicationId()
        {
            switch (HttpContext.Current.Request.Url.Host)
            {
                case "daimler.com": return 5;
                case "daimler.ae": return 7;
                default: return 5;
            }
        }
    }
}

It can be programmed either with domain or url, like daimer.com/en or daimer.com/ar, based on the scenario
Step 2: Add the resolver in the PageFactory in Ninject dependency


            Bind<IPublicationResolver>().To<SitePublicationResolver>();
            Bind<ICacheAgent>().To<DefaultCacheAgent>();
            Bind<IPageFactory>().ToMethod(context => new PageFactory()
            {
                PageProvider = context.Kernel.Get<IPageProvider>(),
                ComponentFactory = context.Kernel.Get<IComponentFactory>(),
                LinkFactory = context.Kernel.Get<ILinkFactory>(),
                PublicationResolver = context.Kernel.Get<IPublicationResolver>()
            });

That’s all you have your resolver, Rest all like PageController class will remain the same. After implementing IPublicationResolver, any request to PageFactory first try to resolve the publication and then queries the broker.

0 comments:

Post a Comment