none
Simple DAL with L2S RRS feed

  • Question

  • I feel silly asking this question, but I am using L2S to connect to a CRM type database.  I am considering writing my own DAL vs just binding straight to LINQ.  I read that each method in my DAL should create a datacontext query the database and then destroy the datacontext. Conceptually, I don't understand how a query that returns 100 rows from a table with 10000 rows could work. 

    Say I have table contacts and table clients in my database and I want to bind the contacts for a given client to a combobox.  I have an object myContact and when you create one you take the given contactID and ask linq to get it from the database.  I need a collection of myContact. Am I supposed to pull every row from the contacts table and iterate through them looking for the client association?  Or would I just keep a collection of all of myContacts in memory and iterate through that? 

    Obviously I don't have a lot of experience with DALs, especially where SQL is concerned.


    J. Duke Rogers Communicore Technologies & Triangle Forensics
    Friday, January 21, 2011 4:46 AM

Answers

  • First off a drop down of more than 50 items seems to be a bad user experiance and why would one want to do such a thing?

    Linq gives you the ability to Take ( x ) and Skip ( x ) depending on the situation which works well with paging out of the box.

    What you will find is that you can write your own DAL, but it will have to do what Linq to SQL gives you out of the box. Where do you want to spend your development time, on buisiness logic for the application or writing a DAL? That is of course your call.

    As to the actual binding of a combobox, create a key value pair on the fly, Linq can do that (after the Linq 2 sql call for the data) and bind it to the combo box. I provide an example (Linq to Object) which shows such a system:

    C#: How to Load a Winform ComboBox or ListBox and have a Unique Value Associated with the Selected Item
    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by jdroger2 Friday, January 21, 2011 3:07 PM
    Friday, January 21, 2011 2:32 PM
    Moderator

All replies

  • First off a drop down of more than 50 items seems to be a bad user experiance and why would one want to do such a thing?

    Linq gives you the ability to Take ( x ) and Skip ( x ) depending on the situation which works well with paging out of the box.

    What you will find is that you can write your own DAL, but it will have to do what Linq to SQL gives you out of the box. Where do you want to spend your development time, on buisiness logic for the application or writing a DAL? That is of course your call.

    As to the actual binding of a combobox, create a key value pair on the fly, Linq can do that (after the Linq 2 sql call for the data) and bind it to the combo box. I provide an example (Linq to Object) which shows such a system:

    C#: How to Load a Winform ComboBox or ListBox and have a Unique Value Associated with the Selected Item
    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by jdroger2 Friday, January 21, 2011 3:07 PM
    Friday, January 21, 2011 2:32 PM
    Moderator
  • Thanks for your reply OmegaMan.  I was trying to simplify my situation in this example and 100 was probably not a good hypothetical number for a drop down - I'm actually using WPF and its definitely more complex than my example.  What I'm taking from your reply is that it is reasonable to forego the DAL and just access Linq directly.  I'm really not trying to spend time developing something unnecessary so I will skip the DAL.  Thanks for your help!

     


    J. Duke Rogers Communicore Technologies & Triangle Forensics
    Friday, January 21, 2011 3:07 PM
  • Yes you are right in your read of my text.

    There are trade-offs to using Linq-2-SQL and pureists will point that out; but for me the overhead is greately negated by the power given using Linq-2-sql. I have done two asp.net projects and a wpf using Linq to sql (about to try to get a new client to buy off on a Silverlight version) and the ability to process the data and present it to the user without having the DAL (writing stored procs or lengthy sql) allowed the end products to be delivered on time.

    My only thought is you won't be able to get Linq to SQL into a shop that has a DAL in place. They will have too much invested in the current system and will not want to change for a variety of reasons. But if you are building from the ground up, IMHO Linq to Sql is your best bet.


    William Wegerson (www.OmegaCoder.Com)
    Friday, January 21, 2011 4:11 PM
    Moderator
  • thanks!
    J. Duke Rogers Communicore Technologies & Triangle Forensics
    Friday, January 21, 2011 7:32 PM