LINQ to SQL Reflections

23Jan08

I’m currently building a ASP.NET 3.5 website using LINQ as my OR Mapping Framework of choice. There are a few things which I’d like to share. One observation I have is that LINQ, in all its beautiful visual design, does not refresh its schema after I make changes to the database schema. I’ve tried using SQLMetal but in the end, it seems easier for me to just remove the table and add it again through the designer.

Second, it took me a while to work out just where LINQ fit in a multi-tiered application. I know the traditional way is to go with UI – Business layer – Data Access layer. That’s all well and good but LINQ seems to span both the business and data access layer. I found it tricky to separate them out and in the end, I decided to leave them in one layer.

While link generates the strongly-typed classes for you to access tables, very often, the developer will have to create their own classes representing those tables and add custom logic and additional properties. Here’s an example:

[Table(Name="UserInfo")]
    public class UserDetails
    {
        private EntityRef<User> _User;
        private EntityRef<UserMembership> _UserMembership;
        [Column(IsPrimaryKey = true)]
        public int UserInfoID
        {
            get;
            set;
        }

        [Column()]
        public Guid UserID
        {
            get;
            set;
        }

        [Association(Storage="_User", ThisKey="UserID", OtherKey="UserId")]
        public User User
        {
            get
            {
                return this._User.Entity;
            }
            set
            {
                this._User.Entity = value;
            }
        }

        [Association(Storage="_UserMembership", ThisKey="UserID", OtherKey="UserId")]
        public UserMembership UserMembership
        {
            get
            {
                return this._UserMembership.Entity;
            }
            set
            {
                this._UserMembership.Entity = value;
            }
        }

        [Column(Name="Work")]
        public string WorkPhone
        {
            get;
            set;
        }

        [Column(Name="Home")]
        public string HomePhone
        {
            get;
            set;
        }

        public string Address
        {
            get
            {
                StringBuilder addressBuilder = new StringBuilder();
                addressBuilder.AppendLine(AddressLine1);

                if (!string.IsNullOrEmpty(AddressLine2))
                    addressBuilder.AppendLine(AddressLine2);

                addressBuilder.AppendLine(PostCode);

                return addressBuilder.ToString();
            }
        }
In this example, you can see that there is custom logic used for the Address property. As such, in some ways, this object contains business logic information and yet is tied to the database. I've actually gone on to create actual business objects, which I might share if there's enough interest, which contain functions to return all the users, get users by username, etc. But as you can see, the delineation is not so distinct as when you would use a 3-tier approach.

Overall, I found LINQ to be new and interesting. In terms of how it stacks up against other OR mapping frameworks, I’d have to admit that while it might not be as good as some of the commercial ones, it definitely is a bold step by Microsoft into that arena. Personally, I quite like LINQ and I’m going to complete this website with it.

Technorati Tags:
Advertisements


No Responses Yet to “LINQ to SQL Reflections”

  1. Leave a Comment

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


%d bloggers like this: