Archive

Archive for November, 2012

CodeFluent Entities and JSON/REST

November 29, 2012 Leave a comment

In a previous post we announced that CodeFluent Entities natively support JSON/REST.

In this post we’re going to see how to take advantage of the generated JSON/REST architecture. Thanks to JSON WCF services we should be able to call server methods from the client of our choice. To illustrate that we’re going to create a back end application exposing JSON WCF services that will be consumed in a console application. As you’re going to see, all the hard work will be done by CodeFluent Entities.

Creating the solution

Start by creating a new CodeFluent Project. We’ll use the “ContactManager Sample Model” for the purpose of this article.ContactManagerModel

  • Add a new Class Library project named “ContactManager” to your solution. It will be used to host the generated files corresponding to your Business Object model and JSON WCF service’s contracts.
  • Add a new SQL Server Database project named “ContactManager.Persistence” to your solution. It will be used to host your generated SQL scripts.
  • Add a new Empty ASP.NET Website project named “ContactManager.BackEnd” to your solution and add a reference to the ContactManager project and create a “Services” folder. It will be used to host the JSON WCF Services.
  • Add a new Console Application project named “ContactManager.Client” to your solution and add a reference to the ContactManager project and to CodeFluent.Runtime.dll. We will use it to make some calls via JSON WCF to the backEnd application and display the results in the console. Also, by default, Microsoft Visual Studio 2010 sets the target framework to .NET Framework 4 Client Profile. Go to the property of your project and change its target to .NET Framework 4.
    image

Adding the producers

  • Add a Business Object Model Producer and set its Target Directory to the ContactManager project. Also add it a Service Object Model Sub Producer and configure it so JSON been enabled and no proxy been produced. This producer will generate the complete WCF-based communication layer (service, contracts) with native JSON/REST support.
  • Add a SQL Server Producer and set its target directory to the ContactManager.Persistence project.
  • Add a Template Producer and set its Target Directory to the the Services folder of your ContactManager.BackEnd project. Also set its Source Directory to “CodeFluent Entities installation folder > Modeler >Templates > ServiceModel > JsonWebServices > Services”.

Configuring the solution

Configure your solution so it first start your  ContactManager.BackEnd application and then your ContactManager.Client application.

Generating code

Now to start generating code, build your CodeFluent Entities project called ContactManager.Model.

Here is the visual studio solution you should have by now, each project containing the generated code (your database should have been created too):

ScreenShot051

Writing the client

When the WCF Service Object Model Producer is set to generate JSON web services, it automatically adds two extra operations to the Business Object model and Services:
1. JsonGet
2. JsonPost

As the name suggests, you can use those methods to read (JsonGet) or write (JsonPost) by calling any other operation available on the service.

So why not use the other operations directly instead?
Well you can (using HTTP POST method and the header Content-Type set to application/json), but what’s interesting with those JsonGet/Post methods is that they add extra JSON oriented features. One of them being able to wrap .NET exception in a nice and simple JavaScript-friendly object.

Note: JsonGet and JsonPost methods are not fully documented yet but a new version of the documentation is on its way.

Here we want to use the JsonGet method in our console application to call the default generated LoadAll method and write the result on the console:

we can break the task down to the following steps:

  • Set up the URL with all parameters
  • Send a GET WebRequest
  • Receive the Json formatted data
  • Deserialize the object(s)
  • Write the result on the console

Therefore, our console application should look something like this:

using System;
using System.Net;
using System.IO;
using CodeFluent.Runtime.Utilities;
using ContactManager;

namespace ContactManager.ConsolApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Type a key to start");
            Console.ReadKey();

            string url = "http://localhost:62738/Services/ContactManager.Customer.svc/JsonGet?method=LoadAll";
            string jsonContent = GET(url);

            //Let's display the RAW JSON content first:
            Console.WriteLine("JSON Content:");
            Console.WriteLine(jsonContent + Environment.NewLine);

            //Then each Customer:
            Console.WriteLine("From deserialized content:");
            CustomerCollection customers = JsonUtilities.Deserialize<CustomerCollection>(jsonContent);
            foreach (Customer c in customers)
            {
                Console.WriteLine(c.Trace());
            }
        }

    }
}

