Quantcast
Viewing all 125 articles
Browse latest View live

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

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! Image may be NSFW.
Clik here to view.
:)

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):

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Create your model from the Visual Studio Server Explorer

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?

Image may be NSFW.
Clik here to view.
Server Explorer

Open the server explorer and configure your database connection.

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
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


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Define your own CodeFluent Entities Keyboard shortcuts

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

Image may be NSFW.
Clik here to view.
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


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Fetch performance of CodeFluent Entities compared to others

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, 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:

Image may be NSFW.
Clik here to view.
CodeFluent Entities Project

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

Image may be NSFW.
Clik here to view.
Class Library Project

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

Image may be NSFW.
Clik here to view.
Solution Explorer

Here is the configuration for each producer:

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

Image may be NSFW.
Clik here to view.
Import

Then select the Microsoft SQL Server importer:

Image may be NSFW.
Clik here to view.
SQL Server Importer

Finally, set the connection string as pictured below:

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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.

Image may be NSFW.
Clik here to view.
Aspect
Aspects 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:

Image may be NSFW.
Clik here to view.
View Inferred Model

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

Image may be NSFW.
Clik here to view.
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 any do 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:

Image may be NSFW.
Clik here to view.
LoadAll Before

Here is the new version of the same function:

Image may be NSFW.
Clik here to view.
LoadAll After

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

Image may be NSFW.
Clik here to view.
Read Record Before

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

Image may be NSFW.
Clik here to view.
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.

Happy Adapting!

The R&D team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

What’s new with CodeFluent Entities 769?

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:

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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 :).

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Using LocalDB with CodeFluent Entities

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:

Image may be NSFW.
Clik here to view.
SqlLocalDb-versions

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

Image may be NSFW.
Clik here to view.
SqlLocalDb-instances

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

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
Server Explorer

Happy LocalDB-ing!

The R&D Team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Views, Auto-Lightweight and the Modeler

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:

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to view.
ribbon - add view

You should see the following window:

Image may be NSFW.
Clik here to view.
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).

Image may be NSFW.
Clik here to view.
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:

Image may be NSFW.
Clik here to 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:

Image may be NSFW.
Clik here to view.
BOM

 

Happy View-ing!

The R&D team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

ASP.NET Identity v2 and CodeFluent Entities

A few weeks ago, we wrote about ASP.NET Identity v1 and CodeFluent Entities. The ASP.NET Identity system is designed to replace the previous ASP.NET Membership and Simple Membership systems. It includes profile support, OAuth integration, works with OWIN and is included with the ASP.NET templates shipped with Visual Studio 2013.

ASP.NET Identity 2.0 was released last month so today we’ll see how to use it with CodeFluent Entities.

What’s new?

One of the drawbacks of the first release was that the primary key must be of type “string”. This new version allows to use any type as primary key. To do so, interfaces now have a second generic parameter, “TKey”, to specify the type of the primary key (int, Guid, etc.).
They also introduce new interfaces:

  • IUserLockoutStore<User, TKey>: allow to lock-out an account after a determined failed connection attempts
  • IUserEmailStore<User, TKey>: allow to store an email address and to confirm it (by sending an email for example)
  • IUserPhoneNumberStore<User, TKey>: allow to store a phone number and to confirm it (by sending a SMS for example)
  • IUserTwoFactorStore<User, TKey>: enable two-factor authentification (for example login/password and a token sent by email or SMS)

The implementation

Here’s the CodeFluent Entities Model to support those interfaces:

Image may be NSFW.
Clik here to view.
model

You’ll note that our model contains:

  • Implementation rules. We don’t even have to add partial class, everything is in the model
  • validation rules (EmailValidation) to ensure email address is valid
  • Unique contraints (UserName, Email, Role Name)

As for ASP.NET Identity v1, each method is only one or two lines of code:

public class UserStore :
    IUserStore<User, Guid>,
    IUserPasswordStore<User, Guid>,
    IUserSecurityStampStore<User, Guid>,
    IUserRoleStore<User, Guid>,
    IUserLoginStore<User, Guid>,
    IUserClaimStore<User, Guid>
{
    public Task<User> FindAsync(UserLoginInfo login)
    {
        return Task.FromResult(User.LoadByProviderKey(login.ProviderKey));
    }

    public Task ResetAccessFailedCountAsync(User user)
    {
        user.AccessFailedCount = 0;
        return Task.FromResult(0);
    }

    public Task<int> GetAccessFailedCountAsync(User user)
    {
        return Task.FromResult(user.AccessFailedCount);
    }

    // and so on
}

The full implementation is available on our GitHub repository.

The R&D team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

CodeFluent Entities and Fetch Performance Benchmark

Two month ago, we heard about a benchmark that fetch performance of .NET Data Access Tools and ORMs. This benchmark has been introduced by Frans Bouma in this following blog post.

We started by creating a simple implementation with CodeFluent Entities and then analyze the result. Thanks to the flexibility of our product, it seems natural for us to provide an optimized but generic implementation. This means that the code is still generated.

CodeFluent Entities provides dynamics modeling features like “Aspects” to implement application-wide behaviors. 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.

That’s what gave us the idea of developing a new Aspect called “FasterReader” to reduce the number of data type conversion.
We explained our approach in a blog Post named “Fetch performance of CodeFluent Entities compared to others“.
This Aspect is generic (not “benchmark dedicated”) and can be used in any kind of model.

Once the article is published, we sent a Pull Request on the RawDataAccessBencher GitHub repository to submit the CodeFluent Entities implementation:

Image may be NSFW.
Clik here to view.
pullrequest

The initial commit is available here and includes the CodeFluent Entities Model with the SoftFluent.FasterReader.xml aspect.

