so what is it all about. Well this solution is using Sitecore Type Mapping to resolve your custom types. So what we do is add a configuration patch to add the following section:
This type mapping "type" attribute will point to classes that implement the Interfaces we need to resolve using the IoC. The next step is to write resolver class that will be used to look at the configuration typeMappings and create the object based on the "Type" attribute in from the configuration
public class TypeResolver { public static T Resolve(string typeName, object[] parameters = null) { var xpath = "typeMappings/mapping[@name='" + typeName + "']"; return (T)(parameters == null ? Sitecore.Reflection.ReflectionUtil.CreateObjectFromConfig(xpath) : Sitecore.Reflection.ReflectionUtil.CreateObjectFromConfig(xpath, parameters)); } }
we can now use the class to resolve our interfaces based on the config. For instance on our controller, we will be able to do something like
public class OPageHistoryController : System.Web.Mvc.Controller { private IPageHistoryService pageHistoryService = TypeResolver.Resolve("PageHistoryService"); /// /// get the full item history information including the renderings datasources /// /// /// /// /// ///public ActionResult GetCurrentItem(string id, string database, string language, string version) { return Json(pageHistoryService.GetPageHistory(id, database, language, version), JsonRequestBehavior.AllowGet); } }
Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep sharing… Sitecore Online Training
ReplyDeleteThis is really great informative blog. Keep sharing.
ReplyDeleteSitecore Training
Sitecore Online Training
Sitecore Training in Hyderabad