Improving LINQ code reusability: Select method

Select method is used to project each element of a sequence into a new form, i.e. it can be used to map a collection of one type to a collection of another type. In this article I’ll show you a simple approach that will allow you to reuse the code used in the Select method.

Table of contents

The Problem

Consider the following model:

Let’s suppose that you have a services layer, so you don’t want to expose your domain objects directly to the client applications. Instead you create a set of data contracts (or DTOs, if you prefer):

At some stage you’ll have to convert those Domain objects to data contracts. This is a common way of doing it:

var details = repository.All<Album>().Select(album => new AlbumDetail {
    AlbumId = album.AlbumId,
    Price = album.Price,
    Title = album.Title,

    ArtistId = album.ArtistId,
    GenreId = album.GenreId,
    ArtistName = (album.Artist == null) ? null : album.Artist.Name,
    GenreName = (album.Genre == null) ? null : album.Genre.Name
});

There is a problem with this approach – if you need to query the same collection but using different criteria you have to duplicate the code inside the Select method.

Solution 1 – Creating a method for the mapping

In order to reuse the code, we can create a method that converts Album objects (Domain) to data contract objects:

private static AlbumSummary CreateAlbumSummary(Album album)
{
    return new AlbumSummary {
        AlbumId = album.AlbumId,
        Title = album.Title,

        ArtistName = (album.Artist == null) ? null : album.Artist.Name
    };
}

private static AlbumDetail CreateAlbumDetail(Album album)
{
    return new AlbumDetail {
        AlbumId = album.AlbumId,
        Price = album.Price,
        Title = album.Title,

        ArtistId = album.ArtistId,
        GenreId = album.GenreId,
        ArtistName = (album.Artist == null) ? null : album.Artist.Name,
        GenreName = (album.Genre == null) ? null : album.Genre.Name
    };
}

Using the code:

var albums = Albums.Select(CreateAlbumDetail);
var albumsByGenre = Albums.Where(x => x.GenreId == genreId).Select(CreateAlbumDetail);

// alternative way
var albums2 = Albums.Select(x => CreateAlbumDetail(x));
var albumsByGenre2 = Albums.Where(x => x.GenreId == genreId).Select(x => CreateAlbumDetail(x));

Solution 2 – Creating a generic ObjectMapper object

The previous solution solves the code reusability problem, but there’s still a tight coupling between components. Abstractions should be used to implement loose coupling between components – in this case, to abstract the mapping code.

Step 1: define a contract (interface) with a method that converts one object of type TSource to an object of type TDestination:

public interface IObjectMapper
{
    TDestination Map<TSource, TDestination>(TSource source);
}

Step 2: create a class that implements IObjectMapper (click to expand):

public class ObjectMapper : IObjectMapper
{
    private Dictionary<Type, Func<object, object>> Mappers = new Dictionary<Type, Func<object, object>>
    {
        { typeof(Tuple<Album, AlbumDetail>), CreateAlbumDetail },
        { typeof(Tuple<Album, AlbumSummary>), CreateAlbumSummary }

        // more mappings here
        // ....
    };


    public TDestination Map<TSource, TDestination>(TSource source)
    {
        if(source == null)
            return default(TDestination);

        Func<object, object> mapper = null;
        Type key = typeof(Tuple<TSource, TDestination>);

        if(Mappers.TryGetValue(key, out mapper))
        {
            var newObject = mapper(source);
            return (TDestination) newObject;
        }

        string errorMessage = string.Format("Invalid mapping (Source: {0}, Destination: {1})";,
                                            typeof(TSource).FullName, 
                                            typeof(TDestination).FullName);
        
        throw new InvalidOperationException(errorMessage);
    }


    private static object CreateAlbumDetail(object source)
    {
        var album = source as Album;

        return new AlbumDetail {
            AlbumId = album.AlbumId,
            Price = album.Price,
            Title = album.Title,

            ArtistId = album.ArtistId,
            GenreId = album.GenreId,
            ArtistName = (album.Artist == null) ? null : album.Artist.Name,
            GenreName = (album.Genre == null) ? null : album.Genre.Name
        };
    }

    private static object CreateAlbumSummary(object source)
    {
        var album = source as Album;

        return new AlbumSummary {
            AlbumId = album.AlbumId,
            Title = album.Title,
            
            ArtistName = (album.Artist == null) ? null : album.Artist.Name
        };
    }
}

Example 1: Using LINQ

Using the mapper in a LINQ expression – convert an Album collection to an AlbumSummary collection:

IObjectMapper mapper = new ObjectMapper();