Notes: the URL should be changed according to your configuration (port number, and service name should be updated);

The GET method simply downloads and returns the internet string response as follow:

// Returns JSON string
private static string GET(string url)
{
    using (WebClient client = new WebClient())
    {
        try
        {
            return client.DownloadString(url);
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, new System.Text.UTF8Encoding());
                String errorText = reader.ReadToEnd();

                Console.WriteLine(errorText);
            }
            throw;
        }
    }
}

But where do we find the JSON Serializer? Well the CodeFluent Runtime (CodeFluent.Runtime.dll) comes with a very convenient class: JsonUtilities.
The JsonUtilities class is a simple yet powerful JSON Serializer allowing you to convert your .NET objects into JSON and vice versa. See that post for more information.

Running the application

Hitting Ctrl+F5 should build and launch your backend application and then your console application. Typing a key should retrieve all the customers using JSON WCF services and display them on the console as follow:

Type a key to start
JSON Content:
[{“EntityKey”:”1″,”EntityDisplayName”:”info@softfluent.com”,”RowVersion”:[0,0,0,0,0,0,7,209],”Id”:1,”EmailAddress”:”info@softfluent.com”,”Password”:null,
“FirstName”:”Thib”,”LastName”:”Nes”,”FullName”:null,”BirthdayDate”:”\/Date(-62135596800000)\/”,”LoyaltyProgramMember”:false,”LicenseNo”:null,”HasPreAccidents”:false,
“Age”:0,”AddressId”:-1,”Rentals”:[],”PaymentType”:0,”EntityState”:1}]

From deserialized content:
[Id=1,EmailAddress=info@softfluent.com,Password=,FirstName=Thib,
LastName=Nes,FullName=,BirthdayDate=1/1/0001 12:00:00 AM,LoyaltyProgramMember=False,LicenseNo=,HasPreAccidents=False,Age=0,
Address=<null>,_addressId=-1,Rentals=[Count=0],PaymentType=Cash, EntityState=Unchanged]
Press any key to continue . . .

 

What about saving data? Well, we would use the JsonPost method instead of JsonGet and use the JsonUtilities.Serialize method to serialize our content. The Main method of our console application would look like this:

static void Main(string[] args)
{
    string url = "http://localhost:62738/Services/ContactManager.Customer.svc/JsonPost?type=EntitySaveCall";
    Customer c = new Customer { FirstName = "John", LastName = "Doe", Age = 42 };
    string jsonContent = JsonUtilities.Serialize(c);
    
    POST(url, jsonContent);
}

And the POST method to send the data:

// POST a JSON string
private static void POST(string url, string jsonContent)
{
    using (WebClient client = new WebClient())
    {
        string response = string.Empty;
        //We need to specify that we are sending Json contents, or our
        //requests will be rejected by the services
        client.Headers.Add("Content-Type: json; charset=UTF-8");
        client.Encoding = System.Text.Encoding.UTF8;
        try
        {
            response = client.UploadString(url, jsonContent);
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, new System.Text.UTF8Encoding());
                String errorText = reader.ReadToEnd();

                Console.WriteLine(errorText);
            }
            throw;
        }
    }
}

This is it! This post highlighted the fact that we can directly consume JSON WCF services generated by CodeFluent Entities from the client of our choice (for instance for Windows Phone, iOS or Android to name but a few). CodeFluent Entities doing all the hard work for us by generating applications based on REST/JSON architecture in a couple of minutes.

Hope this helps!

Cheers,

Thibault NESTOR

Using the SQL Server Template Producer to generate Clustered Indexes

November 27, 2012 Leave a comment

In this post we’ll see how using CodeFluent Entities’ SQL Server Template Producer, you can generate SQL scripts using  the built-in template engine whilst accessing the inferred meta-model, and then automatically deploy the generated script on your desired server.