We also modified the README.MD file with the following changes:

CodeFluent Entities is a Model-First code generator. CodeFluent Entities will access database using automatically generated stored procedures. For more information about CodeFluent Entities and this benchmark: http://blog.codefluententities.com/2014/03/27/fetch-performance-of-codefluent-entities/.

Of course, this readme includes a link to our blog post that explains the approach.

Then, Frans Bouma asked us to make some understandable changes before merging the code:

  • remove links to CodeFluent Entities website
  • remove a large file with all the procs
  • remove useless files in various folders regarding TFS, scc, resx, .user, etc.
  • remove the CodeFluent Entities Model

For your information about the last statement: we have chosen to add this project to let users generate again the code and to be completely transparent.

Of course, feedbacks has been considered a few days later:

Image may be NSFW.
Clik here to view.
pull_request_changes

At the end of April, the CodeFluent Entities implementation was finally merged:
Image may be NSFW.
Clik here to view.
merge

But… the 5th of May, Frans Bouma removed our code:

Image may be NSFW.
Clik here to view.
CFE_Removal

With the following reason:

The CodeFluent code was removed as it kept the connection open during
individual fetches which therefore turned out to be faster than everyone
else. This was hard to find, and therefore a form of cheating.

By default, CodeFluent Entities opens one connection per thread but we attempted to explain that we can change this behavior. His reaction was the following:

Image may be NSFW.
Clik here to view.
Wont_Merge

You will find below the result with the custom configuration:

ADO.NET’s Connection Pooling keeps all connections open so that doesn’t change a lot of things so we don’t change this setting:

Image may be NSFW.
Clik here to view.
Results

We understand the way out products connects to a database out-of-the-box is not ok for Mr Bouma, but we don’t really understand why he went so mad about this, treating us cheaters. Again, this is how it works out-the-box. Why should we investigate why our product – based on stored procedures – is faster than others? Mr Bouma could have discussed with us – like he did in the beginning of the whole process – and we could have changed this default behavior happily to comply with his rules. We were not trying to make a fool of him or to hide crucial information from him, just compete with other products. It’s a sad way of doing thing, and in the end, this behavior does not change anything to the numbers.

Moreover, if you want to check or test our implementation (the Model is included), please visit the following GitHub repository.

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

ASP.NET Identity Producer

CodeFluent Entities can generate out of the box Membership, Role & Profiles Providers. Recently Microsoft release a new Identity system: ASP.NET Identity. As it seems to be more and more use, we decided to write a new producer to support this new Identity system.

First of all, the source code of the producer is available on our GitHub repository.

The producer does two things:

  • It assists you in creating ASP.NET Identity entities (User, Role, Claim, Login)
  • It generates the UserStore and RoleStore and implements IUser and IRole

How to install

  • Download the code from GitHub and compile it. A compiled version is also available here
  • Copy the output “CodeFluent.Producers.AspNetIdentity.dll” to “C:\Program Files (x86)\SoftFluent\CodeFluent\Modeler”
  • (Optional) To integrate the producer to the graphical modeler copy or merge “custom.config” to “%APPDATA%\CodeFluent.Modeler.Design\custom.config”

How to use it

  1. Create a CodeFluent Entities project
  2. Add a Business Object Model (BOM) producer
  3. Add this new producer (Security -> Asp.Net Identity). Note: The target path has no effect. The target path will be “Target Path of the BOM Producer\Web\Security(User|Role)Store”
  4. Image may be NSFW.
    Clik here to view.
    Add New Producer

  5. Right click on the producer and click the “Create Identity Entities” menu item
  6. Image may be NSFW.
    Clik here to view.
    Create Identity Entities

  7. Select necessary entities (User, Role, Claim, Login) and the destination namespace
  8. Image may be NSFW.
    Clik here to view.
    ASP.NET Identity

  9. Customize entities. For example you can remove “Password” property if you don’t need it
  10. Image may be NSFW.
    Clik here to view.
    ASP.NET Identity Model

  11. Generate and use the code :)

Note: you must add the “Microsoft.AspNet.Identity.Core” Nuget package before compiling the generated code (but this is obvious).

CodeFluent Entities is very extensible. As we try to show in this article, one way of extending it is by using producers. Of course the main goal of producers is to generate code. But they can also interact with the model at design time thanks to the CodeFluent Entities API.

Please share your feedbacks about this producer and don’t hesitate to make some suggestion.

Happy coding!

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Exploring the CodeFluent Runtime: Authenticode

Today, on the series “Exploring the CodeFluent Runtime” we’re going to explore how to sign an application with Authenticode method.

Microsoft Authenticode, which is based on industry standards, allows developers to include information about themselves and their code with their programs through the use of digital signatures. Authenticode allows software vendors to sign:

  • .cab files
  • .cat files
  • .ctl files
  • .dll files
  • .exe files
  • .ocx files

First we need a certificate that allows Code Signing. If you haven’t one, let’s create a self-signed one:

REM May change depending of your installed Windows SDK
cd "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin"

REM Generate the root certificate
.\makecert.exe -r -pe -n "CN=Sample.CA" -ss CA -sr CurrentUser -a sha1 -cy authority -sky signature -sv d:\Sample.CA.pvk d:\Sample.CA.cer

REM Add the Root certificate to the user store
certutil.exe -user -addstore Root d:\Sample.CA.cer

REM Create the certificate for code signing
.\makecert.exe -pe -n "CN=Sample.CodeSigning" -eku "1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13" -a sha1 -cy end -sky signature -ic d:\Sample.CA.cer -iv d:\Sample.CA.pvk -sv d:\Sample.CodeSigning.pvk d:\Sample.CodeSigning.cer