IEnumerable<AlbumSummary> summaries = repository.All<Album>()
                                        .Select(mapper.Map<Album, AlbumSummary>);

Example 1: Mapping a single object

Using the mapper for a single object:

var album = new Album {
    AlbumId = 1,
    Price = 10.0m,
    Title = "The Dreamer",
    Artist = new Artist { ArtistId = 1, Name = "José James" },
    Genre = new Genre { GenreId = 1, Name = "Jazz" }
};

IObjectMapper mapper = new ObjectMapper();

AlbumDetail albumDetail = mapper.Map<Album, AlbumDetail>(album);

Unit Testing

Some NUnit tests:

[Test]
public void Given_a_non_existing_mapping_when_mapping_object_then_should_throw_InvalidOperationException()
{
    // arrange
    IObjectMapper mapper = new ObjectMapper();
    var albumDetail = new AlbumDetail();

    // act/assert
    Assert.Throws<InvalidOperationException>(() => 
        // non-existing mapping
        mapper.Map<AlbumDetail, AlbumSummary>(albumDetail)
    );
}

[Test]
public void Given_an_album_when_mapping_to_album_summary_should_equals_expected_album_summary()
{
    // arrange
    IObjectMapper mapper = new ObjectMapper();
    
    var album = new Album {
        AlbumId = 4,
        Price = 10.0m,
        Title = "Heritage",
        Artist = new Artist { ArtistId = 4, Name = "Opeth" },
        Genre = new Genre { GenreId = 4, Name = "Metal" }
    };

    var expectedAlbumSummary = new AlbumSummary {
        AlbumId = 4,
        ArtistName = "Opeth",
        Title = "Heritage"
    };
    
    // act
    AlbumSummary albumSummary = mapper.Map<Album, AlbumSummary>(album);
    
    // assert
    Assert.AreEqual(albumSummary, expectedAlbumSummary);
}

Final thoughts

In this article you learned how to reuse the code used in the Select method, and how you can use that code to map single objects. But writing mapping code is tedious and time consuming. There are mapping tools out there that can make your life easier – AutoMapper is one of them. I’ve used it in the past and I definitely recommend it. So, why use Automapper? Quoting their website:

“What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper’s established convention, almost zero configuration is needed to map two types”

“Mapping code is boring. Testing mapping code is even more boring. AutoMapper provides simple configuration of types, as well as simple testing of mappings”

References

Downloads

Download the demo project (VS2010): LINQ-Select.zip

Technorati Tags: , , ,

Bulk Insert in .NET applications, part 1

This is the first of a 2 series articles about how to perform bulk inserts in your .NET applications, using a SQL Server database.

In this article I’ll show how to create a wrapper object for SqlBulkCopy that can do a bulk insert for a collection of objects. In the second article I’ll show how that wrapper can be easily integrated with Entity Framework (creating extension methods for DbContext and ObjectContext).

Table of contents

The problem

I am working in an application that parses Excel files and creates an object for each line. After some validations and properties mapping the objects are then being inserted in a database table, one by one. At the beggining this wasn’t a big problem because the number of objects to insert in the database was small, but now there can be files with thousands of rows, so using Entity Framework isn’t the best way to do it (currently Entity Framework has no support for bulk insert operations).

The solution

I decided to use the object SqlBulkCopy because it seemed the best and easiest option for performing a bulk insert in a SQL Server database. I created a wrapper for SqlBulkCopy – BulkCopy.cs. This class works the same way as SqlBulkCopy but has some extra features.

The following properties are available:

  • DestinationTableName: Name of the destination table on the server
  • BatchSize (optional): Number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server
  • ConnectionString: Database connection string
  • ExpressionFilter: Filters the properties to be included

And these are the methods available (see examples below):

public void WriteToServer<T>(IEnumerable<T> items) where T : class;
public void WriteToServer<T>(IEnumerable<T> items, SqlBulkCopyOptions options) where T : class;
public void WriteToServer<T>(IEnumerable<T> items, SqlBulkCopyOptions options, IEnumerable<SqlBulkCopyColumnMapping> columnMappings) where T : class;

BulkCopy.cs source code (click to expand):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using BulkCopy.Extensions;

namespace BulkCopy
{
    /// <summary>
    /// Lets you efficiently bulk load a SQL Server table with data from another source.
    /// This is a wrapper class for <see cref="SqlBulkCopy"/>
    /// </summary>
    public class BulkCopy
    {
        /// <summary>
        /// Name of the destination table on the server
        /// </summary>
        public string DestinationTableName { get; set; }

        /// <summary>
        /// Number of rows in each batch. 
        /// At the end of each batch, the rows in the batch are sent to the server.
        /// </summary>
        public int? BatchSize { get; set; }

