2016-11-29 74 views
0

我有一點困惑在這裏,在這行代碼一把umbraco獲得價值從內容

var cs = ApplicationContext.Current.Services.ContentService.GetById(1000); 
cs.GetValue("test"); 

var nd = new Node(1000); 
nd.GetProperty("test"); 

兩個代碼可以使用。什麼是兩個代碼之間的不同。當和爲什麼我們使用其中任意一個

回答

2

一把umbraco服務
在一把umbraco 6中引入的新API一把umbraco的服務層包括contentService的,一個MediaService,一個DataTypeService和LocalizationService。請查看umbraco documentation瞭解有關這些服務和其他umbraco服務的文檔。

umbraco中的服務遇到了數據庫,並沒有利用umbraco提供的所有緩存。你應該謹慎使用這些服務。如果您試圖以編程方式從數據庫中添加/更新/刪除數據,或者您嘗試從數據庫中獲取未發佈的內容,則應使用這些服務。如果您只需要查詢已發佈的內容,則應該使用UmbracoHelper,因爲它速度更快。

var cs = ApplicationContext.Current.Services.ContentService.GetById(1000); 
cs.GetValue("test"); 

UmbracoHelper
的UmbracoHelper是當你想查詢從一把umbraco內容,你應該總是使用什麼。它不會觸及數據庫,並且比umbraco服務快得多。

var node = Umbraco.TypedContent(1000); 
var nodeVal = node.GetPropertyValue<string>("test"); 

如果你發現你沒有訪問UmbracoHelper,你可以讓你自己的,只要你有一個UmbracoContext:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); 
var node = Umbraco.TypedContent(1000); 
var nodeVal = node.GetPropertyValue<string>("test"); 

NodeFactory
的NodeFactory已經過時了。如果您使用Umbraco 6或更高版本,我強烈建議轉換爲UmbracoHelper。

var nd = new Node(1000); 
nd.GetProperty("test"); 
2

在剃刀或前端代碼,始終使用UmbracoHelper

var node = Umbraco.TypedContent(1000); 
var value = node.GetPropertyValue<string>("test"); 

這將查詢發佈爲緩存節點

你想使用電話contentService的查詢數據庫,例如,如果您想了解有關未發表的節點的信息(你不想這樣做在你的看法)

與Node對象查詢可能是傳統(我從來沒有用過它)