Roberto Brunetti

ASP.NET - Mobility
Team System

SharePoint Conference

.NET Programming

Corsi

SharePoint

GAT: Creare una Unbound Recipe

Dopo un giorno di consulenza in cui abbiamo anche affrontata l'argomento di generazione codice e automatizzazione procedure e abbiamo analizzato GAT per la parte di Recipe, Action, Template Reference, Bound Recipe e creazione di Action, Editor e Converters custom, visto che la documentazione sul prodotto è praticamente inesistente posto il codice per creare una unbound recipe, condivido i passi per la creazione di una unbound recipe reference.

Nel file .xml di definizione del Guidance Package occorre inserire una "BindingRecipe", ovvero la Recipe che viene eseguita quando si abilita un guidance package in un progetto Visual Studio.

<Recipe Name="BindingRecipe">

<Types>

<TypeAlias Name="RefCreator" Type="Microsoft.Practices.RecipeFramework.Library.Actions.CreateUnboundReferenceAction, Microsoft.Practices.RecipeFramework.Library"/>

</Types>

<Caption>Crea una reference unbound nella "BindingRecipe" </Caption> <Actions>

<Action Name="UnboundRecipe" Type="RefCreator" AssetName="MyUnboundRecipe"

ReferenceType="GATSteps07.References.MyUnboundReference, GATSteps07" /> </Actions>

</Recipe>

Nel mio caso il progetto GAT si chiama GATSteps07. Nel progetto è stata creata una classe (nella directory references) derivata da UnboundRecipeReference denominata MyUnboundReference. Il namespace utilizzato è GATSteps07.References.

Ecco il codice della classe (scusate l'eventuale indentazione sballata)

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Practices.RecipeFramework;

namespace GATSteps07.References

{

public class MyUnboundReference : UnboundRecipeReference

{

public MyUnboundReference(string recipe) : base(recipe) { }

public override bool IsEnabledFor(object target)

{

return true;

}

public override string AppliesTo

{

get { return "Dove ti pare"; }

}

}

}

Il codice molto semplice indica true come valore di ritorno per la condizione di abilitazione della Unbound Recipe Reference quindi la reference viene attivata da qualunque elemento Visual Studio e sarà sempre presenta nel Guidance Package Navigator.

Fatto questo è sufficiente creare la Recipe MyUnboundRecipe (il nome deve corrispondere a quanto indicato in AssetName) come una qualunque altra Recipe del Package. L'unica particolarità è l'attributo Bound impostato a False.

A questo punto è sufficiente avere un progetto Visual Studio, abilitare il Guidance Package e richiamare la Recipe "Aggiunge una classe autogenerata per test".

Per completezza aggiungo la definizione della Recipe MyUnboundRecipe che è identica (per coloro che hanno seguito il nostro corso su GAT/GAX o le sessioni di DevCon) alla demo 04 dove appunto andavamo ad aggiungere al progetto corrente di Visual Studio una classe autogenerata con il T4 Template.

<Recipe Name="MyUnboundRecipe" Bound="false">

<Caption>Aggiunge una classe autogenerata per test</Caption>

<HostData>

<Icon ID="525" />

<CommandBar Name="Solution" />

<CommandBar Name="Solution Folder" />

<CommandBar Name="Project" />

<CommandBar Name="Folder" />

<CommandBar Name="Item" />

<CommandBar Name="Web Project" />

<CommandBar Name="Web Folder" />

<CommandBar Name="Web Item" />

</HostData>

<Arguments>

<!-- Recupero progetto corrente EnvDTE di VS 2005 -->

<Argument Name="CurrentProject" Type="EnvDTE.Project, EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">

<ValueProvider Type="Microsoft.Practices.RecipeFramework.Library.ValueProviders.FirstSelectedProject, Microsoft.Practices.RecipeFramework.Library" />

</Argument>

<!-- Recupero DefaultNamespace dalle propriet… del progetto -->

<Argument Name="ProjectSuffixName">

<ValueProvider Type="Microsoft.Practices.RecipeFramework.Library.ValueProviders.ExpressionEvaluatorValueProvider,Microsoft.Practices.RecipeFramework.Library"

Expression="$(CurrentProject.Properties.Item('DefaultNamespace').Value)" />

</Argument>

<!-- Nome per la classe da creare -->

<Argument Name="EntityClassName">

<Converter Type="Microsoft.Practices.RecipeFramework.Library.Converters.CodeIdentifierStringConverter, Microsoft.Practices.RecipeFramework.Library"/>

</Argument>

<!-- Nome del file della classe da creare -->

<Argument Name="EntityTargetFile">

<ValueProvider Type="Microsoft.Practices.RecipeFramework.Library.ValueProviders.ExpressionEvaluatorValueProvider,Microsoft.Practices.RecipeFramework.Library"

Expression="$(EntityClassName).cs">

<MonitorArgument Name="EntityClassName" />

</ValueProvider>

</Argument>

<!-- Quanti elementi (richiesti nel Wizard) creare -->

<Argument Name="HowMany" Type="System.Int32">

</Argument>

</Arguments>

<GatheringServiceData>

<Wizard xmlns="http://schemas.microsoft.com/pag/gax-wizards" SchemaVersion="1.0">

<Pages>

<Page>

<Title>GATStep07 Information </Title>

<LinkTitle>XXX Information Title</LinkTitle>

<Help>

Metti i' nome di' progetto !

</Help>

<Fields>

<Field ValueName="EntityClassName" Label="Nome Classe" InvalidValueMessage="Deve essere un nome di classe valido" />

<Field ValueName="HowMany" Label="Quanti" InvalidValueMessage="Deve essere un numero" />

</Fields>

</Page>

</Pages>

</Wizard>

</GatheringServiceData>

<Actions>

<Action Name="GenerateClass"

Type="Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TextTemplateAction, Microsoft.Practices.RecipeFramework.VisualStudio.Library"

Template="Text\Entity.t4">

<Input Name="ClassName" RecipeArgument="EntityClassName" />

<Input Name="TargetNamespace" RecipeArgument="ProjectSuffixName" />

<Input Name="HowMany" RecipeArgument="HowMany" />

<Output Name="Content" />

</Action>

<Action Name="AddEntityClass"

Type="Microsoft.Practices.RecipeFramework.Library.Actions.AddItemFromStringAction, Microsoft.Practices.RecipeFramework.Library"

Open="false">

<Input Name="Content" ActionOutput="GenerateClass.Content" />

<Input Name="TargetFileName" RecipeArgument="EntityTargetFile" />

<Input Name="Project" RecipeArgument="CurrentProject" />

</Action>

</Actions>

</Recipe>

Spero utile

Posted: lug 12 2008, 07.42 by rob | with no comments
Filed under: