In EPiServer CMS7 < X
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
// We must have a content REF.
var contentReference = new ContentReference(5);
// Get page (or block)
return repository.Get<TheContentType>(contentReference);
And if you want to load it as a given type but do not want an exception to be thrown in case type is not correct you could load it as:
TextPage page = Locate.ContentRepository().Get<IContent>(pageLink) as TheContentType;
// With a pagetype called Startpage we could get it like this:
var startPage = repository.Get<StartPage>(PageReference.StartPage);
https://world.episerver.com/blogs/Johan-Bjornfot/Dates1/2015/2/changes-regarding-language-handling-during-content-loading-in-cms8/
DataFactory in EPiServer 7
In Episerver 5,6 DataFactory was used quite extensively now with ASP.NET MVC unit testing becomes a lot more feasible in practice, and so making your code testable makes more sense.So, although DataFactory is still available in EPiServer 7 just as it was in EPiServer 6 and earlier versions, the preferred approach nowadays is to make use of the dependency resolver ServiceLocator class to get an instance of IContentRepository.
DataFactory in fact implements the IContentRepository interface, so you will likely be using it implicitly quite a lot regardless.
Good link to read: http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/8/EPiServer7-Working-with-IContentRepositoryDataFactory/
No comments:
Post a Comment