REM Convert to certificate to pfx file format
.\pvk2pfx.exe -pvk d:\Sample.CodeSigning.pvk -spc d:\Sample.CodeSigning.cer -pfx d:\Sample.CodeSigning.pfx

We can now sign a file. Add the “CodeFluent.Runtime.dll” reference and use the following code:

X509Certificate2 certificate = Authenticode.FindSuitableCertificate();
Authenticode.SignFile(certificate, "sample.exe", null, "SoftFluent");

The first line find a valid certificate for code signing in the user certificate store. If none is found, it returns null.

The second line signs the file. You have to indicate:

  • the certificate to use
  • the file to sign
  • the timestamp server
  • the display name

If you look at the file properties, you’ll find a new tab ‘Digital Signature’ which contains details about the signer.

Image may be NSFW.
Clik here to view.
Digital Signature

Please note:

  • you don’t need to provide a password, nor a path to the certificate => Generic, simple and secure
  • the method doesn’t rely on the Windows SDK, so you don’t have to bother with SDK path => much simplier :)
  • it’s a DLL so it’s very easy to integrate in your application

Additionally you’ll find two methods:

//Determines whether the specified certificate can sign code.
public static bool CanSignCode(X509Certificate2 certificate)

//Determines whether the specified file is signed using authenticode.
public static bool IsSigned(string filePath)

Happy authenticoding,

The R&D team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

How to compress or extract CAB archives?

Today, on the series “Exploring the CodeFluent Runtime” we’re going to explore how to compress or extract CAB archives.

Cabinet (or CAB) is an archive file format for Windows that supports lossless data compression and embedded digital certificates used for maintaining archive integrity. Cabinet files have .cab file name extensions.

First, we have to add a reference to “CodeFluent.Runtime.dll”. The namespace we’ll use is “CodeFluent.Runtime.Compression”, the same as for compressing or extracting ZIP file.

One of the biggest advantage compare to ZIP file, is that it can work in memory or by using the file system as it uses Stream. The second point is you don’t have to add a native DLL: it’s all managed. While it’s less efficient than a native implementation, it’s easier to deploy.

Here’s an example to compress one file and a directory:

using (MemoryStream stream = new MemoryStream())
{
    using (CabFile file = new CodeFluent.Runtime.Compression.CabFile(stream, CabFileMode.Compress))
    {
        file.CompressionLevel = CabCompressionLevel.Maximum;
        file.AddEntry("File.txt");
        file.AddDirectory("Sample");
    }
}

Extracting files is as simple as compressing.

using (CabFile file = new CodeFluent.Runtime.Compression.CabFile(stream, CabFileMode.Decompress))
{
    file.EntryExtracted += (sender, e) =>
    {
        //e.Entry.Name
        //e.Entry.OutputStream
        //e.Entry.Bytes
        //e.Entry.Size
        //e.Entry.LastWriteTime
        //e.Entry.Tag
    };
    file.ExtractEntries();
}

And there’s more…

We add a method to determine the compression method (CAB or ZIP) of a file. This method can use the file extension if available or the first four bytes of the file (i.e. magic number):

CompressionUtilities.SniffFileFormat("test.cab", useExtensionAsHint: true) == CompressionFileFormat.Cab

And that’s not all, in the previous article, we wrote about how to sign a file by using the runtime. CAB file can also be signed, so let sign it:

X509Certificate2 certificate = Authenticode.FindSuitableCertificate(); 
Authenticode.SignFile(certificate, "test.cab", null, "SoftFluent");

Image may be NSFW.
Clik here to view.
Cab properties

Happy compressing and signing,

The R&D Team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

What’s new with CodeFluent Entities 770?

CodeFluent Entities provides several producers allowing you to generate your persistence layer on several database engines. The previous build includes numerous new features such as the support of Microsoft SQL Server 2014.

Image may be NSFW.
Clik here to view.
MariaDB

Once again, the CodeFluent Entities 2014 – Build 770 introduces the support of a new relational database management system: MariaDB. It is an a community-developed fork of MySQL created in 2009 which includes some new improvements that (some claim) make it better than MySQL.

Our latest build improves the MySQL producer and now supports MariaDB. The producer supports MariaDB 5.1 and above and relies on the MySQL Connector for .NET version 6.5.4.0. The producer will try to load dynamically the MySql.Data assembly so you might need to copy the MySql.Data.dll in your CodeFluent Entities installation directory if you don’t want to register the MySQL Connector in the GAC.

Image may be NSFW.
Clik here to view.
MariaDB Producer

Below, you can see a generated MariaDB according to the ContactManager CodeFluent Entities Sample Model:

Image may be NSFW.
Clik here to view.
MariaDB Generated

Note that you might need to reference the CodeFluent Runtime.dll and CodeFluent.Runtime.MySQL.dll if you generate the C# Business Object Layer (CodeDom Producer).

CodeFluent Entities 2014 – Build 770 also includes numerous bug fixes and new features. The full list is available here. Yesterday, we published a post that talk about a new feature: How to compress or extract CAB archives. Feel free to read it!

If you have any questions or comments, please share them below.

Happy downloading!

The R&D Team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Thinktecture IdentityServer and CodeFluent Entities

Thinktecture IdentityServer v3 is a .NET-based open source implementation of an OpenID Connect provider and OAuth2 authorization server. Today, we are going to explain how to use this light-weight security token service with CodeFluent Entities.

To save time, we provide on our GitHub repository a sample of IdentityServer with CodeFluent Entities but… how to install and use it ? First, you need to get and install the ASP.NET Identity Producer. There’s nothing to it. Download the server source code from our GitHub and compile it. In a few words, this producer will assist you in creating ASP.NET Identity entities (User, Role, Claim, Login).