        /// <summary>
        /// Database connection string
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// Filters the properties to be included
        /// </summary>
        public Func<PropertyDescriptor, bool> ExpressionFilter { get; set; }

		
		
        /// <summary>
        /// Initializes a new instance of the <see cref="BulkCopy&lt;T&gt;"/> class.
        /// </summary>
        public BulkCopy()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BulkCopy&lt;T&gt;"/> class.
        /// </summary>
        public BulkCopy(string connectionString)
        {
            this.ConnectionString = connectionString;
        }

		
		
        /// <summary>
        /// Copies all items in a collection to a destination table
        /// </summary>
        /// <param name="dataTable">The items that will be copied to the destination table</param>
        /// <param name="options">A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. <see cref="SqlBulkCopyOptions"/></param>
        public virtual void WriteToServer<T>(IEnumerable<T> items) where T : class
        {
            WriteToServer(items, SqlBulkCopyOptions.Default);
        }

        /// <summary>
        /// Copies all items in a collection to a destination table
        /// </summary>
        /// <param name="dataTable">The items that will be copied to the destination table</param>
        /// <param name="options">A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. <see cref="SqlBulkCopyOptions"/></param>
        public virtual void WriteToServer<T>(IEnumerable<T> items, SqlBulkCopyOptions options) where T : class
        {
            DataTable dataTable = (this.ExpressionFilter == null) ? items.ToDataTable() : items.ToDataTable(this.ExpressionFilter);

            WriteToServer(dataTable, options);
        }

        /// <summary>
        /// Copies all items in a collection to a destination table
        /// </summary>
        /// <param name="dataTable">The items that will be copied to the destination table</param>
        /// <param name="options">A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. <see cref="SqlBulkCopyOptions"/></param>
        /// <param name="columnMappings">Returns a collection of System.Data.SqlClient.SqlBulkCopyColumnMapping items. 
        /// Column mappings define the relationships between columns in the data source and columns in the destination.</param>
        public virtual void WriteToServer<T>(IEnumerable<T> items, SqlBulkCopyOptions options, IEnumerable<SqlBulkCopyColumnMapping> columnMappings) where T : class
        {
            DataTable dataTable = (this.ExpressionFilter == null) ? items.ToDataTable() : items.ToDataTable(this.ExpressionFilter);

            WriteToServer(dataTable, options, columnMappings);
        }

        /// <summary>
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// </summary>
        /// <param name="dataTable">A System.Data.DataTable whose rows will be copied to the destination table</param>
        private void WriteToServer(DataTable dataTable)
        {
            WriteToServer(dataTable, SqlBulkCopyOptions.Default);
        }

        /// <summary>
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// </summary>
        /// <param name="dataTable">A System.Data.DataTable whose rows will be copied to the destination table</param>
        /// <param name="options">A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. <see cref="SqlBulkCopyOptions"/></param>
        private void WriteToServer(DataTable dataTable, SqlBulkCopyOptions options)
        {
            var columnMappings = from x in dataTable.Columns.Cast<DataColumn>()
                                 select new SqlBulkCopyColumnMapping(x.ColumnName, x.ColumnName);

            WriteToServer(dataTable, options, columnMappings);
        }

        /// <summary>
        /// Copies all rows in the supplied System.Data.DataTable to a destination table
        /// </summary>
        /// <param name="dataTable">A System.Data.DataTable whose rows will be copied to the destination table</param>
        /// <param name="options">A combination of values from the System.Data.SqlClient.SqlBulkCopyOptions 
        /// enumeration that determines which data source rows are copied to the destination table. <see cref="SqlBulkCopyOptions"/></param>
        /// <param name="columnMappings">Returns a collection of System.Data.SqlClient.SqlBulkCopyColumnMapping items. 
        /// Column mappings define the relationships between columns in the data source and columns in the destination.</param>
        private void WriteToServer(DataTable dataTable, SqlBulkCopyOptions options, IEnumerable<SqlBulkCopyColumnMapping> columnMappings)
        {
            // table name matching:
            // checks for DestinationTableName value
            // if null or empty, checks for dataTable.TableName
            string destinationTableName =
                (string.IsNullOrWhiteSpace(DestinationTableName) ? null : DestinationTableName)
                ?? (string.IsNullOrWhiteSpace(dataTable.TableName) ? null : dataTable.TableName);

            if(string.IsNullOrWhiteSpace(destinationTableName))
                throw new ArgumentException("destinationTableName cannot be null or empty");

            using(var bulkCopy = new SqlBulkCopy(this.ConnectionString, options))
            {
                bulkCopy.DestinationTableName = destinationTableName;

                if(this.BatchSize.HasValue)
                    bulkCopy.BatchSize = this.BatchSize.Value;

                foreach(var mapping in columnMappings)
                    bulkCopy.ColumnMappings.Add(mapping);

                bulkCopy.WriteToServer(dataTable);
            }
        }
    }
}

