使用文字範本中的 Visual Studio ModelBus
發行︰ 2016年7月
如需 Visual Studio 2017 的最新文件請參閱 Visual Studio 2017 文件。
如果您撰寫文字範本,以讀取模型,包含Visual StudioModelBus 參考,您可能想要解析的參考,以存取目標模型。 在此情況下,您必須調整文字範本和參考的定義域專屬語言 (Dsl):
參考目標 DSL 必須設定為從文字範本存取的 ModelBus 配接器。 您也會從其他程式碼存取 DSL,重新設定配接器需要除了標準的 ModelBus 配接器。
配接器管理員必須繼承自VsTextTemplatingModelingAdapterManager而且必須具有屬性
[HostSpecific(HostName)]
。
注意
如果您想要讀取不包含 ModelBus 參考的 DSL 模型,您可以使用指示詞處理器所產生的 DSL 專案。 如需詳細資訊,請參閱文字範本存取模型。
如需文字範本的詳細資訊,請參閱設計階段使用 T4 文字範本產生程式碼。
建立模型匯流排配接器從文字範本存取
若要解決 ModelBus 參考文字範本中的,目標 DSL 必須具有相容的配接器。 從不同的 AppDomain 中執行的文字範本Visual Studio文件編輯器,並因此配接器已載入而不是存取 DTE 透過模型。
若要建立文字範本與相容的 ModelBus 配接器
如果目標 DSL 方案並沒有ModelBusAdapter專案中,使用 Modelbus 擴充功能精靈來建立︰
下載並安裝Visual StudioModelBus 擴充功能,如果您有未執行此作業。 如需詳細資訊,請參閱Visualization and Modeling SDK。
開啟 DSL 定義檔。 以滑鼠右鍵按一下設計介面,然後按一下 啟用 Modelbus。
在對話方塊中,選取我想要這個 DSL 公開給 ModelBus。 如果您想要這個 DSL 公開給其模型並使用其他 Dsl 的參考,您可以選取這兩個選項。
按一下 [確定]。 新專案 "ModelBusAdapter" 會隨即加入至 DSL 方案。
按一下 轉換所有範本。
重建方案。
如果您想要存取 DSL,從文字範本和其他程式碼,例如命令,從複製ModelBusAdapter專案︰
在 Windows 檔案總管中,複製並貼上的資料夾,其中包含ModelBusAdapter.csproj。
重新命名專案檔 (例如,若要T4ModelBusAdapter.csproj)。
在方案總管] 中,請以滑鼠右鍵按一下方案節點,指向新增,然後按一下 [現有專案。 找出新的配接器專案, T4ModelBusAdapter.csproj。
在每個
*.tt
檔案的新專案中,變更命名空間。以滑鼠右鍵按一下 方案總管 中新的專案,然後按一下 屬性。 在屬性編輯器中,變更所產生的組件和預設命名空間的名稱。
在 DslPackage 專案中加入新的配接器專案的參考,使其具有兩種配接器的參考。
在 DslPackage\source.extension.tt,加入這一行會參考新的配接器專案。
<MefComponent>|T4ModelBusAdapter|</MefComponent>
轉換所有範本並重建方案。 不發生任何建置錯誤。
在新的配接器專案中,加入下列組件的參考︰
Microsoft.VisualStudio.TextTemplating.11.0
Microsoft.VisualStudio.TextTemplating.Modeling.11.0
在 AdapterManager.tt:
變更 AdapterManagerBase 的宣告,使它繼承自VsTextTemplatingModelingAdapterManager。
public partial class <#= dslName =>AdapterManagerBase :
Microsoft.VisualStudio.TextTemplating.Modeling.VsTextTemplatingModelingAdapterManager { ...
檔案結尾附近取代之前 AdapterManager 類別 HostSpecific 屬性。 移除以下一行︰
[DslIntegration::HostSpecific(DslIntegrationShell::VsModelingAdapterManager.HostName)]
插入下列這一行︰
[Microsoft.VisualStudio.Modeling.Integration.HostSpecific(HostName)]
這個屬性來篩選當配接器會搜尋 modelbus 取用者時所提供的介面卡集合。
轉換所有範本並重建方案。 不發生任何建置錯誤。
撰寫文字範本可以解析 ModelBus 參考
一般而言,您會開始讀取,並產生檔案從 「 來源 」 DSL 的範本。 這個範本會使用在來源 DSL 專案中讀取來源模型檔案中所述的方式會產生指示詞文字範本存取模型。 不過,來源 DSL 包含 「 目標 」 DSL 的 ModelBus 參考。 因此,您想讓解析的參考,以及存取目標 DSL 範本程式碼。 您因此必須採用範本,依照下列步驟︰
變更基底類別的範本以ModelBusEnabledTextTransformation。
包含
hostspecific="true"
範本指示詞中。目標 DSL 和其介面卡,以及啟用 ModelBus 加入組件參考。
您不需要產生目標 DSL 的一部分的指示詞。
<#@ template debug="true" hostspecific="true" language="C#"
inherits="Microsoft.VisualStudio.TextTemplating.Modeling.ModelBusEnabledTextTransformation" #>
<#@ SourceDsl processor="SourceDslDirectiveProcessor" requires="fileName='Sample.source'" #>
<#@ output extension=".txt" #>
<#@ assembly name = "Microsoft.VisualStudio.Modeling.Sdk.Integration.11.0" #>
<#@ assembly name = "Company.TargetDsl.Dsl.dll" #>
<#@ assembly name = "Company.TargetDsl.T4ModelBusAdapter.dll" #>
<#@ assembly name = "System.Core" #>
<#@ import namespace="Microsoft.VisualStudio.Modeling.Integration" #>
<#@ import namespace="Company.TargetDsl" #>
<#@ import namespace="Company.TargetDsl.T4ModelBusAdapters" #>
<#@ import namespace="System.Linq" #>
<#
SourceModelRoot source = this.ModelRoot; // Usual access to source model.
// In the source DSL Definition, the root element has a model reference:
using (TargetAdapter adapter = this.ModelBus.CreateAdapter(source.ModelReference) as TargetAdapter)
{if (adapter != null)
{
// Get the root of the target model:
TargetRoot target = adapter.ModelRoot;
// The source DSL Definition has a class "SourceElement" embedded under the root.
// (Let’s assume they’re all in the same model file):
foreach (SourceElement sourceElement in source.Elements)
{
// In the source DSL Definition, each SourceElement has a MBR property:
ModelBusReference elementReference = sourceElement.ReferenceToTarget;
// Resolve the target model element:
TargetElement element = adapter.ResolveElementReference<TargetElement>(elementReference);
#>
The source <#= sourceElement.Name #> is linked to: <#= element.Name #> in target model: <#= target.Name #>.
<#
}
}}
// Other useful code: this.Host.ResolvePath(filename) gets an absolute filename
// from a path that is relative to the text template.
#>
執行這個文字範本時,SourceDsl
指示詞會載入檔案Sample.source
。 範本可以存取該模型中,從開始的項目this.ModelRoot
。 程式碼可以使用的網域類別和屬性的 DSL。
此外,範本可以解析 ModelBus 參考。 參考指向之目標模型時,其中的組件指示詞可讓使用的網域類別和屬性,該模型 DSL 的程式碼。
如果您未使用的指示詞所產生的 DSL 專案,您也應該包含下列。
<#@ assembly name = "Microsoft.VisualStudio.Modeling.Sdk.11.0" #> <#@ assembly name = "Microsoft.VisualStudio.TextTemplating.Modeling.11.0" #>
使用
this.ModelBus
ModelBus 的存取。
逐步解說︰ 測試使用 ModelBus 的文字範本
在此逐步解說中,您可以依照下列步驟︰
建立兩個 Dsl。 一個 DSL消費者,具有
ModelBusReference
DSL,可以參考的屬性提供者。提供者中建立兩個的 ModelBus 配接器︰ 一個用於文字範本存取另一個則用於一般程式碼。
在單一的實驗性專案中建立 Dsl 執行個體的模型。
設定網域中的屬性以指向其他模型的一種模型。
撰寫開啟模型所指向的按兩下處理常式。
撰寫文字範本,載入第一個模型、 依照參考到其他模型,以及讀取其他模型。
建構存取 ModelBus DSL
建立新的 DSL 方案。 此範例中,選取工作流程 方案範本。 語言名稱設為
MBProvider
和".provide"副檔名。在 DSL 定義圖表中,以滑鼠右鍵按一下最上方,不是圖表的空白部分,然後按一下 啟用 Modelbus。
- 如果看不到啟用 Modelbus,您必須下載並安裝 VMSDK ModelBus 擴充功能。 VMSDK 網站上找到它︰ Visualization and Modeling SDK。
在啟用 Modelbus對話方塊中,選取這個 DSL 公開給 ModelBus,然後按一下 確定。
新的專案,
ModelBusAdapter
,加入至方案。
您現在已可透過 ModelBus 文字範本存取 DSL。 參考,可以在程式碼中的命令、 事件處理常式或其中都運作模型檔案編輯器 AppDomain 中的規則來解決。 不過,文字範本不同的 AppDomain 中執行,而且正在進行編輯時,無法存取模型。 如果您想要存取此 DSL 的 ModelBus 參考從文字範本,您必須擁有個別的 ModelBusAdapter。
若要建立文字範本已設定的 ModelBus 配接器
在 Windows 檔案總管中,複製並貼上 ModelBusAdapter.csproj 所在的資料夾。
將資料夾命名為 T4ModelBusAdapter。
重新命名專案檔 T4ModelBusAdapter.csproj。
在方案總管 中,加入 T4ModelBusAdapter MBProvider 解決方案。 以滑鼠右鍵按一下方案節點,指向新增,然後按一下 現有專案。
以滑鼠右鍵按一下 T4ModelBusAdapter 專案節點,然後按一下 屬性。 在 [專案屬性] 視窗中,變更組件名稱和預設命名空間到
Company.MBProvider.T4ModelBusAdapters
。T4ModelBusAdapter 中每個 *.tt 檔案,在 「 T4 」 插入命名空間,最後一個部分,使該行如下所示。
namespace <#= CodeGenerationUtilities.GetPackageNamespace(this.Dsl) #>.T4ModelBusAdapters
在
DslPackage
專案中,加入的專案參考T4ModelBusAdapter
。在 DslPackage\source.extension.tt,加入下列這一行底下
<Content>
。<MefComponent>|T4ModelBusAdapter|</MefComponent>
在
T4ModelBusAdapter
專案中,加入的參考︰ Microsoft.VisualStudio.TextTemplating.Modeling.11.0開啟 T4ModelBusAdapter\AdapterManager.tt:
變更到 AdapterManagerBase 的基底類別VsTextTemplatingModelingAdapterManager。 此組件的檔案現在會如下所示。
namespace <#= CodeGenerationUtilities.GetPackageNamespace(this.Dsl) #>.T4ModelBusAdapters { /// <summary> /// Adapter manager base class (double derived pattern) for the <#= dslName #> Designer /// </summary> public partial class <#= dslName #>AdapterManagerBase : Microsoft.VisualStudio.TextTemplating.Modeling.VsTextTemplatingModelingAdapterManager {
檔案結尾附近插入下列額外的屬性類別 adaptermanager 的前面。
[Microsoft.VisualStudio.Modeling.Integration.HostSpecific(HostName)]
結果如下所示。
/// <summary> /// ModelBus modeling adapter manager for a <#= dslName #>Adapter model adapter /// </summary> [Mef::Export(typeof(DslIntegration::ModelBusAdapterManager))] [Mef::ExportMetadata(DslIntegration::CompositionAttributes.AdapterIdKey,<#= dslName #>Adapter.AdapterId)] [DslIntegration::HostSpecific(DslIntegrationShell::VsModelingAdapterManager.HostName)] [Microsoft.VisualStudio.Modeling.Integration.HostSpecific(HostName)] public partial class <#= dslName #>AdapterManager : <#= dslName #>AdapterManagerBase { }
按一下 [轉換所有範本中的方案總管] 標題列。
重建方案。 按一下 F5。
確認 DSL 運作按下 F5。 在實驗性的專案中,開啟
Sample.provider
。 關閉 Visual Studio 的實驗執行個體。
在文字範本,也在一般程式碼,現在可以解決此 DSL 的 ModelBus 參考。
建構 DSL 的 ModelBus 參考網域屬性
使用最小語言方案範本建立新的 DSL。 MBConsumer 的語言,然後設定要".consume"副檔名。
在 DSL 專案中加入 MBProvider DSL 的組件的參考。 以滑鼠右鍵按一下
MBConsumer\Dsl\References
然後按一下 加入參考。 在瀏覽索引標籤上,找出MBProvider\Dsl\bin\Debug\Company.MBProvider.Dsl.dll
這可讓您建立使用其他 DSL 的程式碼。 如果您想要建立數個 Dsl 的參考,也將其加入。
在 DSL 定義圖表中,以滑鼠右鍵按一下圖表,然後按一下 啟用 ModelBus。 在對話方塊中,選取啟用這個 DSL 以使用 ModelBus。
在類別中
ExampleElement
,加入新的網域屬性MBR
,在 [屬性] 視窗中,將其類型設定為ModelBusReference
。以滑鼠右鍵按一下圖表上的網域屬性,然後按一下編輯 ModelBusReference 特定屬性。 在對話方塊中,選取模型項目。
下列設定檔案對話方塊篩選器。
Provider File|*.provide
之後的子字串"|"是檔案選取對話方塊中的篩選器。 您可以將它設定為允許任何檔案使用 *。*
在模型項目型別清單中,輸入提供者 (例如,Company.MBProvider.Task) DSL 中的一或多個網域類別的名稱。 它們可以是抽象類別。 如果 清單空白,使用者可以設定任何項目的參考。
關閉對話方塊並轉換所有範本。
您已建立可以包含在另一個 DSL 的項目參考的 DSL。
在方案中建立另一個檔案的 ModelBus 參考
在 MBConsumer 方案中,按 CTRL + F5。 實驗性的執行個體Visual Studio中開啟MBConsumer\Debugging專案。
新增複本到 Sample.provide 的MBConsumer\Debugging專案。 這是必要的因為 ModelBus 參考必須參考相同方案中的檔案。
偵錯專案上按一下滑鼠右鍵,指向新增,然後按一下 現有項目。
在加入項目 對話方塊中,若要設定篩選器所有檔案 (*。*).
瀏覽至
MBProvider\Debugging\Sample.provide
然後按一下 新增。
開啟
Sample.consume
。一個 「 範例 」 圖形,然後在 [屬性] 視窗中,按一下**[...]** MBR 屬性中。 在對話方塊中,按一下 瀏覽,然後選取
Sample.provide
。 在項目] 視窗中,展開 [工作類型,然後選取其中一個項目。儲存檔案。
(尚未關閉的實驗執行個體Visual Studio。)
您已建立的模型,包含 ModelBus 參考另一個模型中的項目。
解析文字範本中的 ModelBus 參考
在實驗性執行個體Visual Studio,開啟範例文字範本檔案。 以下列方式設定其內容。
<#@ template debug="true" hostspecific="true" language="C#" inherits="Microsoft.VisualStudio.TextTemplating.Modeling.ModelBusEnabledTextTransformation" #> <#@ MBConsumer processor="MBConsumerDirectiveProcessor" requires="fileName='Sample.consume'" #> <#@ output extension=".txt" #> <#@ assembly name = "Microsoft.VisualStudio.Modeling.Sdk.Integration.11.0" #> <#@ assembly name = "Company.MBProvider.Dsl.dll" #> <#@ import namespace="Microsoft.VisualStudio.Modeling.Integration" #> <#@ import namespace="Company.MBProvider" #> <# // Property provided by the Consumer directive processor: ExampleModel consumerModel = this.ExampleModel; // Iterate through Consumer model, listing the elements: foreach (ExampleElement element in consumerModel.Elements) { #> <#= element.Name #> <# if (element.MBR != null) using (ModelBusAdapter adapter = this.ModelBus.CreateAdapter(element.MBR)) { // If we allowed multiple types or DSLs in the MBR, discover type here. Task task = adapter.ResolveElementReference<Task>(element.MBR); #> <#= element.Name #> is linked to Task: <#= task==null ? "(null)" : task.Name #> <# } } #>
請注意以下要點:
hostSpecific
和inherits
屬性template
指示詞必須設定。取用者模型存取指示詞處理器所產生的 DSL 中透過一般的方式。
組件和匯入指示詞必須能夠存取 ModelBus 與 DSL 的提供者類型。
如果您知道許多 Mbr 會連結至相同的模型,最好是只有一次呼叫 CreateAdapter。
儲存範本。 請確認產生的文字檔案如下所示。
ExampleElement1 ExampleElement2 ExampleElement2 is linked to Task: Task2
解決在軌跡處理常式中的 ModelBus 參考
關閉的實驗執行個體Visual Studio,如果它正在執行。
加入名為 MBConsumer\Dsl\Custom.cs,並將其內容如下的檔案。
namespace Company.MB2Consume { using Microsoft.VisualStudio.Modeling.Integration; using Company.MB3Provider; public partial class ExampleShape { public override void OnDoubleClick(Microsoft.VisualStudio.Modeling.Diagrams.DiagramPointEventArgs e) { base.OnDoubleClick(e); ExampleElement element = this.ModelElement as ExampleElement; if (element.MBR != null) { IModelBus modelbus = this.Store.GetService(typeof(SModelBus)) as IModelBus; using (ModelBusAdapter adapter = modelbus.CreateAdapter(element.MBR)) { Task task = adapter.ResolveElementReference<Task>(element.MBR); // Open a window on this model: ModelBusView view = adapter.GetDefaultView(); view.Show(); view.SetSelection(element.MBR); } } } } }
按下 CTRL+F5。
在實驗性執行個體Visual Studio,開啟
Debugging\Sample.consume
。按兩下圖形。
如果您已設定 MBR 該項目上,開啟參考的模型,並選取參考的項目。