Now, open the solution and set “SoftFluent.Samples.Thinktecture.IdentityServer.Host” as Startup Project. Don’t forget to set “Don’t open a page option. Wait for a request from an external application” in Host project and start it:

 

Image may be NSFW.
Clik here to view.
Configuration

Then, download the client source code from Thinktecture’s GitHub and compile it. Set “JavaScript Implicit Client” as Startup Project  and start it.

You should obtain this page:

Image may be NSFW.
Clik here to view.
Thinktecture Sample 1
Then, click on “Login With Profile” :

Image may be NSFW.
Clik here to view.
Thinktecture Sample 2
We choose to show you external login with Google so click the “Google” butto. Enter your credentials and allow the permissions you wish to grant. At this point IdentityServer will be save our new user in database through ASP.NET Identity.

If we take a look at the User’s table, we should find a new entry:

Image may be NSFW.
Clik here to view.
Thinktecture Sample 3

Finally, you receive Token and information about your account :

Image may be NSFW.
Clik here to view.
Thinktecture Sample 4

IdentityServer is provided without UI and administration tool but recently Thinktecture offers a new project called IdentityManager for developers and administrators to manage the identity information for users of their applications.

Happy Authenticating,

The R&D Team

 

 


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Template Engine and Extensibility

A few time ago, we presented the CodeFluent Runtime’s Template Engine. Today we’ll see how to extend it to change a little bit its syntax and to add custom keywords.

Here is a simple template:

<% var now = new Date(); %>
Hello <%= Name %>!
It's <%= now.toDateString() %>
Cheers,
<%= CurrentUser %>

To use the standard Template Engine, we use this following code:

Template template = new Template();

template.LoadText(templateText, "Name", "CurrentUser");
string templateResult = template.Run(new Dictionary<string, object>
{
     { "Name", "Jone Doe" }, 
     { "CurrentUser", Environment.UserName }
}); 

Console.WriteLine(templateResult);

This will output:

Hello Jone Doe!
It’s Thu Mar 6 2014
Cheers,
The R&D Team

After some customization, the template will be more readable:

Hello {{Name}}!
It's {{Now:yyyy/MM/dd}}
Cheers,
{{CurrentUser}}

Before extending the template engine we have to understand how it works. The engine is composed of 4 classes:

  • Template: contains the configuration such as delimitation tokens and methods to load and run the template
  • ParsedTemplate: contains parameters and code or text blocks extracted while parsing the template
  • ParsedBlock / CodeBlock: contains block text and a method to transform it into JavaScript code
  • Output: contains methods to write into the output file

Let’s use the first sample to understand. First, it will parse the template and extract the following blocks:

  • CodeBlock: var now = new Date();
  • TextBlock: Hello
  • CodeBlock: = Name
  • TextBlock: It’s
  • CodeBlock: = now.toDateString()

Then it will transform blocks into JavaScript instructions. These Text blocks are transformed into “Output.Write(block content)”. When code block starts with “=” it will generate “Output.Write(parameterName)”, otherwise the JavaScript is written as is.

The generated JavaScript is:

ActiveXObject=null;
function __run(Output, Name, CurrentUser) {
 var now = new Date(); 
Output.WriteBlock(1);
Output.Write( Name );
Output.WriteBlock(3);
Output.Write( now.toDateString() );
Output.WriteBlock(5);
Output.Write( CurrentUser );
}

Now we understand the template engine, let’s extend it.
We would like to change to {{ and }}. To do so, we extend the Template class.

public class CustomTemplate : Template
{
    public CustomTemplate()
    {
        StartToken = "{{";
        EndToken = "}}";
    }
}

We also need to create a new CodeBlock to parse the content and write the appropriate JavaScript code:

public class CustomCodeBlock : CodeBlock
{
    public string Format { get; private set; }
    public string ArgumentName { get; private set; }

    public CustomCodeBlock(string code, int creationIndex)
        : base(code, creationIndex)
    {
        Parse(code);
    }

    private void Parse(string text)
    {
        if (text == null || text.StartsWith("="))
            return;

        string argumentName = text;
        string format = null;
        int formatIndex = text.IndexOf(":");
        if (formatIndex > 0)
        {
            argumentName = text.Substring(0, formatIndex);
            format = text.Substring(formatIndex + 1);
        }

        ArgumentName = argumentName;
        Format = format;
    }

    public override void BuildSourceCode(StringBuilder source, ParsedTemplate parsed)
    {
        if(!string.IsNullOrEmpty(ArgumentName))
        {
            source.Append(parsed.Template.OutputItemName);
            if (string.IsNullOrEmpty(Format))
            {
                source.Append(".Write(");
                source.Append(ArgumentName);
                source.Append(");");
            }
            else
            {
                source.Append(".WriteFormatted(");
                source.Append(ArgumentName);
                source.Append(", \"");
                source.Append(Format);
                source.Append("\");");
            }

            return;
        }

        base.BuildSourceCode(source, parsed);
    }
}

Now we have to create the WriteFormatted function in the Output class:

[ComVisible(true)] // Needed to be used by JavaScript code
public class CustomOutput : Output
{
    public CustomOutput(ParsedTemplate template, TextWriter writer)
        : base(template, writer)
    {

    }

    public void WriteFormatted(object value, string format)
    {
        if (string.IsNullOrWhiteSpace(format))
        {
            Write(string.Format("{0}", value));
        }
        else
        {
            string formattedValue = string.Format("{0:" + format + "}", value);
            Write(formattedValue);
        }
    }
}

Finally let’s say to the Template to use our custom classes instead of the default ones.

