Views, Auto-Lightweight and the Modeler

April 22, 2014 Leave a comment

In a previous post we saw how to create views and lightweight entities by using XML. Nowadays most of you are using the graphical modeler, so let’s see how to create views.

First, let’s create a simple model:

model

We can create a view that contains the supplier name and the country name. Select the supplier entity and click the “Add view” button from the ribbon:

ribbon - add view

You should see the following window:

Add view 1

Enter a valid name and select properties to include in the view. Only properties from the selected entity are shown. So to add the country name, we have to click the “Add” button and enter the binding expression “Country.Name” (auto completion prevents mistakes).

Add view 2

The « Auto infer lightweight entity » checkbox allows to generate an entity that contains selected properties. As the name suggests it’s a light entity: it’s not persistent, it has no CRUD methods (e.g. load, save, delete) nor extra properties by default, and it does not implement any interfaces other than the ICodeFluentLightEntity by default.

Now we can create a method that will load data from this view:

load from view

The generated code contains the SQL view :

CREATE VIEW [dbo].[vSupplierSupplierLight]
AS
SELECT [Supplier].[Supplier_Name], [Country].[Country_Name] AS 'CountryName'
FROM [Supplier]
INNER JOIN [Country]
ON ([Supplier].[Supplier_Country_Id] = [Country].[Country_Id])

And the inferred lightweight entity:

BOM

 

Happy View-ing!

The R&D team

Using LocalDB with CodeFluent Entities

April 8, 2014 Leave a comment

With Microsoft SQL Server 2012, Microsoft has introduced a feature called LocalDB which is a new edition of SQL Express. LocalDB is created specifically for developers and it is much easier to install (no service) and manage than standard editions. Developers initiate a connection by using a special connection string. It supports AttachDbFileName property, which allows you to specify a database file location.

When connecting, the server is automatically created and started, enabling the application to use the database without complex configuration tasks. This edition uses the same sqlservr.exe as the regular SQL Express and other editions of SQL Server.

The installation of Visual Studio 2012 and 2013 includes LocalDB 2012 and you can download the SQL Express 2014 LocalDb edition directly from the MSDN.

The SqlLocalDB Utility help you to manage your LocalDb instances. The following command will list you all LocalDB versions installed on your computer:

SqlLocalDb-versions

And the existing LocalDB instances owned by the current user and all shared LocalDB instances:

SqlLocalDb-instances

To check on the status and other details about an instance, you can run:

SqlLocalDB-info

CodeFluent Entities Build 769 introduced the support of Microsoft SQL Server 2014 and gives you the opportunity to use SQL Server LocalDB (2012 and 2014) as your persistence server of your CodeFluent Entities application.

The SQL Server Producers allows you to generate your database layer on an SQL Server LocalDB instance:

SQL Server Producer LocalDb

Just build your model and connect to your LocalDb instance with SQL Server Management Studio or the Visual Studio Server Explorer. You can see that a new database has been created with the named you specified in the Connection String, and populated with the tables automatically inferred from your model as well as instances:

Server Explorer

Happy LocalDB-ing!

The R&D Team

What’s new with CodeFluent Entities 769?

April 7, 2014 Leave a comment

As with any previous release, CodeFluent Entities 769 includes numerous new features and bug fixes.

Keeping ahead with the latest technologies is our priority and we are extremely thrilled to announce that CodeFluent Entities now supports Microsoft SQL Server 2014.

Since the beginning of the year, CodeFluent Entities offers a new licensing model. You can try CodeFluent Entities by downloading the 30-day trial version which is unlimited in features and model size. The tool is also available in four registred editions: Personal (free for non-commercial use and limited to 50 entities), Professional, Enterprise and Ultimate.

But what else?

JsonUtilities

We made some greats improvements to the JsonUtilities:

Pretty-print Json

string json = @"{""name"": ""John Doe"",""age"": 35}";
var obj = JsonUtilities.Deserialize(json);
JsonUtilities.WriteFormatted(Console.Out, obj);

Will output:

{
  "name": "John Doe",
  "age": 35
}

Deserialization

JsonUtilities now supports XmlAttribute:

string json = @"{""name"": ""John Doe"",""age"": 35}";
var person = JsonUtilities.Deserialize<Person>(json, 
                 JsonSerializationOptions.UseXmlAttribute);

public class Person
{
    [XmlAttribute("name")]
    public string Name { get; set; }
 