The BulkCopy object is using internally some extension methods that converts a collection of objects to a DataTable. (taken from SO’s post Generic List to DataTable, with some small modifications).

Click to expand the source code:

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace BulkCopy.Extensions
{
    public static class DataExtensions
    {
        /// <summary>
        /// Basic data types 
        /// </summary>
        private static Type[] dataTypes = new[] {
            typeof(byte)
            ,typeof(sbyte)
            ,typeof(short)
            ,typeof(ushort)
            ,typeof(int)
            ,typeof(uint)
            ,typeof(long)
            ,typeof(ulong)
            ,typeof(float)
            ,typeof(double)
            ,typeof(decimal)
            ,typeof(bool)
            ,typeof(char)
            ,typeof(Guid)
            ,typeof(DateTime)
            ,typeof(DateTimeOffset)
            ,typeof(byte[])
            ,typeof(string)
        };

        /// <summary>
        /// Converts a generic List to a DataTable
        /// <see cref="http://stackoverflow.com/a/5805044"/>
        /// </summary>
        /// <typeparam name="T">Type of the object to convert to DataTable</typeparam>
        /// <param name="data">Data to be converted</param>
        /// <returns>The converted DataTable</returns>
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            IEnumerable<PropertyDescriptor> properties = from x in TypeDescriptor.GetProperties(typeof(T)).Cast<PropertyDescriptor>()
                                                         where IsBasicType(x.PropertyType)
                                                         select x;

            DataTable table = GetDataTable(data, properties);
            return table;
        }

        /// <summary>
        /// Converts a generic List to a DataTable
        /// <see cref="http://stackoverflow.com/a/5805044"/>
        /// </summary>
        /// <typeparam name="T">Type of the object to convert to DataTable</typeparam>
        /// <param name="data">Data to be converted</param>
        /// <returns>The converted DataTable</returns>
        public static DataTable ToDataTable<T>(this IList<T> data, Func<PropertyDescriptor, bool> expression)
        {
            var properties = TypeDescriptor.GetProperties(typeof(T))
                .Cast<PropertyDescriptor>()
                .Where(expression);

            DataTable table = GetDataTable(data, properties);
            return table;
        }

        /// <summary>
        /// Converts an IEnumerable to a DataTable
        /// <see cref="http://stackoverflow.com/a/5805044"/>
        /// </summary>
        /// <typeparam name="T">Type of the object to convert to DataTable</typeparam>
        /// <param name="data">Data to be converted</param>
        /// <returns>The DataTable</returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> data)
        {
            return data.ToList().ToDataTable();
        }

        /// <summary>
        /// Converts an IEnumerable to a DataTable
        /// <see cref="http://stackoverflow.com/a/5805044"/>
        /// </summary>
        /// <typeparam name="T">Type of the object to convert to DataTable</typeparam>
        /// <param name="data">Data to be converted</param>
        /// <param name="expression">Predicate to filter the properties of <typeparamref name="T"/> to be included to the DataTable</param>
        /// <returns>The DataTable</returns>
        public static DataTable ToDataTable<T>(this IEnumerable<T> data, Func<PropertyDescriptor, bool> expression)
        {
            return data.ToList().ToDataTable(expression);
        }

        #region Private methods

        private static bool IsBasicType(Type type)
        {
            type = Nullable.GetUnderlyingType(type) ?? type;

            return type.IsEnum || dataTypes.Contains(type);
        }

        private static DataTable GetDataTable<T>(this IList<T> data, IEnumerable<PropertyDescriptor> mappedProperties)
        {
            DataTable table = new DataTable();

            // columns
            foreach(PropertyDescriptor prop in mappedProperties)
            {
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }

            // row values
            foreach(T item in data)
            {
                DataRow row = table.NewRow();

                foreach(PropertyDescriptor prop in mappedProperties)
                {
                    object value = prop.GetValue(item) ?? DBNull.Value;
                    row[prop.Name] = value;
                }

                table.Rows.Add(row);
            }

            return table;
        }

        #endregion
    }
}

Using the code

Assuming a table named dbo.Albums (I’m using Mvc Music Store model):

1. Basic usage

This example shows how to use a model object whose property names match the database column names from the table above:

public class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
}

All you need to do is to specify the connection string and the destination table:

