views:

357

answers:

2

Hi there. I am starting a new Winforms application, and I've always used traditional ADO.NET methods in my DAL (...yes, hardcoding!)

I have used Linq on many ocassions previously, but more for adhoc queries, so I have some experience with it and I loved how easy it was to write querying code. What I would like to perhaps do is replace my existing DAL with pure LINQ queries. I know that they may be areas of concerns with this, which is why I need your help.

If I had to do things like how I always had done in the past, I would structure my app like:

  • [AppName].ClientUI --> desktop client Presentaion layer
  • [AppName].WebUI --> web Presentation layer
  • [AppName].DAL --> ADO.NET Data access layer
  • [AppName].BLL --> Business logic layer (validation, extra calcs, + manager classes)
  • [AppName].BE --> Business Entities containing business objects and collection classes

To be honest, I've used this always in web apps and had never done an n-layered Winforms app before.

If I want to replace my old DAL methods with LINQ ones, what challenges am I in store for.

Finally, is it even recommended to use LINQ in a multi-layered app? Sometimes I think that using the old ADO.NET methods is still better...more work, but better. I mean - and correct me if Im wrong - at the core of all these new technologies (that are meant to make our work as developers better) are they not all still making use of traditional ADO.NET???

Hope someone can shed some strong light on this! :)

+2  A: 

I've done it both ways.

You are right, rolling an ADO.NET DAL by hand is more work, so do you get a performance benefit for that additional work? Yes, when you use Linq to SQL classes, you will take about 7% to 10% off the top as overhead. However, for that small overhead, you get a lot of benefits:

  • Lazy Loading (which provides performance increases by deferring execution of queries until they are actually needed)
  • CRUD for free, with built-in concurrency handling
  • Linq, IQueryable, and all of the goodness that provides
  • Partial classes allow you to insert business logic and validation int your Linq to Sql classes, without worrying about the Linq to SQL code generator overwriting them.

Of course, if you don't need any of these things, then Linq to SQL might seem like a luxury. However, I find that Linq to SQL is easier to maintain, especially since the DAL classes can be regenerated if the database changes.

And yes, Linq to SQL uses ADO.NET under the hood.

Linq to SQL's multi-tier story is less clear, in large part due to the way the DataContext object needs to be handled. I suggest checking out this CodePlex project:

An Example of a Multi Tier Architecture for Linq to Sql
http://www.codeplex.com/MultiTierLinqToSql

Robert Harvey
Hi robert and thanks for the reply! I figured as much!! all of these new technologies still adopt it under the hood. But u make a lot of sense with regards to the benefits versus the slight addiotnal overhead. Let me just think about this a few minutes and I'll post back here shortly. Basically, I'll pitch u my current setup, and hopefully, u can advise me further.
Shalan
+2  A: 

For a straight forward winforms client app that connects directly to the database, Linq is a good replacement for ADO.NET, there are very few gotchas that you will come up against. Use a tool like SQLMetal (Or the designer that's built in with VS2008) to generate your Linq data objects and database connection classes. If you want use the generated objects as your "BE" objects you can just copy this stuff and move it into what ever assembly you want (if you want to separate the assemblies). Or alternatively you can just provide separate "Business entities" and a translation layer that copies data from the BEs to the Linq generated objects and back again.

The one gotcha that I have come across with Linq a few times is that it doesn't have very good support for disconnected architectures. For example if you wanted to have your DAL on a server and have all your client apps connect to it, you will hit on problems if you just let your linq objects be transfered across the server.

If you do choose to have separate business entities (or have a disconnected architecture) you will find you have to carefully manage disconnecting the Linq objects from the data context, and then reattaching them when you are ready to save/update. It's worth doing some prototyping in this area first to make sure you understand how it works.

The other thing that often trips people up is that linq queries are not executed immediately against the database, they are only executed as the data is needed. Watch out for this, as it can catch you out if you aren't expecting it (and it's hard to spot when debugging because when you look at your linq query in the debugger it will execute to get the data).

It's also worth considering the Entity framework as an alternative of linq2sql (you can still to linq2EF queries). The EF is a more complete ORM and has better support for mapping related tables to multiple objects, but still suffers from poor support for disconnected apps. The EF in .net 4.0 is supposed to have better support for disconnected architectures.

Simon P Stevens
Hi Simon and thanks for the in-depth reply! A few days ago I came accross "http://www.codeproject.com/KB/linq/LINQ2SQLNTiers.aspx?display=PrintAll" which described similarly what u r talking about. I am however yet to understand in lay-mans terms what the difference is between disconnected and connected architectures. Could you perhaps clear the air about this so that I can fully understand your answer? Im choosing to not separate the DAL/BLL,etc across physical tiers, but rather for *good* code organisation and maintenance, keep them all on the client side. Is this a good idea?
Shalan
Disconnected in practise means that your objects are serialized over some gap between the client and server as messages are sent. Usually it means you have 1 app server often used by multiple clients. In a connected architecture the objects are the same object throughout the whole application, usually in a connected architecture the client application talks directly to a database server with no app server in between. Keeping the BLL/DAL all on the client side is a fine solution if that is the best thing in your circumstances. Try asking these as new questions. It's hard to answer in 600 chars.
Simon P Stevens
Thanks! All layers except for the actual Data Layer will reside on each client. So typically with a client-server architecture, and with the current setup, each client will transact with a central database server over an internal network. Therefore by your description, even though my BLL/DAL reside on the client, I imagine that my objects in this instance are (from the DAL) being populated on the server and then returned to the BLL on the client, right? In that case, I would be dealing with a disconnected arch.
Shalan
No. This sounds connected to me. Your DAL is on the client yes? which connects directly to the database server. The DAL creates and populates the objects from the database. This is connected as far as your application logic is concerned. Linq will work fine with this setup because your Linq objects can remain always connected to the data context object, and you will only ever have 1 data context object which will be re-used for every transaction.
Simon P Stevens
Makes sense. Thanks Simon!
Shalan