    [XmlAttribute("age")]
    public int Age { get; set; }
}

We also added some automatic conversions between types. For example you can deserialize json to a Dictionary<string, decimal>. Values will be converted to decimal if possible. Internally this will use the ConvertUtilities class which I hope you are already using.

Relation UX improvements

We introduce a new ribbon item named Add relation. Of course the concept of relation already exists in the product, but we constantly strive to improve our product. Now there is an item in the ribbon to add a relation. The new form allows to select entities and properties and even create a new property if needed:

Add relations

When you don’t know which option to select (one-to-one, one-to-many, many-to-one or many-to-many) to define the type of relation, just read the description:

Relation Type

In this example, only the second sentence (one Customer has many Orders) is logical. If none of them seem valid, it may indicate that properties are badly named.

Microsoft SQL Server 2014 support

The SQL Server Producer now supports Microsoft SQL Server 2014. Christmas comes early for CodeFluent Entities users :).

SQL Server 2014

A new way to Activate

Last but not least, we provide a new screen to activate the product. With this new form, you can get your license key by connecting to your account directly in Microsoft Visual Studio:

Registered edition

If you are not a current customer, you can always download our free evaluation and see it in action.

Remember that you can follow the latest new features and bug fixes of CodeFluent Entities subscribing to this RSS Feed.

If you have any questions or comments, feel free to share them below.

Happy downloading!

The R&D Team

Fetch performance of CodeFluent Entities compared to others

March 27, 2014 Leave a comment

CodeFluent Entities has a great way to extend or modify generated code. This can be done through Custom Producers, Sub-Producers or Aspects. In fact, it can happen that you will meet very special requirements during your project, the kind of requirements that will involve some customizations to all of your code. Of course you don’t want to do this job manually by modifying each property or method in your project. Instead you can change the generated code to fit your expectations by using aspect. For a first introduction to aspect development in CodeFluent Entities please visit our blog.

In our situation, after reading Frans Bouma’s blog on benchmark of several ORMs of the .Net platform (Entity Framework, NHibernate, LLBLGen Pro, Linq To Sql and more), we wanted to integrate CodeFluent Entities to the benchmarks he made.

Integrating CodeFluent Entities to the benchmark project

First we downloaded the project from its GitHub repository.

After opening the RawBencher solution we created a CodeFluent Entities Model project:

CodeFluent Entities Project

Also we created a class library project to hold the code generated by CodeFluent Entities:

Class Library Project

Then we added one Business Object Model Producer and one SQL Server Producer to this project:

Solution Explorer

Here is the configuration for each producer:

Business Object Model

Business Object Model

Microsoft SQL Server

Microsoft SQL Server

Then we imported the AdventureWorks database to our CodeFluent Entities model:

Import

Then select the Microsoft SQL Server importer:

SQL Server Importer

Finally, set the connection string as pictured below:

SQL Server importer configuration

Once the import from database was done we built the model project to generate C# code and database stored procedures. After this, for the final step we added a bencher class to call CodeFluent Entities generated code. We basically reproduced the same schema as the existing ones for other ORMs already set up in this benchmarking project.

We are now ready to start the benchmarking!

Running the benchmark and analyzing results

We ran the project in release mode and we got the following results:

Non-change tracking fetches, set fetches (10 runs), no caching:

  1. Handcoded materializer using DbDataReader: 214,63ms
  2. PetaPoco Fast v4.0.3: 285,50ms
  3. Dapper: 306,25ms
  4. Linq to SQL v4: 318,50ms
  5. PetaPoco v4.0.3: 355,00ms
  6. Entity Framework v6: 362,13ms
  7. CodeFluent Entities 551,00ms
  8. ServiceStack OrmList v4.0.9.0: 555,75ms
  9. LLBLGen Pro v4.1.0.0, typed view: 585,00ms
  10. Oak.DynamicDb using dynamic Dto class: 902,50ms

Non-change tracking individual fetches (100 elements, 10 runs), no caching:

  1. CodeFluent Entities: 0,18ms
  2. DataTable, using DbDataAdapter: 0,37ms
  3. Oak.DynamicDb using dynamic Dto class: 0,40ms
  4. LLBLGen Pro v4.1.0.0: 0,44ms
  5. Telerik DataAccess/OpenAccess Fluent v4.0.3: 0,50ms
  6. Telerik DataAccess/OpenAccess Domain v4.0.3: 0,50ms
  7. NHibernate v3.3.1.4000: 0,68ms
  8. Entity Framework v6: 1,85ms
  9. Linq to Sql v4: 2,89ms

