maandag 11 januari 2010

LINQ to SQL is the Data Access Layer (DAL)

The last few weeks I have found the time to take a better look at LINQ. I studied the architecture and the technique in more detail. I have a special interest in LINQ to SQL since I love databases, especially Microsoft SQL Server. I have to admit I really like LINQ. The part I liked most is the native language integration in both C# and VB9. For a developer it feels very natural to use LINQ within the code.

It is really interesting to see what LINQ will bring to the developer community. I personally think that LINQ is the feature in VS2008 that brings real developer productivity. Many language enhancements make the language just more complex, but LINQ is the one that makes it simpler. Increased developer productivity is definitely something we (the software industry) can use. We should ask ourselves the question has our productivity really improved in the last 15 years? Personally I don’t think so. This is off course very bad for the image of the industry. LINQ seems to be a very promising technology to increase developer productivity. At least for the data access part of our applications.

When you start using LINQ it becomes very soon clear that LINQ is much more than only a data access technology. For example each “foreach” statement is a candidate to be replaced by a LINQ query. But the main part of LINQ is focused at data access (at least for me). LINQ has to do with data connections, OR/M, queries and data manipulation. Typically data access layer functionality. Although the technology is interesting the impact of LINQ at the architecture of our applications is of much more interest to me. It really surprises me that most articles and blog posts are about the technology while the design and architecture of an application is also very interesting. (I really like the series of articles Scott Guthrie has written)

After programming LINQ for a while the main question which comes into my mind is: “Is LINQ to SQL the Data Access Layer (DAL)?”. At the moment I really think the answer to this question is “yes”. I think that LINQ will change the design of many .NET applications in the near future. We finally will remove a layer!

What is a DAL?

Great question. When you google for a data access layer definition you will find many different statements en opinions. At Wikipedia you will find some description. Some general points (a.k.a tenetsJ) can be filtered by all the definitions:

- Responsibility for data access (querying & data manipulation)

- Abstraction of underlying data store (e.g. SQL Server)

- Mapping between OO and relation information

- Provides services

In my opinion the data access layer has indeed something to do with all four points listed above. In .NET this will be translated in a few assemblies which are referenced by the business layer.

What has LINQ to do with a DAL?

LINQ to SQL is about data access with a SQL Server. LINQ to SQL provides a model for the OR/Mapping and a data context. Using LINQ to SQL provides you with an easy programming model to access and manipulate data. These are important aspects you have to build in your DAL. So, LINQ is a great technology to use inside your DAL. You design and program the abstraction (classes, interfaces & methods) and provide the services (WCF) the rest is LINQ code.

I really think that I will see many solutions were LINQ indeed is used as described above. This will end up with “empty” methods which are nothing more than a wrapper around a LINQ statement. Something likes this:

public static class DataHelper
{

   public static IQueryable GetAllRestaurants()

   {
      DinnerNowDataContext context = new DinnerNowDataContext();
      return from r in context.Restaurant

      select r;

   }

}

 
Will this type of code really improve our designs? More important, will this type of code really improve the developer productivity? I don’t think so…

Another problem has to do with the LINQ language integration. You can use LINQ very usefully in your business and presentation layer as well. Even when you use a method as above you can still write a LINQ query in the business layer on top of the GetAllRestaurant() method. Is this correct from a architecture point of view? Why not, LINQ is just a great language feature which you use everywhere. But if that is the case, where do you write queries? In the data access layer? Or in the business layer? Or even in the presentation layer? This gives the developer many choices, and choices are bad for productivity!

Is there a better way to use LINQ in our design?

Yes, I do think so. If we start from the point of view that LINQ to SQL is our data access layer. We just build a data context and map our tables to classes with the standard VS2008 functionality. We configure our data store connection by using a configuration file and we are done. You have your data access layer. We simple do not discuss what goes in which layer, that will save a lot of time.

We allow our developers to use LINQ queries all over the place. It is just a language feature that improves the productivity and provides us with more readable code.

That’s it. I think LINQ to SQL is a great DAL (or whatever you wane call it). It is a mind shift. Stop programming useless classes. Start thinking about better software and a better productivity!

SQL Server peer-to-peer replication

In my last post I wrote about scaling SQL Server 2005. One of the most important technologies we have used to scale SQL Server 2005 is peer-to-peer replication.


This type of replication is a special transaction replication type. This type of replication is available in SQL Server Enterprise Edition since the 2005 version. The peer-to-peer replication is responsible for replicating transactions across the configured peers. Transactions marked for replication are transmitted and executed at other SQL Server databases. The peer-to-peer replication makes sure that database A and B stays in sync without circular references. This concept is great to start building a NLB for load balancing. At msdn you can find a good article about setting up a peer-to-peer replication topology.

Before setting up a peer-to-peer replication topology you should realize you are entering the world of SQL Server replication. This means you should be familiar with articles, publishers, subscriber, subscription and SQL Agent for example. Replication is a grown up, but also complex technology of SQL Server 2005. Get to know the basics before starting with peer-to-peer replication.

One of the misunderstandings has to do with the basics of the peer-to-peer replication: transactional replication. Only, and only transactions are replicated (in the right order). When you for example bulk insert a set of data without logging in the transaction log the data is not automatically replicated to the other peers. This means a manual action to get the other peers in sync. The order of the transactions is another concept to take notice of. When transaction A fails to be replicated, for whatever reason all other transactions (later in time) in the log will not be replicated. These transactions “wait” until the problem with transaction A is solved. When you start with peer-to-peer topology, get to know the transactional replication basics.


Once you decided to use a peer-to-peer replication topology, you must start thinking about synchronizing the peers. The replication topology has a snapshot option to get a new peer in sync with the rest. However when the database consists of a fair amount of data I advice to use the backup/ restore technology or simple attach the database. Make sure the schemas between the peers are identical!

Two important design issues are about the number of peers en conflict resolution. Keep the number of peers low. When you see the subscriptions and publication for a four node technology, you can imagine that this can become complex very soon. Microsoft recommends to the keep the number of peers below 12. I personally think 12 is a very high number. An alternative you can find in the second figure. However, this means when node B or C fails the transactions of A don’t reach node D and vice versa. The replication topology in the second figure is much easier to maintain and configure.


An other design issue has to do with conflict detection and resolving conflicts. Most of the cases a conflict situation comes up when somebody updates some data manually. In a normal situation, this data will be replicated as well. However, don’t be surprise when somebody at your organization blows up the whole peer-to-peer replication without even realizing it. The replication monitor can help you to detect problems, but there are no replication tools to help you resolving the problems. You are at your own…in some future post I might talk about resolving problems in a peer-to-peer replication topology.