public class CustomTemplate : Template
    {
        public override CodeBlock CreateNewCodeBlock(string code, int creationIndex)
        {
            return new CustomCodeBlock(code, creationIndex);
        }

        public override Output CreateNewOutput(ParsedTemplate parsedTemplate, TextWriter writer)
        {
            return new CustomOutput(parsedTemplate, writer);
        }
    }

That’s it! The full sample is available on our GitHub.

Happy Templating

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Dissecting the ASP.NET Identity Producer – Part 1

A few weeks ago we release a new producer: ASP.NET Identity Producer. Let’s see how we wrote it.

The main idea of the article is not to explain every line of code, but to explain the main concepts of the producer.

As a reminder the full source code of the producer is available on our GitHub repository.

What is a producer?

The main goal of a producer is to translate the model into code of any kind: C#, VB.NET, SQL, plain text. But a producer can generate what it want, or maybe don’t generate anything. It can also interact with database or file system.  You can do what you want with a producer.

First you have to implement the interface IProducer.

public interface IProducer 
{ 
    event Producer.OnProductionEventHandler Production; 
    void Initialize(Project project, Producer producer); 
    void Produce(); 
    void Terminate(); 
}

The build process uses producer this way:

Image may be NSFW.
Clik here to view.
Build Process

In the Initialize method you should make required initializations. Often this method simply copy the Project parameter to a field, so it can be used in the Produce method. The project object contains all information about the model (Entities, Views, Tables, etc.).

In the Produce method, you have to generate the output. This can be a complex output as the BOM or a much simpler as a summary of the project or maybe no output.
The Terminate method allows to clean resources.

This is the theory. In practice producers often inherit from a base class which already implement the IProducer interface:

  • BaseProducer
  • UIProducer
  • CodeDomBaseProducer
  • TemplateProducer
  • any existing producer

The basic code of the ASP.NET Identity producer is:

public class AspNetIdentityProducer : BaseProducer
{
    protected override string NamespaceUri
    {
        get { return Constants.NamespaceUri; }
    }

    public Version TargetFrameworkVersion
    {
        get
        {
            return new Version(4, 5);
        }
    }

    public override void Initialize(Project project, Producer producer)
    {
        base.Initialize(project, producer);
    }

    public override void Produce()
    {
    }
}

public class Constants
{
    public const string NamespaceUri = "http://www.softfluent.com/codefluent/producers.aspNetIdentityProducer/2014/1";
}

We introduce two additional notions: TargetFrameworkVersion and NamespaceUri.

The first one is the Framework version needed by the generated code. The second one is the NamespaceUri used to store custom xml attributes. You should create a unique name.

How to deploy and use the producer?

A producer is composed of one DLL that contains a class which implements IProducer. To deploy the producer, you must copy the DLL to the CodeFluent Entities directory (by default: C:\Program Files (x86)\SoftFluent\CodeFluent\Modeler). To use it, you have to declare it in the model file:

  <cf:producer name="Asp.Net Identity" typeName="CodeFluent.Producers.AspNetIdentity.AspNetIdentityProducer, CodeFluent.Producers.AspNetIdentity">
    <cf:configuration targetDirectory="..\Samples" implementQueryableUserStore="true" implementQueryableRoleStore="true" cfx:targetProjectLayout="Update" />
  </cf:producer>

The next post will cover how does the producer generate the code. In the meantime you can download the full code on our GitHub repository.

Let us know what you think!

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Dissecting the ASP.NET Identity Producer – Part 2

In our last post, we talked about some generalities regarding writing a custom producer.
Today we’ll see how does the ASP.NET Identity Producer generate C# code. Let’s remind that the full source code is available on our GitHub repository.

First and before producing code, we have to find the Role and the User entities in the model.

Do you remember the NamespaceUri? It allows you to add custom attributes in the xml file which represents the model.

<cf:entity name="User" d2p1:entityType="User" xmlns:d2p1=
"http://www.softfluent.com/codefluent/producers.aspNetIdentityProducer/2014/1"> 

</cf:entity> 

We add the entityType attribute to the entity. Xml Namespace are useful to avoid naming conflict between producers.

To find the User entity we can do the following, create an enumeration named “EntityType”:

public enum EntityType 
{ 
    None, 
    User, 
    Role, 
    UserRole, 
    Claim, 
    Login 
} 

Then, read the attribute value in the following way:

foreach (var entity in project.Entities) 
{ 
    if (entity.GetAttributeValue("entityType", 
               Constants.NamespaceUri, EntityType.None) == EntityType.User) 
    { 
        return entity; 
    } 
} 

The Produce method is a blank method so you can do what you want. When the output file is simple (with no complex logic), the easiest way is to use a template. When the logic is more complex you may want to use other mechanism such as CodeDom.

In the ASP.NET Identity producer we decided to use two templates: one for UserRole and the other one for RoleStore. CodeFluent Runtime already provides everything you need to use template. We’ll use here the SimpleTemplateProducer abstract class to simplify templates mechanisms (loading, parsing, processing). This producer takes directly the template from the resources of the producer, so you’ll have only one DLL to release. Of course the template has access to the producer instance, and so to its properties and methods.

Image may be NSFW.
Clik here to view.
AspNet Identity Producer

Tips: Keep the maximum of the logic in the producer, your templates will be much simpler to read

Here’s an extract from the template:

public System.Threading.Tasks.Task CreateAsync([%=TemplateProducer.IdentityRole.Entity.ClrFullTypeName%] role)
{
	if(role == null)
		throw new System.ArgumentNullException("role");

    return System.Threading.Tasks.Task.FromResult(role.Save());
}

And the code to run the template

