Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

Entity Framework and T4: Generate Specification Objects for Your Entities

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
31 Jul 2011CPOL3 min read 24.8K   11   2
Learn how to use the Specification Pattern and how to generate Specification Objects for your Entity Framework entities using T4 templates.

In this article, you will learn how to use the Specification Pattern and how to generate Specification Objects for your Entity Framework entities using T4 templates.

Table of Contents

Specification Pattern Overview

According to Martin Fowler and Eric Evans, a specification defines a set of conditions that a candidate object must fulfill in order to meet the specification. Specifications can be used for:

  • Selection: When you need to select a set of objects based on some criteria
  • Validation: When you need to check that only suitable objects are used for a certain purpose

The Specification Pattern can be represented like this in .NET (using Generics):

C#
public interface ISpecification<T> where T : class
{
    Expression<Func<T, bool>> GetExpression();
    bool IsSatisfiedBy(T entity);
}

We can also create Composite Specifications by combining other specifications – this allows us to reuse existing specifications to create more complex ones.

Using Specification Pattern

I’m using the MVC Music Store database, this is the model:

Music Store Model

And now some examples. I will assume that you have a repository like this (I’m using this implementation):

C#
public IQueryable All<T>(Expression<Func<bool, T>> expression) where T : class

A Generic Specification Class

C#
public class Specification<T> : ISpecification<T> where T : class
{
    private Expression<Func<T, bool>> expression;

    public Expression<Func<T, bool>> GetExpression()
    {
        return expression;
    }

    public Specification(Expression<Func<T, bool>> expression)
    {
        this.expression = expression;
    }

    public bool IsSatisfiedBy(T entity)
    {
        var query = (new[] { entity }).AsQueryable();

        return query.Any(this.expression);
    }
}

Creating Specifications

Using the generic class to create specifications:

  • One specification for jazz albums
  • One specification for cheap albums (price between 1 and 10)
C#
public static ISpecification<Album> JazzAlbumSpecification
{
	get
	{
		return new Specification<Album>(
			x => x.Genre.Name == "Jazz"
		);
	}
}

public static ISpecification<Album> CheapAlbumSpecification
{
	get
	{
		return new Specification<Album>(
			x => x.Price >= 1 && x.Price <= 10
		);
	}
}

Selecting Objects

C#
var albums = from x in repository.All<Album>(JazzAlbumSpecification.GetExpression())
             select x;

Performing Validation

C#
Album metalAlbum = GetMetalAlbum();
Album jazzAlbum = GetJazzAlbum();

bool isJazzAlbum = JazzAlbumSpecification.IsSatisfiedBy(metalAlbum);
isJazzAlbum = JazzAlbumSpecification.IsSatisfiedBy(jazzAlbum);

Composing Specifications

Existing specifications can be combined to form more complex ones. Using these extension methods, it’s easy to create composite specifications (see this article to understand how to combine lambda expressions):

C#
public static ISpecification<T> And<T>(
    this ISpecification<T> first, ISpecification<T> second) where T : class
{
    return new Specification<T>(
        first.GetExpression()
        .And(second.GetExpression())
    );
}

public static ISpecification<T> Or<T>(
       this ISpecification<T> first, ISpecification<T> second) where T : class
{
	return new Specification<T>(
        first.GetExpression()
        .Or(second.GetExpression())
    );
}

The specifications defined above can now be combined to compose a new specification like this:

C#
ISpecification<Album> cheapJazzAlbumSpecification = 
              JazzAlbumSpecification.And(CheapAlbumSpecification);

// using the specification to select all cheap jazz albums
var cheapJazzAlbums = from x in repository.All<Album>(cheapJazzAlbumSpecification.GetExpression())
                      select x;

Using T4 to Generate Specification Objects

T4 is a code generator built right into Visual Studio. You can generate any text file using T4 templates: C#, JavaScript, HTML, XML and many others. If you’ve never heard about it, this is a good place to start:

I’ve created a T4 template that generates automatically all the Specification Objects, one for each entity in our model. All the generated objects have all the public properties of their respective entities, including association properties. All objects were marked with the [Serializable] attribute, so you can easily serialize it if you need.

In a previous article, I’ve created query objects for Entity Framework, I’m generating exactly the same properties in this template. You can see a complete description of the generated properties here.

This is the generated object model:

Image 2

The previous specifications can now be written like this:

C#
public static ISpecification<Album> JazzAlbumSpecification
{
    get
    {
        return new AlbumSpecification() {
            Genre = new GenreSpecification() { Name = "Jazz" }
        };
    }
}

public static ISpecification<Album> CheapAlbumSpecification
{
    get
    {
        return new AlbumSpecification() {
            PriceFrom = 1,
            PriceTo = 10
        };
    }
}

Configuration

In the demo solution, double-click ModelSpecification.tt and change the following lines, according to your needs:

C#
string inputFile = @"Model.edmx";
string namespaceName = @"MusicStore.Model";
string filenameSuffix = "Specification.gen.cs";

When you save the template file or you rebuild the project, the code will be regenerated. If you don’t want to generate the code, remove the value of the Custom Tool property in the property browser of the template file (by default, the value is TextTemplatingFileGenerator).

References

Downloads

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.

I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).

Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.

I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.

Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).

Comments and Discussions

 
Questionadding Not<T> specification to your list of specifications Pin
Member 832697027-Feb-13 4:22
Member 832697027-Feb-13 4:22 
AnswerRe: adding Not<T> specification to your list of specifications Pin
Rui Jarimba10-Mar-13 10:20
professionalRui Jarimba10-Mar-13 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.