IEnumerable<Album> data = GetData();

var bulkCopy = new BulkCopy() {
    ConnectionString = ConnectionString,
    DestinationTableName = "dbo.Albums"
};

bulkCopy.WriteToServer(data);

2. Specifying batch size and bulk options

Using the model from the previous example, you can specify some other options:

IEnumerable<Album> data = GetData();

var bulkCopy = new BulkCopy() {
    BatchSize = 200,
    ConnectionString = ConnectionString,
    DestinationTableName = "dbo.Albums"
};

// SqlBulkCopyOptions.CheckConstraints: Check constraints while data is being inserted. 
// By default, constraints are not checked.
bulkCopy.WriteToServer(data, SqlBulkCopyOptions.CheckConstraints);

3. Using column mappings

This example shows how to use a model object whose property names don’t match the database column names:

public class Album
{
    public virtual int Id { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string AlbumTitle { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtImage { get; set; }
}

In this case we have to create the column mappings. It can be done like this:

private static IEnumerable<SqlBulkCopyColumnMapping> GetColumnMappings()
{
    yield return new SqlBulkCopyColumnMapping("Id", "AlbumId");
    yield return new SqlBulkCopyColumnMapping("GenreId", "GenreId");
    yield return new SqlBulkCopyColumnMapping("ArtistId", "ArtistId");
    yield return new SqlBulkCopyColumnMapping("AlbumTitle", "Title");
    yield return new SqlBulkCopyColumnMapping("Price", "Price");
    yield return new SqlBulkCopyColumnMapping("AlbumArtImage", "AlbumArtUrl");
}

Finally, the bulk insert can be done this way:

IEnumerable<Album> data = GetData();
IEnumerable<SqlBulkCopyColumnMapping> mappings = GetColumnMappings();

var bulkCopy = new BulkCopy() {
    ConnectionString = ConnectionString,
    DestinationTableName = "dbo.Albums"
};

bulkCopy.WriteToServer(data,SqlBulkCopyOptions.Default, mappings);

4. Filtering properties to be copied

This example shows how to filter the properties of a model object to be used in the bulk insert:

public partial class Album
{
    public virtual int AlbumId { get; set; }
    public virtual int GenreId { get; set; }
    public virtual int ArtistId { get; set; }
    public virtual string Title { get; set; }
    public virtual decimal Price { get; set; }
    public virtual string AlbumArtUrl { get; set; }
    public virtual string P1 { get; set; }
    public virtual string P2 { get; set; }
}

Properties P1 and P2 don’t match any column of the table above, so they cannot be used in the bulk insert operation. Creating a filter to exclude those properties and using it can be done like this:

// properties to exclude from mapping
var nonMappedProperties = new string[] { "P1", "P2" };

Func<PropertyDescriptor, bool> expression = x => 
    !nonMappedProperties.Contains(x.Name);

IEnumerable<Album> data = GetData();

var bulkCopy = new BulkCopy() {
    BatchSize = 200,
    ConnectionString = ConnectionString,
    DestinationTableName = "dbo.Albums",
    ExpressionFilter = expression
};

bulkCopy.WriteToServer(data, SqlBulkCopyOptions.CheckConstraints);

That’s it! In the next article I’ll show you how to integrate easily this wrapper class in Entity Framework (creating extension methods for DbContext and ObjectContext).

References

Downloads

Download the demo project (VS2010): BulkCopy-part1.zip

Technorati Tags: , , ,

Entity Framework: Get mapped table name from an entity

Extension methods for ObjectContext and DbContent to get the mapped table name from an entity.

Table of contents

The problem

I am working on a set of extension methods to perform a bulk insert using Entity Framework, using internally the SqlBulkCopy object. One of the steps involved is to get the mapped table name from an entity. After some google searching, I found a post on StackOverflow that led me to the solution.

The solution

The trick is to use the method ObjectQuery.ToTraceString to generate a SQL Select statement for an entity, and then extract the table name from that statement.

Let’s assume that you have an entity named Album corresponding to a table named dbo.Albums.

// context is ObjectContext
string sql = context.CreateObjectSet<T>().ToTraceString();

...

The generated SQL for that entity can be something like this:

SELECT 
[Extent1].[AlbumId] AS [AlbumId], 
[Extent1].[GenreId] AS [GenreId], 
[Extent1].[ArtistId] AS [ArtistId], 
[Extent1].[Title] AS [Title], 
[Extent1].[Price] AS [Price], 
[Extent1].[AlbumArtUrl] AS [AlbumArtUrl]
FROM [dbo].[Albums] AS [Extent1] 

So, all we need to do is to parse the SELECT statement to get the table name. This is the approach used in the post above but it has some limitations – that code will work only for tables that are in the default SQL Server schema (dbo.{tableName}). I made some changes to that code and I’m extracting the full table name using regular expressions.

The extension methods

I have created one extension method for DbContext and other for ObjectContext:

public static class ContextExtensions
{
    public static string GetTableName<T>(this DbContext context) where T : class
    {
        ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext;

        return objectContext.GetTableName<T>();
    }

    public static string GetTableName<T>(this ObjectContext context) where T : class
    {
        string sql = context.CreateObjectSet<T>().ToTraceString();
        Regex regex = new Regex("FROM (?<table>.*) AS");
        Match match = regex.Match(sql);

        string table = match.Groups["table"].Value;
        return table;
    }
}

Using the code

Getting the mapped table name for an entity named Album, using a ObjectContext object:

ObjectContext context = ....;
string table = context.GetTableName<Album>();

Or using a DbContext object:

DbContext context = ....;
string table = context.GetTableName<Album>();

References

Technorati Tags: ,

ASP.NET: Downloading files from a UNC share

Implementing a cross-browser solution for downloading files from a UNC share in ASP.NET applications

Table of contents

The scenario

I was working recently in an intranet application that had a download page. The output HTML was similar to the following:

<li><a class="download" href="\\MYSERVER\reports 2011\report1.zip" title="Report 1">Report 1</a></li>
<li><a class="download" href="\\MYSERVER\reports 2011\report2.zip" title="Report 2">Report 2</a></li>
<li><a class="download" href="\\MYSERVER\reports 2011\report 3.zip" title="Report 3">Report 3</a></li>
<li><a class="download" href="\\MYSERVER\reports 2011\report 4.zip" title="Report 4">Report 4</a></li>
<li><a class="download" href="\\MYSERVER\reports 2011\report 5&6.zip" title="Report 5&6">Report 5&6</a></li>

This was working fine in IE9, but not in other browsers. There was no action using Google Chrome, and using Firefox there was an error (HTTP Error 400 – Bad Request).

I tried to convert the file path to a file URI but it didn’t fix it. It continued to work on IE only.

<li><a class="download" href="file://MYSERVER/reports 2011/report1.zip" title="Report 1">Report 1</a></li>
<li><a class="download" href="file://MYSERVER/reports 2011/report2.zip" title="Report 2">Report 2</a></li>
<li><a class="download" href="file://MYSERVER/reports 2011/report 3.zip" title="Report 3">Report 3</a></li>
<li><a class="download" href="file://MYSERVER/reports 2011/report 4.zip" title="Report 4">Report 4</a></li>
<li><a class="download" href="file://MYSERVER/reports 2011/report 5&6.zip" title="Report 5&6">Report 5&6</a></li>

The solution was to create a custom ASP.NET download page. I used also jquery on the client side.

Step 1: Using jquery on the client side

The first step was to add an event handler to the download links. The request URI is encoded and is sent as a parameter to the download page. Creating an hidden iframe and setting the src attribute with the download link allows the file to be downloaded asynchronously.

$("a.download").bind("click", function (e) {
    e.preventDefault();
    var requestedFile = encodeURIComponent($(this).attr('href'));

    var iframe = document.createElement("iframe");
    iframe.src = 'Download.aspx?file=' + requestedFile;
    iframe.style.display = "none";
    document.body.appendChild(iframe); // triggers download page
});

Make sure you use encodeURIComponent function to encode special characters in the filename.

Step 2: Create an ASP.NET download page

This is the source code of the download page:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        string requestFile = Request.QueryString["file"];

        if(string.IsNullOrEmpty(requestFile))
        {
            throw new FileNotFoundException("File to download cannot be null or empty");
        }

        // Get file name from URI string in C#
        // http://stackoverflow.com/a/1105614
        var uri = new Uri(requestFile);
        string filename = Path.GetFullPath(uri.LocalPath);
        var fileInfo = new FileInfo(filename);

        if(!fileInfo.Exists)
        {
            throw new FileNotFoundException("File to download was not found", filename);
        }


        // get content type based on file extension. Example:
		// http://stackoverflow.com/a/691599
        Response.ContentType = GetContentType(fileInfo.Extension);

        Response.AddHeader("Content-Disposition", 
                           "attachment; filename=\"" + fileInfo.Name + "\"");
        Response.WriteFile(fileInfo.FullName);
        Response.End();
    }
    catch(ThreadAbortException)
    {
        // ignore exception
    }
    catch(FileNotFoundException ex)
    {
        Response.StatusCode = (int) System.Net.HttpStatusCode.NotFound;
        Response.StatusDescription = ex.Message;
    }
    catch(Exception ex)
    {
        Response.StatusCode = (int) System.Net.HttpStatusCode.InternalServerError;
        Response.StatusDescription = string.Format("Error downloading file: {0}", ex.Message);
    }
}

