Home > Developing Using CodeFluent Entities, Producers > Dissecting the ASP.NET Identity Producer – Part 1

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:

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.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s