Skip to content

Archive

Category: Code

After generating classes and properties using the Entity Framework or Linq2sql you somtimes want to know in your code how big a database field is. Probably that would be for doing some input validation. The best way to do that is of course asking that information from the database.

But wait… Didn’t we made a .dbml which generated a couple of classes for the tables in our database? The information we need should be in one of the files that is generated by the .dbml. When you search through the .cs file for your classes and fields, you will see that the properties that are linked to your databse contain a ColumnAttribute. See this code snippet:

[Column(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int id
{ …

[Column(Name="name", Storage="_name", DbType="VarChar(150) NOT NULL", CanBeNull=false)]
public string name
{ …

Here you see that de database field type is specified in the DbType attribute field. So that is wat we want to know in our code. We just have to find an easy way for getting this information. For this I created a Reflection Utility class. Here is the code:

public static class ReflectionUtility
{
    public static string GetPropertyName<T>(Expression<Func<T>>  expression)
    {
        return ((MemberExpression)expression.Body).Member.Name;
    }
    public static ColumnAttribute GetColumnAttribute<T>(Expression<Func<T>> expression)
    {
        string propertyName = ((MemberExpression)expression.Body).Member.Name;
        Type type = ((MemberExpression)expression.Body).Member.ReflectedType;
        PropertyInfo memberInfo = type.GetProperty(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        object[] attributes = memberInfo.GetCustomAttributes(typeof(ColumnAttribute), false);
        foreach (var attribute in attributes)
        {
            var columnAttribute = attribute as ColumnAttribute;
            if (columnAttribute != null) return columnAttribute;
        }
        return null;
    }
    public static string GetDbType<T>(Expression<Func<T>> expression)
    {
        ColumnAttribute attribute = GetColumnAttribute(expression);
        return (attribute != null) ? attribute.DbType : null;
    }
}

As you can see getting the information can easilly be done with some simple reflection. I even made sure you could query both public and non public properties (internal). After you added the ReflectionUtility class to your solution you can get the DbType of a property or just the name of a property with one simple line of code. Her is a sample of how you can use it:

string fieldName = ReflectionUtility.GetPropertyName(() => (new user()).id);
string dbType = ReflectionUtility.GetDbType(() => (new user()).name);

As you can see you only need one simple line of code for getting the database type information from a property. I am not even using ‘magic strings’ for doing the reflection. I just pass the property itself to the method. The bennefit of this is that after a refactoring (changing the name of a property) you don’t even have to try to find all the property name string and replace them by the new name.

There is one drawback for getting the field type information this way. It is possible to enter something in your .dbml that is different from the actual field type in the database. For instance you could set the size smaller than the actual field size.

Conclusion:

This was a fun exersize. If you think that the DbType information in your .dbml is good, then using this utility class is a very easy way to get the type information. There are a couple of things that could be done for improving this code. For instance you could parse the field type string into an object or you could make a validator method that wil check if the current value is not longer that the field length.

Did you ever have a method that returned a generic list of some type but you actually needed a generic list of one of its interfaces or base class? You probably solved this by cloning the method or by creating a new list while looping through all items. In the article below I will show you a nice and simple generic solution for this problem.

Let’s assume you have a class named Car that has an interface named IVehicle and you also have a class named Plane that also has the interface IVehicle. We also have a data repository that has a method for each of them for retrieving a list of items. Now we want to execute the IVehicle method named Move on all Car objects and Plane objects. We will start by retrieving both lists from our data repository object named MyRepository:

    var cars = MyRepository.GetCars()
    var planes = MyRepository.GetPlains()

Wouldn’t it be nice if we could just concatenate those 2 lists so that we could call the IVehicle method for all of them in one simple loop?

    var vehicles = cars.Concat(planes);
    foreach(var vehicle  in vehicles)
    {
        vehicle.Move();
    }

Unfortunately trying this will give you this error:
Error 1 The type arguments for method ‘System.Linq.Enumerable.Concat(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

If we do want to Concat the two collections together, then we first have to cast both collections to th IVehicle interface. At this moment C# 3.0 does not have a simple option to convert a generic list to one of its interfaces or base classes. The .ConvertAll is not easy to use. Fortunately Microsoft recognized this missing feature and in C# 4.0 you will be able to perform this cast by using Covariance and Contravariance. For now you have to use the difficult .ConvertAll method. The simple helper class below will make this easy for you.

    public static class GenericListConverter
    {
        public static T2 AsType<T1, T2>(this T1 model) where T1 : T2
        {
            return model;
        }
        public static IList<T2> ToListOfType<T1, T2>(this IEnumerable<T1> myList) where T1 : T2
        {
            return (new List<T1>(myList.ToList())).ConvertAll<T2>(AsType<T1, T2>);
        }
        public static IList<T2> ToListOfType<T1, T2>(this IList<T1> myList) where T1 : T2
        {
            return (new List<T1>(myList)).ConvertAll<T2>(AsType<T1, T2>);
        }
    }

Using this class is simple. You can just perform a ToListOfType on any generic IEnumerable or IList.

    var cars = myRepository.GetCars().ToListOfType<Car, IVehicle>();
    var planes = myRepository.GetPlains().ToListOfType<Plain, IVehicle>();

The result of this is that you have 2 generic lists of IVehicle and the Concat will now work.

In this case there is an alternative to the above helper class. It is possible to create a helper method that accept anything that implements the IVehicle interface. The code would then look something like:

    ...
        Move((List<Car>)cars);
        Move((List<Plane>)planes);
    ...
    public static void Move<TItem>(List<TItem> vehicles) where TItem : IVehicle
    {
        foreach (var vehicle in vehicles)
        {
            vehicle.Move();
        }
    }