Some notes:

This is necessary in order to make the download work with a UNC share (\\MYSERVER\….) or a file URI (file://….)

    var uri = new Uri(requestFile);
    string filename = Path.GetFullPath(uri.LocalPath);

To avoid filename truncating, it’s necessary to wrap the filename with quotes

    Response.AddHeader("Content-Disposition", 
                       "attachment; filename=\"" + fileInfo.Name + "\"");

References

Technorati Tags: , , ,

ASP.NET Web Services Dependency Injection using Unity

Recently, I had to setup Unity IoC container in an existing ASP.NET 3.5 Web Forms application. This application had not only web pages (.aspx files), but also some Web Services (.asmx files). After some research, I found out David Hayden’s screencast: Unity IoC and ASP.NET screencast – Dependency Injection into Web Pages.

You can use a similar technique for your Web Services – that’s what I’ll show you in this article.

Table of contents

Example – Adding logging to your application

You have the following interface and its implementation:

public interface ILogger
{
    void Write(string message);
}

public class DebugLogger : ILogger
{
    public void Write(string message)
    {
        Debug.WriteLine(message);
    }
}

 

Step 1: Setting up the container in Global.asax

The first step is to setup Unity Container in Global.asax file. This is a good place to do it because it can be accessed either by web pages or by web services.
The CreateContainer() method is the place where the dependencies are specified.

public class Global : HttpApplication, IContainerAccessor
{
    private static IUnityContainer _container;

    public static IUnityContainer Container
    {
        get
        {
            return _container;
        }
        private set
        {
            _container = value;
        }
    }

    IUnityContainer IContainerAccessor.Container
    {
        get
        {
            return Container;
        }
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        CreateContainer();
    }

    protected virtual void CreateContainer()
    {
        IUnityContainer container = new UnityContainer();
        container.RegisterType<ILogger, DebugLogger>();
        
        Container = container;
    }
}

 

Step 2: Creating a base class for the services

Create a generic BaseService that all your services will inherit from. The dependencies will be injected when you create an instance of the service (default constructor).

public abstract class BaseService<T> : System.Web.Services.WebService where T : class
{
    public BaseService()
    {
        InjectDependencies();
    }

    protected virtual void InjectDependencies()
    {
        HttpContext context = HttpContext.Current;

        if (context == null)
            return;

        IContainerAccessor accessor = context.ApplicationInstance as IContainerAccessor;

        if (accessor == null)
            return;

        IUnityContainer container = accessor.Container;

        if (container == null)
            throw new InvalidOperationException("Container on Global Application Class is Null. Cannot perform BuildUp.");

        container.BuildUp(this as T);
    }
}

 

Step 3: Setting up the services

Now all you need to do is to inherit from the BaseService and invoke its base constructor.
Don’t forget to add the [Dependency] attribute to your dependency, and it has to be public.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DummyService : BaseService<DummyService>
{
    [Dependency]
    public ILogger Logger
    {
        get;
        set;
    }

    public DummyService() : base()
    {
    }

    [WebMethod]
    public string HelloWorld(string name)
    {
        string message = string.Format("Hello World, {0}!", name);

        this.Logger.Write(message);

        return message;
    }
}

That’s it! Now you just need to compile and run the application and see it in action :)

