Visual Studio 2010 Code Snippets

Posts   
 
    
Posts: 14
Joined: 14-Jan-2009
# Posted on: 08-Oct-2010 20:25:27   

Hey gang,

I created a code snippet for VS2010 to create a generic method that creates a dynamic typed list. Once in VS, just hit cntl-K, cntl-X to get the code snippet menu, select this snippet and voila! The method is totally stubbed out. This version includes comments and placeholders for certain variables which can be removed from the code/snippet as developers get more used to the code.

Has anyone else created code snippets? I am in the process of creating them for ScalarQueryExpression, Expressions, new EntityField and a few other LLBLGen features that we use but often have to refer back to the forum or docs for the specific usages.

Would appreciate any additions or comments people have on the below, or suggestions of new ones.

USAGE: Save the below as mySnippet.snippet. Then in VS2010, go to Tools / Code Snippets Manager and "Add..." the .snippet file you just created. Then you can use cntl-K, cntl-X, select your snippet and voila!

Mark


<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        LLBLGen Static Dynamic Typed List - DataTable Method Snippet
      </Title>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>SD.LLBLGen.Pro.ORMSupportClasses</Assembly>
        </Reference>
      </References>
      <Declarations>
        <Literal>
          <ID>functionname</ID>
          <ToolTip>The name of your function.</ToolTip>
          <Default>GetDataTable</Default>
        </Literal>
      </Declarations>
      <Code Language="CSharp">
        <![CDATA[
/// <summary>
/// Get a custom DataTable via the TypedListDAO.GetMultiAsDataTable Method.
/// The "EntityFields.FieldName" should be changed to whatever Entity you are interested in, e.g. "ProductFields.ProductId."
/// The "Field Name Alias" should be changed to whatever you want the string-based name of the field to be named, e.g. "TotalProductCount."
/// The "Entity.Relations.FKRelationship" should be changed to your Entity name and the actual FK relationship involved, e.g. "ProductEntity.Relations.OrderEntityViaProductOrders."
/// Make sure you have added the appropriate "using" directives for your Entity, Collection and Helper classes.
/// </summary>
/// <returns>A DataTable with defined "fields," per "filter" WHERE clause, joined to "relations," grouped by "groupBy" non-aggregate fields and sorted by "sort."</returns>
private static DataTable $functionname$()
{
  // Define the fields that you want in your result DataTable
  ResultsetFields fields = new ResultsetFields(3);
  // fields.DefineField(EntityFields.FieldName1, 0);
  // fields.DefineField(EntityFields.FieldName2, 1, "Field Name Alias");
  // fields.DefineField(EntityFields.FieldName3, 2, "Aggregate Name Alias", AggregateFunction.Count);

  // Add the WHERE clause to the query - "Condition" can be a literal or a variable passed into this method
  IPredicateExpression filter = new PredicateExpression();
  // filter.Add(EntityFields.FieldName == "Condition");

  // Add all JOIN clauses to the relation collection
  IRelationCollection relations = new RelationCollection();
  // relations.Add(Entity.Relations.FKRelationship);
  // relations.Add(Entity.Relations.FKRelationship, JoinHint.Left);

  // Must add all non-aggregated fields to the groupBy collection
  IGroupByCollection groupBy = new GroupByCollection();
  // groupBy.Add(fields[0]);                            

  ISortExpression sort = new SortExpression();
  // sort.Add(EntityFields.FieldName | SortOperator.Ascending);

  // Create the DataTable, DAO and fill the DataTable with the above query definition/parameters
  DataTable dt = new DataTable();
  TypedListDAO dao = new TypedListDAO();
  dao.GetMultiAsDataTable(fields, dt, 0, sort, filter, relations, false, groupBy, null, 0, 0);

  return dt;
}       
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>


Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 11-Oct-2010 15:15:28   

Thanks for sharing.

Suggestion: DynamicList using Adapter would be nice too simple_smile