net

.NET 3.5 Linq to Sql and IBatis.NET

I've been looking at some of the new features in the .NET 3.5 framework and have found Object Relation Mapping (ORM) very similar to IBatis.NET and NHibernate. In .NET 3.5, they use attribute based mapping between the underlying data table and entity class (Domain Object). For instance

using System.Data.Linq;
using System.Data.Linq.Mapping;
[Table="Books"]
public class Book {
    [Column="Name"]
    public string _Name;
    [Column="ISBNNo"]
    public string _isbn;
    ...
}

What you notice is the the class Book is mapped to a table called Books using the Table attribute and particular table columns are mapped using the column attribute on the Book class instance fields. Compared with IBatis.NET XML files; you would have to:

  1. Reference the Book class using the <typedalias type="Namespace.Book, Namespace"/>
  2. Create a ResultMap that exposes the Book class internal fields using <property name="ISBN" Column="ISBN">
  3. Create the sql statement that references the resultmap for the Book class so IBatis.NET and hydrate the results. Below is sample sqlmap.xml file for Ibastis.net
<sqlMap namespace="Surveys" xmlns="http://ibatis.apache.org/mapping">
    <alias>
        <typeAlias alias="Book" type="Namespace.Books, AssemblyName"/>
    </alias>
    <resultMaps>
        <resultMap id="BookMap" class="Book">
           <result property="Name" column="Name"/>
            <result property="ISBN" column="ISBN"/>
        </resultMap>
    </resultMaps>
    <statements>
        <select id="GetBooks" parameterClass="map" resultMap="BookMap">
            select name, isbn from books
        </select>
    </statements>
</sqlMap>

Furthermore, much of the guesswork required to create the parameterizes queries used to pull information from the database and load a collection of Book objects has been added. Simply creating a new Linq DataContext object, passing the connection string to the database and calling the generic GetTable<T> (where T is class name. in this case Book) is all you need to get the results, versus creating the individual sql in the xml file and calling one of the QueryForObject or QueryForList methods found for IBatis.NET.

Last but not least, Linq provides the programmer with built-in sql like keywords used to build expressions that sort, aggregate and limit the original result set to some more specific or limited result result set without going back to the underlying database.

Although, this is a very simplicity view of mapping a database table to .NET class. This does give you a good picture to the additional overhead required to using IBatis.NET xml file mapping versus Microsoft's Linq to Sql attribute base mapping, and demonstrates how ORM is now built right into the .NET Framework. And, you be happy to know that your .NET 3.5 Linq to Sql applications will still run on .NET 2.0 because the new features are specific to the specific 3.5 .NET compiler.

Topics:

Leave a comment

Powered by WP Hashcash

Who is Pathfinder?

Topics

Search

WordPress

Comments about this site: info@pathf.com