We focused only on non-change Tracking mode because it is the one that matches CodeFluent Entities features.

We can see that CodeFluent Entities is ranked at the first place for single fetch operations. Also we can see that it is ranked 7th for the multiple fetch operations.

Of course each ORM offers different features and because of that some of them can have a more naïve approach than others which will check data type conversion for instance or check cache during the fetch. This will lead to a speed difference in execution time.

For example if you compare a SQL hand coded query against any ORM among the ones available in .NET, hand coded query will be for sure faster. In our case we can explain why CodeFluent Entities generated code is taking more time in the multiple set fetch operation. Basically the code generated is doing some additional operations that we can get rid of in this particular scenario:

For instance in this LoadAll method we do not need to check if an element is already contained in the inner list so we should remove the check:

LoadAll

Another example is the ReadReacord method, in this case we do not need to test if the reader is null or not, neither the options and since the type are secure and primitive types we do not need to use the CodeFluent Persistence GetReader methods a simple reader.GetIn32 or reader.GetDate can be used depending on the type:

ReadRecord

After making these changes we can run the benchmark again to see what changed!

Running the benchmark with the adapted code

Here are the new result after code adaptation:

Non-change tracking fetches, set fetches (10 runs), no caching:

  1. Handcoded materializer using DbDataReader: 214,63ms
  2. CodeFluent Entities 273,25ms
  3. PetaPoco Fast v4.0.3: 285,50ms
  4. Dapper: 306,25ms
  5. Linq to SQL v4: 318,50ms
  6. PetaPoco v4.0.3: 355,00ms
  7. Entity Framework v6: 362,13ms
  8. ServiceStack OrmList v4.0.9.0: 555,75ms
  9. LLBLGen Pro v4.1.0.0, typed view: 585,00ms
  10. Oak.DynamicDb using dynamic Dto class: 902,50ms

After these changes CodeFluent Entities is now ranked at the 2nd place just after the hand coded query!

I will now show you how to make these custom changes more generic to apply them to the entire project for instance.

Understanding CodeFluent Entities Aspects

In CodeFluent Entities, the code generation process is model-first and continuous: from your declarative model, a meta-model will be inferred which code generators will then translate into code.

AspectAspects introduce a new notion allowing you to plug into this process. Using aspects you’ll be able to work on this in-memory representation of the model, before anything is produced and dynamically add/remove/modify elements in your model: this is what we call dynamic modeling. In a nutshell, in CodeFluent Entities, dynamic modeling is materialized as aspects and it allows developers to inject extra-behaviors in models.

You can easily see what the inferred model contains by selecting the option “View Inferred Model” on your project:

View Inferred Model

Then you can get details about any method or property of your code, for instance in our case the LoadAll method of the SalesOrderHeader entity:

Inferred Model

This inferred model will be used by the Business Object Model Producer we configured before to generate our code.

When you build your project, the enabled producers are instantiated to work on your model. With CodeFluent Entities you can interact at moment of the code production. For example you can change on the fly the behavior of your CodeDom producer (aka Business Object Model Producer) by accessing its instance:

CodeDomProducer codeDomProducer = Project.Producers.GetProducerInstance<CodeDomProducer>();

codeDomProducer.CodeDomProduction += (sender, e) =>
{
    //write your code here ...
}

And then you can manipulate this codeDomProducer object to change the body of your methods or do any other change to the generated code. In our case this will be very helpful to change the body of the LoadAll and ReadRecord methods.

Making code adaptation using aspects

In fact the changes we made can be reproduced automatically among the code by using a custom aspect that will interact with the Business Object Model Producer on the fly.

You can download the FastReader.xml file that contains the aspect we developed to make our customizations generic.

To remind you here is what the LoadAll method looked like before using the aspect:

LoadAll Before

Here is the new version of the same function:

LoadAll After

Another example is the ReadRecord method; here is what it was like before using the aspect:

Read Record Before

After enabling the aspect the method is replaced by a new one with the name FastReadRecord:

Read Record After

CodeFluent Entities offers many ways to customize the code generation and aspects is only one way among the others. In fact code customization can also be done by using sub-producers or patch-producers. Each technic has its pros and cons and in our case aspects was the best way to reach our goal. If you want to read more about sub-producers or patch producers please visit our blog here and here.