By default SQL Azure and SQL Server add the clustered index on the primary key if a clustered index on the table does not already exist and if you do not specify a unique nonclustered index. In the case where the primary key is of GUID type it won’t be efficient. The reason for this is that GUIDs are generated in non-sequential order and SQL Server orders a clustered index sequentially. It will work – SQL Server will let you build a clustered index around a unique identifier column, however it will cause the SQL Server to do unnecessary work and cause performance slowdowns.

That being said, what we can do is using the SQL Template Producer to remove all inefficient clustered indexes and recreate them on the right columns. Let’s even go a bit further and create a little aspect that will add a property on each property to tell if a clustered index needs to be created or not on that particular property.

Add a new Part called IsClusteredIndexAspect and past it the following code (replacing the defaultNamespace’s value by yours):

<cf:project xmlns:cf=”http://www.softfluent.com/codefluent/2005/1” defaultNamespace=”yourNamespace”>

<cf:pattern name=”IsClusteredIndex Aspect” namespaceUri=”http://www.sample.com/aspects/isclusteredindexaspect/2012/11” preferredPrefix=”sa” step=”Tables”>

<cf:message class=”_doc”>
This aspect creates an extra IsClusteredIndex bool property on every property.
</cf:message>

<cf:descriptor name=”IsClusteredIndex” typeName=”boolean” targets=”Property” defaultValue=”false” displayName=”IsClusteredIndex” description=”Should the IsClusteredIndex Aspect apply to this property?” />

</cf:pattern>
</cf:project>

This will create our aspect and add a IsClusteredIndex property on each property in the “Aspects and Producers” property grid:

image

You can by now choose which property you want to use as a clustered index. Obviously this property should be set to true only on one property by entity since clustered index cannot be applied on several columns.

Now let’s write a script that will remove all clustered indexes and then create new ones based on the columns selected thanks to our aspect. In a file called “[Template]CreateIndexes.sql” add it the following code (Note that this code is only an illustration for this post, it does not take into account constraints, primary keys and so on):

[%@ namespace name="CodeFluent.Model"%]
[%@ namespace name="CodeFluent.Model.Persistence"%]
[%@ namespace name="CodeFluent.Producers.SqlServer"%]
/* [%=Producer.GetSignature()%] */

[%foreach (Entity e in Producer.Project.Entities)
{%]
    --remove any existing clustered index on e.Table.FullName
    --[...]
  
   [%foreach (Property p in e.Properties)
    {
        if (p.GetAttributeValue("sa:IsClusteredIndex", false))
        {%]      
        --create your index such as:
        CREATE CLUSTERED INDEX [CL_[%=p.Entity.Table.Name%]] ON [%=p.Entity.Table.FullName%] ([[%=p.Column.Name%]]);
        GO
        [%}
    }
}%]

 

Create a folder called “Template” under the file folder of your CodeFluent Entities project:

image

Right click on that folder and choose “Add existing item” then browse to your template file and select it.

Now in your CodeFluent Entities project, add an instance of the SQL Server Template Producer with your Template directory as the “Source Directory” and set the “Target Directory” to the project of you choice and then build over your model. The SQL Server Template Producer will generate a script file from your template, and run the script on the server removing and creating clustered indexes. Therefore using the template producer you can quickly create complex scripts by taking advantage of the meta model.

 

Cheers,

Thibault NESTOR

Generating unit test skeleton using the Template Producer

November 26, 2012 1 comment

We already introduced the Template Producer in a previous post with a small example showing how to generate a DGML graph from a CodeFluent Entities model. This post is another example to remind you how easy it is to generate text files using the Template Producer whilst accessing the inferred meta-model.

Let’s say we’d like to generate a skeleton for our unit tests. By skeleton I mean a vb or csharp file by entity that will contain test methods ready to be implemented.

Assuming we are writing a pretty simple bank application. Our model has an “Account” entity and 3 methods (to deposit, withdraw, and transfer funds) as follow:

image

Supposing we’re using Nunit as unit-testing framework, let’s create our template to be used by the Template Producer:

    1. Add a new folder called Templates into the File folder
      ScreenShot047
    2. Add it a new text file (Ctrl+shift+A) called “[Template]UnitTests.tplScreenShot049
    3. Past it the following code describing our skeleton:
