Quantcast
Channel: Gael Duhamel [MVP]
Viewing all articles
Browse latest Browse all 10

Linq provider for CommerceServer : thanks to CSMetal !

$
0
0

A couple of weeks ago, Kerry Havas, Microsoft Commerce Server Product Architect, has released the second version of CSMetal.

What is CSMetal? It’s a free tool for Commerce Server which generates strongly typed classes called by a dedicated Linq provider based on the Commerce Foundation API => ie, that do not replace the Commerce Foundation API!

Kerry did a very well job with CSMetal as the development becomes very easy for all developers who don’t know Commerce Server!

Before using the Linq provider for Commerce Server we have to generate the entities with the CSMetal command line.

You need to have:

  • Le ChannelConfiguration.config file correctly configured
    • Please, check that the useSharedCommerceContexts attribute is set to false.
    • Add to the message handler node with an attribute name called "CommerceQueryOperation_CommerceEntityDefinition" the below nodes:
   1: <!-- Note that RepositoryMetadataLoader is replaced by a slightly customized one -->

   2: <Component name="RepositoryMetadataLoader" type="Microsoft.Commerce.Metal.Generator.OperationSequenceComponents.RepositoryMetadataLoader, Microsoft.Commerce.Metal.Generator, Version=1.0.0.0, Culture=neutral" />

   3: <!-- Note that this is added to add overlay capability to the Metadata repository -->

   4: <Component name="MetadataOverlayProcessor" type="Microsoft.Commerce.Metal.Generator.OperationSequenceComponents.MetadataOverlayProcessor, Microsoft.Commerce.Metal.Generator, Version=1.0.0.0, Culture=neutral" />

      • Note that the last child node allows you roughly to generate new entity or add new attribute to existing entity. More details below.
  • MetadataDefinitions.xml file correctly configured 
    • Add to the RelationshipTypes node, a child one:
      • <RelationshipType name="Inherits"/>
    • Review the “CSMetal_Beta2_ReadMe.rtf” file, and see that we have to add 2 new entities:
      • Basket
        • Order status enumeration
      • Ads
        • Size enumeration for the ads
  • The famous OrderObjectMappings.xml and OrderPipelineMappings.xml files, properly configured.

Put all the beyond dedicated Commerce Server files in a directory, and add some files to the proper execution of CSMetal:

  • CSMetal.exe Smile
  • CSMetal.exe.config which is roughly a copy of the web.config of your web.application
  • GeneratorSettings.xml which allows you to define the output directory for the generated assemblies. And cherry on the cake with your own namespace!
  • Microsoft.Commerce.Metal.Generator.dll for the code generation.

So, at the end you will have:

image

As explained beyond, Kerry introduces a new concept called “MetaDataOverlays”:

  • You can extand your metadata with an external file and do not apply any modification to the MetaDataDefinition.xml file. That allows you to keep the MetaDataDefinition as a repository and the overlays files. This provides a way to manage the changes you have made to the metadata by keeping it in a separate file.