I hope this article helped you to figure out the flexibility of CodeFluent Entities.

Feel free to download and use the FastReader.xml aspect if you need.
Moreover, the full source code is available on our GitHub Profile.

Happy Adapting!

The R&D team.

Define your own CodeFluent Entities Keyboard shortcuts

March 24, 2014 1 comment

Some of you ask us if we can create your own keyboard shortcuts with CodeFluent Entities. Yes, of course. You can assign a shortcut to add an entity or a property into your CodeFluent Entities surface thanks to Visual Studio Keyboard Commands. While we do not set default keyboard shortcuts, you can define your own:

  • Open the Visual Studio Options dialog (Tools / Options / Environment / Keyboard)
  • Select the desired command (search command containing CodeFluent)
  • Define the keyboard shortcut

shortcut

Below is a list of the most common CodeFluent commands:

  • Add Entity: OtherContextMenus.CodeFluentSurface.Add.Entity
  • Add Property: OtherContextMenus.CodeFluentSurface.Add.Property
  • Add Rule: OtherContextMenus.CodeFluentSurface.Add.Rule
  • Add Instance: OtherContextMenus.CodeFluentSurface.Add.Instance
  • Model Search: OtherContextMenus.CodeFluentSurface.ModelSearch (my favorite!)
  • Arrange Surface: OtherContextMenus.CodeFluentSurface.Arrange.Surface

Happy shortcutting, The R&D Team

Create your model from the Visual Studio Server Explorer

March 13, 2014 1 comment

CodeFluent Entities provides model importer capabilities which allows you to create model from an existing database or from an existing model.

In the past, we covered lots of topics about these features but do you know you can also create your model from the Visual Studio Server Explorer?

Server Explorer

Open the server explorer and configure your database connection.

Wizard

Then select tables and drag and drop them onto your CodeFluent Entities Surface. The importer wizard opens with preconfigured values. Click the “next” button and your entities will be created.

Model importer

Don’t forget that CodeFluent Entities is shipped with the following importers:

  • Microsoft SQL Server importer,
  • Microsoft SQL Server CE importer,
  • Microsoft Access importer,
  • Oracle Database importer,
  • OLE DB Importer,
  • PostgreSQL importer,
  • MySQL importer,
  • SQLite importer
  • Enterprise Architect importer,
  • XMI importer,
  • Entity Framework importer.

More information about CodeFluent Entities Importer Wizard.

Happy importing!

The R&D team

Multi-database deployment with PowerShell and the Pivot Script Runner – Part 2

March 6, 2014 Leave a comment

In Part 1 of this article, we looked at using the PowerShell strengths to automate the process of updating several databases through the PivotRunner tool.

Now, we want to go further and create a PowerShell command, better known as a Cmdlet.

Build the Cmdlet

A Cmdlet can be built directly in a Powershell script, or through the .NET Framework. We need to inherit from System.Management.Automation.Cmdlet and define its naming attributes therefor.

By agreement, the name of a Cmdlet consists of a verb, followed by a dash and a name (e.g: Get-ChildItem andAdd-PSSnapIn):

using System.Management.Automation;

namespace CodeFluentEntitiesCmdlet
{
    [Cmdlet(VerbsData.Update, "CFEDatabase", SupportsShouldProcess = true, 
            ConfirmImpact = ConfirmImpact.High)]
    public class UpdateCFEDatabase : Cmdlet
    {
    }
}

Here, the Cmdlet’s name will be Update-CFEDatabase.

Use the following PowerShell command: Copy ([PSObject].Assembly.Location) C:\MyDllPath to find the System.Management.Automation library

SupportsShouldProcess and ConfirmImpact attributes allow the Cmdlet to use the PowerShell Requesting Confirmation feature.

The Cmdlet abstract class includes a fairly advanced command parameters engine to define and manage parameters:

[Parameter(Mandatory = true)]
public string ConnectionString { get; set; }

[Parameter(Mandatory = true)]
public string PivotFilePath { get; set; }

The Mandatory term is used to warn the command parameters engine of whether or not a parameter is required.

Cmdlet also exposes some methods which can be overriden. These pipeline methods allow the cmdlet to perform pre-processing operations, input processing operations, and post-processing operations.

Here, we’ll just override the ProcessRecord method:

protected override void ProcessRecord()
{
  // Process logic code
}

Then, we need to use the PivotRunner which is located in the CodeFluent.Runtime.Database assembly.