[%@ namespace name="System"%]
[%@ namespace name="CodeFluent.Model"%]
[%@ namespace name="CodeFluent.Model.Code"%]
[%@ template enumerable='Producer.Project.Entities' enumerableItemName="entity" enumerableTargetPathFunc='Path.Combine(Path.GetDirectoryName(TargetPath), String.Format("{0}Tests", entity.ClrFullTypeName)) + ".cs"' %]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace [%= entity.Namespace %]
{
    [TestFixture]
    public class [%= String.Format("{0}Tests", entity.Name) %]
    {
        [SetUp]
        public void Init()
        {
            /* ... */
        }

        [TearDown]
        public void Cleanup()
        {
            /* ... */
        }

        [% foreach(Method m in entity.Methods) { if (!m.Enabled) continue; %]
            [Test]
            [%= String.Format("public void {0}Test()", m.Name)  %]
            {
                /* ... */
            }
        [% } %]
    }
}

Basically, what the template does is that it iterates through all entities of the project as well as each method to write a file by entity containing a test class with all test methods.

  1. Create a class library project called Bank.UnitTests to store all your unit tests and add it a reference to Nunit framework.
  2. Add a Template Producer and set the Source directory to your Templates folder created earlier and the Target directory to the Bank.UnitTests project.

The Template Producer instance will execute all templates placed in the Templates directory, and place output files in the Bank.UnitTests project directory.

Building your model should produce one file called Bank.AccountTests.cs with all the test methods ready to be implemented:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Bank
{
    [TestFixture]
    public class AccountTests
    {
        [SetUp]
        public void Init()
        {
            /* ... */
        }

        [TearDown]
        public void Cleanup()
        {
            /* ... */
        }

        [Test]
        public void DepositTest()
        {
            /* ... */
        }

        [Test]
        public void WithdrawTest()
        {
            /* ... */
        }

        [Test]
        public void TransferFundsTest()
        {
            /* ... */
        }

    }
}

As you can see, this short and simple example gives you an idea of what it is possible to do by taking advantage of the meta model using the Template Producer.

Want to know more? You’ll find more information on the Template Producer here.

Cheers,

Thibault NESTOR

November 23rd 2012 Links: Visual Studio 2012, .NET 4.5, Office 2013, IE10, Windows 8, SQL Server 2012, TFS, TypeScript, Build

November 23, 2012 Leave a comment

Here is our new entry in our link-listing series:

 

Windows 8

Windows 8 launched
Windows 8 has been released.

Windows Phone 8 launched
Windows Phone 8 has been released.

Microsoft Design Language: The newest official way to refer to ‘Metro’
Microsoft Design Language is apparently the new term for the design first known as Metro…

 

Visual Studio 2012 / .NET 4.5

Visual Studio 2012 and .NET 4.5 Launched
Visual Studio 2012 and .NET 4.5 have been released.

Visual Studio 2012 Update 1 CTP 4
Microsoft released the final CTP of Visual Studio 2012 Update 1.

Productivity Power Tools 2012
Power Tools for Visual Studio 2012.

 

Office 2013

Office 2013 reaches RTM!
Office 2013 Family product reaches RTM! (Office 2013, Lync 2013, Exchange 2013, SharePoint 2013)

 

IE10

IE10: Fast, Fluid, Perfect for Touch, and Available Now for Windows 7
IE10 available for Windows 7.

 

SQL Server 2012

Announcing Microsoft SQL Server 2012 Service Pack 1 (SP1)
Updates and new capabilities for SQL Server 2012.

 

Software Development

TypeScript
Microsoft unveiled Anders Hejlsberg’s newest creation: TypeScript.

Team Foundation Service is Released
A version of Team Foundation Server hosted on Windows Azure.

Announcing the release of the .NET Framework for Windows Phone 8
.NET on Windows Phone 8

 

Miscellaneous

Windows head Steven Sinofsky leaves Microsoft
Microsoft executive Steven Sinofsky, in charge of Windows has left the company.

Microsoft To End Windows Live Messenger Service
Windows Live Messenger Service to be retired soon.