public class RoleStoreProducer : SimpleTemplateProducer
{
    public IdentityRole IdentityRole { get; set; }
    
    protected override string DefaultNamespace
    {
        get { return Producer.Project.DefaultNamespace + Producer.WebNamespaceSuffix + ".Security"; }
    }

    protected override string DefaultTypeName
    {
        get { return "RoleStore"; }
    }

    protected override Template CreateTemplate()
    {
        var template = base.CreateTemplate();

        template.AddReferenceDirective(typeof(CodeDomBaseProducer));
        template.AddReferenceDirective(typeof(UserStoreProducer));

        return template;
    }

    public override string TargetPath
    {
        get
        {
            string path = ConvertUtilities.Nullify(XmlUtilities.GetAttribute(Producer.Element, ConvertUtilities.Camel(this.TargetName) + "TargetPath", (string)null), true);
            if (path == null)
                return BaseType.GetFilePath(Producer.TargetBaseNamespace, TypeName, Namespace, Producer.FullTargetDirectory, null);

            return Producer.GetFullRelativeDirectoryPath(path);
        }
    }
}

Note: We omit some code to make this implementation simpler

The template file is found based on the DefaultTypeName, so we don’t need to specify anything else. Instantiating the RoleStoreProducer and calling its Produce method will load the template from resources, and run it.

To implement the IUser and IRole interfaces, we need to edit the code generated by the CodeDom Producer. To do so, the way to go is to write a CodeDomSubProducer. But you can also get the instance of the BOM producer and register to the CodeDomProduction event:

var producer = project.Producers.GetProducerInstance<CodeDomProducer>();
if (producer == null)
    return;
producer.CodeDomProduction += CodeDomProducer_CodeDomProduction;

We can now handle the event and add the interface implementation:

private void CodeDomProducer_CodeDomProduction(object sender, CodeDomProductionEventArgs e)
{
    if (e.EventType == CodeDomProductionEventType.EntityCommitting)
    {
        CodeCompileUnit unit = e.Argument as CodeCompileUnit;
        if (unit == null)
            return;

        foreach (CodeNamespace ns in unit.Namespaces)
        {
            foreach (CodeTypeDeclaration typeDeclaration in ns.Types)
            {
                BaseType type = UserData.GetBaseType(typeDeclaration);
                if (type.GetAttributeValue("entityType", NamespaceUri, EntityType.None) == EntityType.User)
                {
                    // Implements IUser<TKey> & IUser
                    if (_identityUser.MustImplementGenericInterface)
                    {
                        ImplementIUser(typeDeclaration, true);
                    }

                    ImplementIUser(typeDeclaration, false);
                }
            }
        }
    }
}

private void ImplementIUser(CodeTypeDeclaration typeDeclaration, bool generic)
{
    string keyTypeName = generic ? _identityUser.KeyTypeName : typeof(string).FullName;
    var iuserCodeTypeReference = new CodeTypeReference("Microsoft.AspNet.Identity.IUser");
    var iuserGenericCodeTypeReference = new CodeTypeReference("Microsoft.AspNet.Identity.IUser");
    iuserGenericCodeTypeReference.TypeArguments.Add(keyTypeName);
    if (generic)
    {
        iuserCodeTypeReference.TypeArguments.Add(keyTypeName);
    }

    typeDeclaration.BaseTypes.Add(iuserCodeTypeReference);

    CodeMemberProperty idProperty = new CodeMemberProperty();
    idProperty.PrivateImplementationType = iuserGenericCodeTypeReference;
    idProperty.Type = new CodeTypeReference(keyTypeName);
    idProperty.Name = "Id";
    idProperty.HasSet = false;
    idProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), generic ? _identityUser.KeyPropertyName : "EntityKey")));
    typeDeclaration.Members.Add(idProperty);
}

The CodeDom can be traversed to find class declarations. The CodeDom producer annotates CodeDom elements, so we can get model concept from the CodeDom element by using UserData class. In this case we know if a class corresponds to the User entity by using UserData.GetBaseEntity.

In the last part, we’ll explain how to integrate this producer in Visual Studio and how to debug it.

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Dissecting the ASP.NET Identity Producer – Part 3

If you’ve read our two previous posts (Part 1 and Part 2), you should know how to create a CodeFluent Entities custom producer. Now you may ask yourself how to integrate it into Microsoft Visual Studio and how to debug it.

Visual Studio Integration

Fist, to declare the producer, we have to create or edit the xml file located in “%APPDATA%\CodeFluent.Modeler.Design”.

<codeFluent.Modeler> 
    <producerDescriptors> 
      <producerDescriptor name="AspNetIdentity" displayName="Asp.Net Identity" category="Security" typeName="SoftFluent.AspNetIdentity.AspNetIdentityProducer, SoftFluent.AspNetIdentity" /> 
    </producerDescriptors> 
</codeFluent.Modeler>

Then, open Visual Studio and try to add a new producer:

Image may be NSFW.
Clik here to view.
AspNet Identity Producer Configuration

The property grid displays properties exposed by the producer:

public class AspNetIdentityProducer : BaseProducer
{
    [DefaultValue(false)]
    [Category("Source Production")]
    [DisplayName("Must Implement IQueryableUserStore")]
    [Description("Determines if the IQueryableUserStore interface must be implemented. WARNING: this is not a real IQueryable data source. This can be used to load all users.")]
    [ModelLevel(ModelLevel.Normal)]
    public bool MustImplementQueryableUserStore
    {
        get
        {
            return XmlUtilities.GetAttribute(Element, "implementQueryableUserStore", false);
        }
        set
        {
            XmlUtilities.SetAttribute(Element, "implementQueryableUserStore", value.ToString().ToLowerInvariant());
        }
    }
}