Feel free to download the demo application

References

Downloads

Download the demo project (VS2010): UnityAsmxWebServices.zip

Entity Framework and T4: Generating GetHashCode() for your entities

A simple Entity Framework T4 template that generates GetHashCode() method

Table of contents

GetHashCode() overview

A hash code is a numeric value that is used to identify an object during equality testing, and it can also serve as an index for an object in a collection, such as a List, Dictionary or HashTable.

The problem is that the default .NET implementation of GetHashCode() does not guarantee unique values for different values, so you should override this method and provide your own implementation.

Some rules for implementing GetHashCode() are:

  • If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.
  • The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state
  • For the best performance, a hash function must generate a random distribution for all input
  • Implementations of the GetHashCode method must not result in circular references (it can lead to a StackOverflowException).
  • Implementations of the GetHashCode method must not throw exceptions.

This is a short overview of GetHashCode() method, taken from MSDN. You can read more here: Object.GetHashCode Method

Implementing GetHashCode()

I found a great post on StackOverflow on this topic. Jon Skeet has provided a good and simple implementation. Basically, you need to use prime numbers like 17, 23, 29, 31 in the hash code calculation. I decided to include also the type of the object in the calculation, because two objects with identical properties/values can return the same hash code.

public class Organisation
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Country { get; set; }

	public override int GetHashCode()
	{
		unchecked
		{
			int multiplier = 31;
			int hash = GetType().GetHashCode();

			hash = hash * multiplier + Id.GetHashCode();
			hash = hash * multiplier + (Name == null ? 0 : Name.GetHashCode());
			hash = hash * multiplier + (Country == null ? 0 : Country.GetHashCode());

			return hash;
		}
	}
}