Build
The Build conference 2012.

 

 

Cheers,

Thibault Nestor

Count and Delete methods now supported by Event Rules

November 20, 2012 Leave a comment

Event rules are triggered upon a lifecycle event of a CodeFluent Entities concept allowing the developer to plug himself in the lifecycle (e.g. before saving an entity, or before the validation step) of an entity to extend its behavior. Event rules can be applied on the following three concepts:

  1. On entities,
  2. On properties,
  3. On methods.
    The third one (On Methods) contains three event rule types:
  • OnAddParameters
  • OnAfter
  • OnBefore
    Until today the Count and Delete methods were not supported by Method event rules. This post is meant to let you know that as of today Delete and Count methods can be used with event rules.

More information on Event rules in the documentation.

Cheers,

Thibault NESTOR

Custom Sub Producer Example

November 16, 2012 Leave a comment

There are several ways to add custom code to the generated code. You could simply do it by using Partial classes (which would be a manual action), Snippets or Aspects. But what if you wanted to add custom code to a specific layer such as the Business Object Layer right before production? Well you would need to create your own Sub Producer.

Creating your own custom Sub Producers in CodeFluent Entities is pretty straightforward: all you have to do is to:

1. create a Class Library project referencing the following assemblies:

  • CodeFluent.Model.dll
  • CodeFluent.Model.Common.dll
  • CodeFluent.Producers.CodeDom.dll

2. create a class implementing the ICodeDomSubProducer interface.

3. build and Deploy the Class Library to the CodeFluent current directory (%ProgramFiles%\SoftFluent\CodeFluent\Modeler).

Everything is documented here with examples: Developing a Custom Sub-Producer.

Besides, since it easier to understand something we can actually see running, you’ll find a sample project to play with based on real world code on our forum: Ninject dependency injection.

 

Cheers,

Thibault NESTOR

Property Default Values and Enumerations

November 15, 2012 Leave a comment

CodeFluent Entities defines the concept of “default value” for properties of any type, including value types and enums.

Each property has two types of default values:

  • the default value in the Persistence layer
  • the default value in the Business Object Model layer.
    In this article will focus on the default value in the Persistence Layer that allows to store undefined/null value types and enums (which cannot be nullable), in the database. So, for example, you can store null value types and enums without having to use .NET Nullable<T> type.

The default value depends on the type and what you should know is that for an enumeration type, the default value is the enumeration default value. By default, an enumeration default value is the first value. So for instance in the following example, “Valid” would be the default value and hence becoming NULL in database.

<cf:enumeration name=”EmailStatus” namespace=”Contacts”>
<cf:enumerationValue name=”Valid” /> <!– will become NULL in the Database –>
<cf:enumerationValue name=”Invalid” />
</cf:enumeration>

Obviously in the example above, considering the “Valid” enumeration value as the default value is not necessarily useful. Using an “Undefined” enumeration value for instance would be cleverer:

<cf:enumeration name=”EmailStatus” namespace=”Contacts”>
<cf:enumerationValue name=”Undefined” /> <!– will become NULL in the Database–>
<cf:enumerationValue name=”Valid” />
<cf:enumerationValue name=”Invalid” />
</cf:enumeration>

This is especially useful if it has some correspondence in the .NET code, for example, if you want to display a drop down list with a “not selected” option, or if you do conversion into this enum type and want to handle the “error” case.

 

Although the default values behavior is enabled by default you can also change it:

  • at the Property level by setting its “usePersistenceDefaultValue” attribute to false.

<cf:property name=”CollectionStatus” typeName=”Contacts.EmailStatus” usePersistenceDefaultValue=”false” />

  • at the Enumeration level by setting its “usePersistenceDefaultValue” attribute to false.

<cf:enumeration name=”Contacts.EmailStatus” namespace=”Contacts” usePersistenceDefaultValue=”false” />

  • at the Project level by setting its “defaultUsePersistenceDefaultValue” attribute to false.

<cf:project […] defaultUsePersistenceDefaultValue=”false” />

 

More on Default Values in the documentation: Property Default Values

 

Cheers,

Thibault NESTOR