Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

LINQ to SQL Classes might not look like business objects but you can make them perform business rules validation. Let's say that you have a class called "Person". Person has FirstName, LastName, DOB (Date of Birth) etc. Now, you don't want two persons to have the same firstname or the lastname ( I know kinda weird requirement). You can use the OnFirstNameChanging method to implement the condition for same first name. Here is the code:

   partial void OnFirstNameChanging(string value)
        {
            // check if the firstname already exists in the database!
            SchoolDataContext school = new SchoolDataContext();

            var query = from p in school.Persons
                                  where p.FirstName == value
                                  select p;

            if (query.Count() > 0) throw new Exception("FirstName already exists in the database");
        }

Now, if the same first name already exists in the database then an exception is thrown. You can do the same thing for last name property.

 partial void OnLastNameChanging(string value)
        {
            SchoolDataContext school = new SchoolDataContext();

            var query = from p in school.Persons
                        where p.LastName == value
                        select p;

            if (query.Count() > 0) throw new Exception("LastName already exists in the database");
        }        

You can also use the OnValidate method of the Person object. In the following code I am checking that if the address has already been added.

  partial void OnValidate(System.Data.Linq.ChangeAction action)
        {
            SchoolDataContext school = new SchoolDataContext();

            if (action == System.Data.Linq.ChangeAction.Insert)
            {
                foreach (var address in this.Addresses)
                {
                    var query = from a in school.Addresses
                                where a.Street == address.Street
                                && a.PersonID == this.PersonID
                                select a;

                    if (query.Count() > 0) throw new Exception("Address already exists");
                }
            }
        }

If your requirment was that two persons cannot have the same FirstName and LastName then you should have used the OnValidate event.

Posted on Sunday, March 30, 2008 3:01 PM | Back to top


Comments on this post: LINQ to SQL Classes and Validation

# re: LINQ to SQL Classes and Validation
Requesting Gravatar...
hi,
I'm new to LINQ. But I think this technology is really cool and I started using it with my projects. Thanks for the code.
Left by Zud PH on Apr 09, 2008 4:11 PM

# re: LINQ to SQL Classes and Validation
Requesting Gravatar...
I get an exception when I try to use the

if (query.Count() > 0) throw new Exception("Address already exists");

Line or equivelent, What am I missing?
Left by Brad G on Apr 22, 2008 3:35 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net