As you can see, parameter values are stored in the XML file. Do not create automatic properties, it won’t work!

We show that we can create custom attributes, but it can be very useful to display them in the property grid:

Image may be NSFW.
Clik here to view.
Custom Producer Property Grid

The BaseProducer implements the IDescribable interface, so we have to override the BuildDescriptors method.

protected override void BuildDescriptors(IList<Descriptor> descriptors)
{
    if (descriptors == null)
        return;

    descriptors.Add(BuildDescriptor(
        name: "entityType",
        typeName: typeof(EntityType).AssemblyQualifiedName,
        defaultValue: "None",
        displayName: "Entity Type",
        description: "ASP.NET Identity Entity Type.",
        targets: NodeType.Entity));

    descriptors.Add(BuildDescriptor(
        name: "propertyType",
        typeName: typeof(PropertyType).AssemblyQualifiedName,
        defaultValue: "None",
        displayName: "Property Type",
        description: "ASP.NET Identity Property Type.",
        targets: NodeType.Property));

    descriptors.Add(BuildDescriptor(
        name: "methodType",
        typeName: typeof(MethodType).AssemblyQualifiedName,
        defaultValue: "None",
        displayName: "Method Type",
        description: "ASP.NET Identity Method Type.",
        targets: NodeType.Method));

    base.BuildDescriptors(descriptors);
}

