Home » none » Multi-layer LINQ via Extension Points

Multi-layer LINQ via Extension Points

As promised a few days ago, I will be covering multi-layered architecture as it relates to LINQ.

The first thing we need to evaluate is how Microsoft intended multi-layered architecture to work.  I mean, if we want to know how to do this, the best place to start is with the people who created the technology in the first place, right?

So, remember when we discussed partial functions?  Partial functions are the key to understanding the multi-layer capabilities of LINQ.

The LINQ to SQL classes are composed of the DataContext class and the class that represents a row in each table.  We’ll call that the Table class for now.  Each of these classes have a set of partial functions.  The DataContext class has the following partial functions:

  • partial void OnCreated();
  • partial void InsertTable(Table instance);
  • partial void UpdateTable(Table instance);
  • partial void DeleteTable(Table instance);

where Table represents a table class.  You would, therefore have an Insert, Update and Delete partial method for each table in your DataContext.

The table classes also have a set of partial methods defined for them.

  • partial void OnLoaded();
  • partial void OnValidate(System.Data.Linq.ChangeAction action);
  • partial void OnCreated();
  • partial void OnFieldChanging(typeName value);
    partial void OnFieldChanged();

So you can write code that will fire when the row is created, loaded, or validated as well as when the field is being set, or right after each field has been set.  Just to avoid any confusion, loaded gets called right after the row is filled from the database and validated is called right before it gets saved back.

So, here’s what you might be able to achieve with this.

  • Create special Insert, Update and Delete handlers for tables that need special handling.  For example, maybe you can’t implement cascading delete at the database level for whatever reason, you could handle it in code during the delete event.
  • Do data transformation on rows when they are loaded and transform back when the row is saved.
  • Validate data as you are changing the record.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Is LINQ Multi Layered?Is LINQ Multi Layered? Several weeks ago, we discussed the point of Multi-Layered Architectures.  We discussed what a multi-layered architecture looks like, and the problems it solves. Today, what I want to […]
  • Partial Classes and Partial MethodsPartial Classes and Partial Methods In order to understand how Microsoft intends LINQ to be used in a multi layered architecture, we first need to take a look at partial classes, which have been around since .NET 2.0, and […]
  • Computed Columns Using LINQ to SQL ClassesComputed Columns Using LINQ to SQL Classes Last week we looked at the extension points Microsoft has wired into the LINQ to SQL classes and how they can be used to achieve some of the capabilities of the Business Logic Layer (BLL) […]
  • Document/View All Over Again…Document/View All Over Again… Back when we programmed in C++ using the Microsoft Foundation Classes (MFC) we had a framework that we programmed in called "Document/View." The question I keep asking myself as I think […]
  • Using StoredProcedures with LINQ 2 SQL ClassesUsing StoredProcedures with LINQ 2 SQL Classes While it is true that LINQ will allow you to write all of your data access in .NET without writing a line of SQL, many organizations have already determined that using stored procedures to […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.