Just add a new xml file called Metadata_CSMetal.xml in a new directory called MetadataOverlays

   1: <?xml version="1.0" encoding="utf-8" ?>

   2: <!-- 

   3: This file contains the Metadata Repository

   4: -->

   5: <MetadataDefinitions xmlns="urn:schemas-microsoft-multi-channel-commerce-foundation-metadata">

   6:     <DefaultChannel>

   7:         <CommerceEntities>

   8:             <!-- Added for Linq based Search -->

   9:             <CommerceEntity name="CatalogEntity">

  10:                 <DisplayName value="Catalog Entity" />

  11:                 <Properties>

  12:                     <Property name="Phrase"    dataType="String">

  13:                         <DisplayName value="Phrase"/>

  14:                     </Property>

  15:                     <Property name="ReturnTypes"    dataType="Integer">

  16:                         <DisplayName value="Return Types"/>

  17:                     </Property>

  18:                     <Property name="FirstItem"    dataType="Integer">

  19:                         <DisplayName value="First Item"/>

  20:                     </Property>

  21:                     <Property name="WhereClause"    dataType="String">

  22:                         <DisplayName value="Where Clause"/>

  23:                     </Property>

  24:                     <Property name="TotalItemCount"    dataType="Integer">

  25:                         <DisplayName value="Total Item Count"/>

  26:                     </Property>

  27:                     <Property name="ResultsPerPage"    dataType="Integer">

  28:                         <DisplayName value="Results Per Page"/>

  29:                     </Property>

  30:                 </Properties>

  31:             </CommerceEntity>

  32:             <!-- Added for Linq based Search -->

  33:  

  34:             <!-- Catalog Start -->

  35:             <CommerceEntity name="Catalog">

  36:                 <Relationships>

  37:                     <Relationship name="DependantCatalogs" type="Relationship" modelName="Catalog" isMultipleItems="true" >

  38:                         <DisplayName value="Dependant Catalogs" />

  39:                     </Relationship>

  40:                     <Relationship name="SourceCatalogs" type="Relationship" modelName="Catalog" isMultipleItems="true" >

  41:                         <DisplayName value="Source Catalogs" />

  42:                     </Relationship>

  43:                 </Relationships>

  44:             </CommerceEntity>

  45:  

  46:             <!-- CreditCard Start -->

  47:             <CommerceEntity name="CreditCard">

  48:                 <Relationships>

  49:                     <Relationship name="PaymentAccount" type="Inherits" modelName="PaymentAccount" isMultipleItems="false">

  50:                     </Relationship>

  51:                 </Relationships>

  52:             </CommerceEntity>

  53:  

  54:             <!-- Basket Start -->

  55:             <CommerceEntity name="Basket">

  56:                 <Relationships>

  57:                     <Relationship name="LineItems" modelName="LineItem" type="Relationship" isMultipleItems="true" />

  58:                     <Relationship name="Addresses" modelName="Address" type="Relationship" isMultipleItems="true" />

  59:                     <Relationship name="Payments" modelName="Payment" type="Relationship" isMultipleItems="true" />

  60:                     <Relationship name="Shipments" modelName="Shipment" type="Relationship" isMultipleItems="true" />

  61:                     <Relationship name="RequestedPromoCodes" modelName="RequestedPromoCode" type="Relationship" isMultipleItems="true" />

  62:                 </Relationships>

  63:             </CommerceEntity>

  64:  

  65:             <!-- ShopperList Start -->

  66:             <CommerceEntity name="ShopperList">

  67:                 <Relationships>

  68:                     <Relationship name="LineItems" modelName="LineItem" type="Relationship" isMultipleItems="true" />

  69:                 </Relationships>

  70:             </CommerceEntity>

  71:  

  72:             <!-- LineItem Start -->

  73:             <CommerceEntity name="LineItem">

  74:                 <Relationships>

  75:                     <Relationship name="ItemLevelDiscounts" modelName="Discount" type="Relationship" isMultipleItems="true" />

  76:                 </Relationships>

  77:             </CommerceEntity>

  78:  

  79:             <!-- CashCard Start -->

  80:             <CommerceEntity name="CashCard">

  81:                 <Relationships>

  82:                     <Relationship name="PaymentAccount" type="Inherits" modelName="PaymentAccount" isMultipleItems="false">

  83:                     </Relationship>

  84:                 </Relationships>

  85:             </CommerceEntity>

  86:  

  87:             <!-- GiftCertificate Start -->

  88:             <CommerceEntity name="GiftCertificate">

  89:                 <Relationships>

  90:                     <Relationship name="PaymentAccount" type="Inherits" modelName="PaymentAccount" isMultipleItems="false">

  91:                     </Relationship>

  92:                 </Relationships>

  93:             </CommerceEntity>

  94:  

  95:             <!-- Shipment Start -->

  96:             <CommerceEntity name="Shipment">

  97:                 <Relationships>

  98:                     <Relationship name="ShippingMethod" modelName="ShippingMethod" type="Relationship" isMultipleItems="false" />

  99:                 </Relationships>

 100:             </CommerceEntity>

 101:  

 102:             <!-- Payment Start -->

 103:  

 104:             <!-- PaymentAccount Start -->

 105:             <CommerceEntity name="PaymentAccount">

 106:                 <Properties>

 107:                     <Property name="Id" dataType="String">

 108:                         <DisplayName value="Id"/>

 109:                         <Description value="Unique ID of the payment." />

 110:                     </Property>

 111:                     <Property name="CustomerName" dataType="String">

 112:                         <DisplayName value="CustomerName"/>

 113:                         <Description value="Name of Customer." />

 114:                     </Property>

 115:                     <Property name="BillingAddressId" dataType="String">

 116:                         <DisplayName value="BillingAddressId"/>

 117:                         <Description value="Id of billing address" />

 118:                     </Property>

 119:                     <Property name="DisplayName" dataType="String">

 120:                         <DisplayName value="DisplayName"/>

 121:                         <Description value="DisplayName." />

 122:                     </Property>

 123:                 </Properties>

 124:                 <Relationships>

 125:                     <Relationship name="PaymentMethod" modelName="PaymentMethod" type="Relationship" isMultipleItems="false" />

 126:                 </Relationships>

 127:             </CommerceEntity>

 128:             

 129:             <CommerceEntity name="Payment">

 130:                 <Relationships>

 131:                     <Relationship name="PaymentAccount" modelName="PaymentAccount" type="Relationship" isMultipleItems="false" />

 132:                 </Relationships>

 133:             </CommerceEntity>

 134:         </CommerceEntities>

 135:     </DefaultChannel>

 136: </MetadataDefinitions>

Now it’s time to run the CSMetal.exe:

image

In the directories, you will find 3 cs files (if you configure the generation for odata), one for the context, one for the entities and one for odata:

image

Copy the files GeneratedContexts.cs and GeneratedTypes.cs in the project CSMetalLinqProvider and re run a new compilation for this project.

Your Commerce Entities are now available through your Linq provider for Commerce Server Smile

To get a category, just write down these lines of code:

var category = new CategoryContext(commerceServiceProxy)
                  .IncludeChildCategories()
                  .Single(o => o.Id=="Departments" && o.CatalogId==DefaultCatalog);
var childCategories = category.ChildCategories;
or for a product :
var product = new ProductContext(commerceServiceProxy)
                 .Single(o=>o.CatalogId==DefaultCatalog && o.Id=="AW200-12");
var productCode = product.ProductCode;

That’s just a simple sample of what the Commerce Server Linq Provider is able to do and I’ll try to show you more in my next posts. But, I’m sure that will simplify the life for your developer Smile


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images