Thanks to the target, descriptors are shown only when needed. This allow to not pollute the property grid with meaningless descriptors. Note that the same descriptor can have multiple targets. Combine them with OR (“|” in C#). For example :

 NodeType.Property | NodeType.Method.

Because creating identity entities is boring, we add a form to create them automatically:

Image may be NSFW.
Clik here to view.
AspNet Identity Form

The two issues are:

  • How to open this form?
  • How to edit the model?

To answer the first one, CodeFluent Entities uses another interface: IDesignProducer. It allows to add menu items at the producer level.

Image may be NSFW.
Clik here to view.
Create Identity Entities

Once again, the BaseProducer already implements this interface so we have to override two methods: EnumerateMenus and ExecuteMenu.

protected override void BuildMenus(IList<IDesignProducerMenu> menus)
{
    base.BuildMenus(menus);

    if (menus == null)
        return;

    menus.Add(new BaseDesignMenu("Create Identity Entities", true));
}

protected override bool ExecuteMenu(IServiceProvider serviceProvider, IDictionary<string, object> context, int index)
{
    Project project = context["Project"] as Project;
    if (project == null)
        return false;

    switch (index)
    {
        case 0:
            var form = new ConfigurationForm(project);
            form.ShowDialog();
            return true;
    }

    return base.ExecuteMenu(serviceProvider, context, index);
}

Now we open the form, we need to create entities. You can see that we have access to the Project object, so just use it. Here’s the code to create a new entity:

Entity entity = new Entity();
entity.Name = entityName;
entity.Namespace = @namespace;
entity.SetAttributeValue("", "entityType", Constants.NamespaceUri, entityType);
project.Entities.Add(entity);

When you edit the model, CodeFluent Entities automatically update surfaces.

How to debug your custom producer?

One way is to start Visual Studio as administrator, so you can use the post build event to copy the generated DLL to the CodeFluent Entities directory.

xcopy “$(TargetPath)” “C:\Program Files (x86)\SoftFluent\CodeFluent\Modeler” /Y

Then you can configure the debugger to start an external program:

Program: C:\Program Files (x86)\SoftFluent\CodeFluent\Modeler\CodeFluent.Build4.exe
Command line arguments:

Image may be NSFW.
Clik here to view.
Producer Debugger Configuration

Another solution is to add System.Diagnostics.Debugger.Launch and System.Diagnostics.Debugger.Break in your code. This can be useful in templates.

Now you can start the debug (F5) and set breakpoints into your producer.

To conclude, this producer is quite simple, but it shows:

  • How to extends the modeler,
  • How to edit the model at design time,
  • How to generate code with Templates and CodeDom.

This is a great start when you want to write a producer. If you need more information, feel free to ask your question on the forums.

The full source code is available on our GitHub repository.

Happy producing!

The R&D Team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

CFQL Raw methods

CodeFluent Query Language (aka CFQL) allows developers to define platform agnostic methods. For exemple you can write:

LOAD(FirstName) WHERE FirstName STARTSWITH @FirstName 

This CFQL method will be translated to SQL by the persistence producer you decide to use. This is very powerful but when you want to write advanced procedures, you have to write platform specific code.

Read more about CFQL: http://blog.codefluententities.com/2013/07/25/hands-on-cfql/.

Inline SQL code

You can inline SQL code in CFQL by surrounding it with “[“ and “]”:

LOAD(FirstName, int year) WHERE FirstName STARTSWITH @FirstName AND [DATEPART(yy, $Customer::DateOfBirth$) = @year] 

You have to set CheckLevel to None to confirm that you understand what you are doing:

Image may be NSFW.
Clik here to view.
Method Properties

The SQL code generated:

CREATE PROCEDURE [dbo].[Customer_LoadByFirstNameAndYear]
(
 @FirstName [nvarchar] (256),
 @year [int],
 @_orderBy0 [nvarchar] (64) = NULL,
 @_orderByDirection0 [bit] = 0
)
AS
SET NOCOUNT ON
SELECT DISTINCT [Customer].[Customer_Id], [Customer].[Customer_FirstName], [Customer].[Customer_LastName], [Customer].[Customer_DateOfBirth] 
    FROM [Customer]
    WHERE (([Customer].[Customer_FirstName] LIKE (@FirstName + '%')) AND DATEPART(yy, [Customer].[Customer_DateOfBirth]) = @year)

SQL method

Sometimes you need to write a custom method directly in SQL. Set the text to:

  • LOAD(arguments) RAW when the return type correspond to a collection of the current entity
  • LOADONE(arguments) RAW when the return type correspond to one instance of the current entity
  • RAW(arguments) otherwise. You have to specify the return type in the property grid

If you want to target multiple DBMS, you have to write one raw body by DBMS:

Image may be NSFW.
Clik here to view.
CodeFluent Query Language Editor

SQL code generated:

CREATE PROCEDURE [dbo].[Customer_LoadRaw]
(
 @year [int],
 @_orderBy0 [nvarchar] (64) = NULL,
 @_orderByDirection0 [bit] = 0
)
AS
SET NOCOUNT ON
SELECT * FROM Customer 
WHERE DATEPART(yy, [Customer].[Customer_DateOfBirth]) = @year

Use persistent view

Persistent views will be translated to SQL views. Persistent views are a key point since they can be used:

  • To focus, simplify, and customize the perception each user has of the database.
  • To control access to rows and columns of data.
  • To aggregate data for performance.

Persistent views in CodeFluent Entities are attached to the entity concept: an entity can have multiple views. Views can be used for CFQL methods Load, LoadOne, Count, Delete and Search
Views can be auto-generated or you can provide the SQL code.

Create the view:

Image may be NSFW.
Clik here to view.
View Editor

Image may be NSFW.
Clik here to view.
CustomerView

Use the view in a CFQL method:

Image may be NSFW.
Clik here to view.
LoadFromView

The generated SQL code:

CREATE VIEW [dbo].[vCustomerCustomerView]
AS
SELECT 
    [Customer].[Customer_Id], 
	[Customer].[Customer_DateOfBirth], 
	[Customer].[Customer_FirstName] + ' ' + [Customer].[Customer_LastName] AS FullName
FROM Customer

CREATE PROCEDURE [dbo].[Customer_LoadFromView]
(
 @_orderBy0 [nvarchar] (64) = NULL,
 @_orderByDirection0 [bit] = 0
)
AS
SET NOCOUNT ON
SELECT DISTINCT [vCustomerCustomerView].[Customer_Id], [vCustomerCustomerView].[Customer_DateOfBirth], [vCustomerCustomerView].[FullName] 
    FROM [vCustomerCustomerView]
    WHERE ([vCustomerCustomerView].[FullName] LIKE 'J%')

Target Name Transformation (TNT)

Using the name of a column in a RAW method is not safe. Indeed CodeFluent Entities allows to define its own naming convention. So if you write the name of a column in a raw method and then you change the naming convention of your project, your method won’t work anymore.
To handle this case, CodeFluent Entities introduce TNT. In a Raw method you can refers to a column by using for example “$Customer::DateOfBirth$”. This will be replaced by CodeFluent Entities by the name of the column corresponding to the property “DateOfBirth” of the entity “Customer”.

TNT supports the following syntaxes:

  • $[EntityName]$ corresponds to the table name,
  • $[PropertyName]$ corresponds to the property name,
  • $[EntityName]::[PropertyName]$ corresponds to the column name,
  • $[EntityName]:[ViewName]$ corresponds to the view name,
  • $[EntityName]:[ViewName]:[PropertyName]$ corresponds to a column name in the defined view,
  • $[Namespace].[EnumerationName].[EnumerationValue]$ corresponds to the enumeration value of an enumeration declared in the model.

The full documentation is available here: http://www.softfluent.com/documentation/Methods_TargetNameTransformation.html

Happy CFQLing,

The R&D team


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Store Enums as Strings

CodeFluent Entities fully suports .NET enumerations since 2005. This supports includes:

  • Being able to create your own enumerations at design time,
  • Use already existing CLR enumerations.
  • Using our designer, you can create and use enumerations in your model (e.g. OrderStatus in the screenshot below):

Image may be NSFW.
Clik here to view.
Enumeration

Currently CodeFluent Entities allows to store the enumeration value as a numeric value. Sometimes you prefer storing it as text so we write an aspect to do that.

Without the Aspect:
Image may be NSFW.
Clik here to view.
Without the aspect

With the Aspect:
Image may be NSFW.
Clik here to view.
With the Aspect

After adding the Aspect, you’ll see new properties in the property grid:

Image may be NSFW.
Clik here to view.
Store Enumeration As Text Property

The “Store Enumeration As Text” property exists at:

  • Enumeration level
  • Property level

You can also specified the column size. By default the column size will be computed from the enumeration values when possible. For example if you have a flag enumeration with values “First”, “Second”, “Third” the longest value will be “First, Second, Third”, so the column size will be 21. Of course you can override the value by setting the property “Default Column Size” at enumeration level or “Column Size” at property level.

How to install the aspect

Create a C# project in the solution and copy the Aspect files: https://github.com/SoftFluent/CodeFluent-Entities/blob/master/Extensions/SoftFluent.StoreEnumAsText/SoftFluent.Samples.StoreEnumAsText.Aspects/StoreEnumerationAsTextAspect.cs

Add a reference in the model to the aspect project:

Image may be NSFW.
Clik here to view.
Solution Explorer

 

Then, select those two projects:

 

Image may be NSFW.
Clik here to view.
Add Reference

Finally, add the Aspect into your model:

Image may be NSFW.
Clik here to view.
Add Existing Aspect

The code of the aspect is available on GitHub repository. Please leave feedback on how you liked this Aspect and what we could improve. You can also find additional resources about Aspects here.

Happy Aspecting!

The R&D team.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.
Viewing all 125 articles
Browse latest View live