The tool takes the connection string and the pivot script producer output file as parameters:

using CodeFluent.Runtime;
using CodeFluent.Runtime.Database.Management.SqlServer;

private void UpdateDatabase()
{
    try
    {
        PivotRunner runner = new PivotRunner(PivotFilePath);

        runner.ConnectionString = ConnectionString;

        if (!runner.Database.Exists)
        {
            WriteObject("Error: The ConnectionString parameter does not lead to an existing database!");
            return;
        }
        runner.Run();
    }
    catch (Exception e)
    {
        WriteObject("An exception has been thrown during the update process: " + e.Message);
    }
}

Do not forget to reference CodeFluent.Runtime.dll and CodeFluent.Runtime.Database.dll!

Moreover, we can recover the PivotRunner output (internal logs) by providing an IServiceHost implementation:

public class CmdletLogger : IServiceHost
{
    private Cmdlet _cmdLet;

    public CmdletLogger(Cmdlet cmdlet)
    { 
        _cmdLet = cmdlet;
    }

    public void Log(object value)
    {
        _cmdLet.WriteObject(value);
    }
}

runner.Logger = new CmdletLogger(this);
runner.Run();

Powershell integration

The Cmdlet is now finished! :)

Now we’ll see how to call it from Powershell! Here, we have several options, but we shall see the PSSnapIn one.

The “Writing a Windows PowerShell Snap-in” article shows that a PSSnapIn is mostly a descriptive object which inherits from System.Configuration.Install.Installer and is used to register all the cmdlets and providers in an assembly.

So, let’s implement our Powershell snap-in:

using System.ComponentModel;
using System.Management.Automation;

namespace CodeFluentEntitiesCmdlet
{
    [RunInstaller(true)]
    public class CodeFluentEntitiesCmdletSnapin01 : PSSnapIn
    {
        public CodeFluentEntitiesCmdletSnapin01()
            : base() { }

        public override string Name
        {
            get { return ((object)this).GetType().Name; }
        }

        public override string Vendor
        {
            get { return "SoftFluent"; }
        }

        public override string VendorResource
        {
            get { return string.Format("{0},{1}", Name, Vendor); }
        }

        public override string Description
        {
            get { return "This is a PowerShell snap-in that includes the Update-CFEDatabase cmdlet."; }
        }

        public override string DescriptionResource
        {
            get { return string.Format("{0},{1}", Name, Description); }
        }
    }
}

Then, we build our solution which contains our Cmdlet and the PSSnapIn and finally register the built library thinks to the InstallUtil.exe (located in the installation folder of the .NET Framework):

Administrator rights are required.

Administrator rights are required.

By using the “Get-PSSnapIn –Registered” Powershell command, we can observe that our PSSnapIn is well registered. This component can now be used into your Powershell environment:

Get-PSSnapIn–Registered

The “Add-PSSnapIn” command enables us to use our Cmdlet into the current session of Powershell.

As result, we can update our previously built Powershell script:

param([string[]]$Hosts, [string]$PivotFilePath, [switch]$Confirm = $true)

Add-PSSnapin CodeFluentEntitiesCmdletSnapin01

if ($Hosts -eq $null -or [string]::IsNullOrWhiteSpace($PivotFilePath))
{
    Write-Error "Syntax: .\UpdateDatabase.ps1 -Hosts Host1[, Host2, ...] -PivotFilePath PivotFilePath"
    break
}

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

Write-Host "-========- Script started -========-"

$Hosts | foreach {
    $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $_

    $online_databases = $srv.Databases | where { $_.Status -eq 1 -and $_.Name.StartsWith("PivotTest_") }
    
    if ($online_databases.Count -eq 0)
    {
        Write-Error "No database found"
        break
    }

    Write-Host "Database list:"
    $online_databases | foreach { Write-Host $_.Name }

    [string]$baseConnectionString = "$($srv.ConnectionContext.ConnectionString);database="
    $online_databases | foreach {
        Update-CFDatabase -ConnectionString "$($baseConnectionString)$($_.Name)" -PivotFilePath $PivotFilePath -Confirm:$Confirm
    }
}

Write-Host "-========-  Script ended  -========-"

We can now simply deploy all changes we’ve recently made on our databases thanks to the Cmdlet and PivotRunner components.

The source code is available for download.

Happy PowerShelling !

The R&D team

Follow

Get every new post delivered to your Inbox.

Join 41 other followers