Using T4 templates to generate GetHashCode()

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:

T4 (Text Template Transformation Toolkit) Code Generation – Best Kept Visual Studio Secret

I’ve modified the ADO.NET C# POCO Entity Generator template to generate GetHashCode() method for each entity in the model. Feel free to download the demo project (VS2010).

References

Downloads

Download the demo project (VS2010): T4-GetHashCode.rar

Technorati Tags: , , , ,

Entity Framework and T4: Generate Specification Objects for your entities

Learn how to use 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 define 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):

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

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

A generic Specification class

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

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

Performing validation

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

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:

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:

T4 (Text Template Transformation Toolkit) Code Generation – Best Kept Visual Studio Secret

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:


The previous specifications can now be written like this:

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:

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

[1] Specification Pattern

[2] Specification (Martin Fowler/Eric Evans)

[3] T4 (Text Template Transformation Toolkit) Code Generation – Best Kept Visual Studio Secret

[4] LINQ to Entities: Combining Predicates

[5] Implementing ISession in EF4

Downloads

Download the demo project: MusicStore-T4-Specification.rar


ASP.NET MVC Localization: Generate resource files and localized views using custom templates

Use ASP.NET MVC T4 custom templates to generate resource files and localized views.

Table of contents

Overriding ASP.NET MVC custom templates

The Add View dialog perform code generation that use T4 templates behind the scenes. These templates can be modified to customize the generated code from these tools. You can also add custom templates.

Basically you have to copy the default templates to your project. This is the path:

[Visual Studio Install Directory]\Common7\IDE\ItemTemplates\[CSharp | VisualBasic]\Web\MVC\CodeTemplates\

I have created a custom template that generates resource files (.resx), and I have modified the default templates to use the generated resource files.

Views Templates

You can find more information here:

T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers

Understanding .resx file format

From MSDN [1]:

The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags. One advantage of a .resx file is that when opened with a text editor (such as Notepad or Microsoft Word) it can be written to, parsed, and manipulated.(…) Each entry is described as a name/value pair.(…) When a string is added to a .resx file, the name of the string is embedded in a <data> tag, and the value is enclosed in a <value> tag

For a resource file like this:

You have the corresponding XML:

I created a custom template that generates the resource file in the specified format.

Continue reading

Entity Framework and T4: Generate Query Objects on the fly, part 1

Generate Query Objects on the fly for your Entity Framework entities using T4 templates. Don’t worry about LINQ, let the objects do all the work for you.

Table of contents

  • Configuration
  • References
  • Downloads
  • I’ve read some stuff about T4 templates in the last 2-3 years, but only recently I decided to give it a try. My first attempt was to generate Query Objects for Entity Framework, that’s what I’ll talk about in this article – what’s their purpose and how to use them.

    In part 2 I’ll create a demo ASP.NET MVC application that uses query objects created with this template. I already have another T4 template that creates javascript objects for my entities, and I’m developing a custom ASP.NET view template for those objects.

    Many thanks to Colin Meek [4], his work has really helpful.

    What is a Query Object?

    A Query Object is an object that represents a database query [1]:

    A Query Object is an interpreter [Gang of Four], that is, a structure of objects that can form itself into a SQL query. You can create this query by referring to classes and fields rather than tables and columns. In this way those who write the queries can do so independently of the database schema and changes to the schema can be localized in a single place.

    Assuming that you have a repository like this (I’m using this implementation):

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

    Instead of:

    var albuns = from x in repository.All<Album>()
                     where x.Artist.Name == "Metallica"
                     && x.Genre.Name.Contains("Metal")
                     && x.Price >= 5 && x.Price
                     select x;
    

    You can do this way:

    var search = new AlbumSearch();
    search.PriceFrom = 5;
    search.PriceTo = 10;
    search.Artist = new ArtistSearch(){ Name = "Metallica" };
    search.Genre = new GenreSearch(){ NameContains = "Metal" };
    
    var albuns = from x in repository.All<Album>(search.GetExpression())
                      select x;
    

    Continue reading