Contents tagged with LINQ

  • Dynamic connection for LINQ to SQL DataContext

    Tags: .NET 3.5, LINQ, Visual Studio, .NET

    If for some reason you need to specify a specific connection string for a DataContext, you can of course pass the connection string when you initialise you DataContext object.  A common scenario could be a dev/test/stage/live connection string, but in my case its for either a live or archive database.

    I however want the connection string to be handled by the DataContext, there are probably lots of different reasons someone would want to do this…but here are mine.

    • I want the same connection string for all instances of DataContext, but I don’t know what it is yet!
    • I prefer the clean code and ease of not using a constructor parameter.
    • The refactoring of using a constructor parameter could be a nightmare.


    So my approach is to create a new partial class for the DataContext and handle empty constructor in there.

    First from within the LINQ to SQL designer I changed the connection property to None.  This will remove the empty constructor code from the auto generated designer.cs file.


    Right click on the .dbml file, click View Code and a file and class is created for you!


    You’ll see the new class created in solutions explorer and the file will open.

    We are going to be playing with constructors so you need to add the inheritance from System.Data.Linq.DataContext

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2.    {
    3.    }

    Add the empty constructor and I have added a property that will get my connection string, you will have whatever logic you need to decide and get the connection string you require.  In my case I will be hitting a database, but I have omitted that code.

    1. public partial class DataClasses1DataContext : System.Data.Linq.DataContext
    2. {
    3.    // Connection String Keys - stored in web.config
    4.    static string LiveConnectionStringKey = "LiveConnectionString";
    5.    static string ArchiveConnectionStringKey = "ArchiveConnectionString";
    7.    protected static string ConnectionString
    8.    {
    9.       get
    10.       {
    11.          if (DoIWantToUseTheLiveConnection) {
    12.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[LiveConnectionStringKey].ConnectionString;
    13.          }
    14.          else {
    15.             return global::System.Configuration.ConfigurationManager.ConnectionStrings[ArchiveConnectionStringKey].ConnectionString;
    16.          }
    17.       }
    18.    }
    20.    public DataClasses1DataContext() :
    21.       base(ConnectionString, mappingSource)
    22.    {
    23.       OnCreated();
    24.    }
    25. }

    Now when I new up my DataContext, I can just leave the constructor empty and my partial class will decide which one i need to use.

    Nice, clean code that can be easily refractored and tested.

    Share this post :


  • LINQ the lot

    Tags: .NET, LINQ

    LINQ goes so much further than just SQL and XML.

    Something like this is just why I love LINQ, a LINQ query on an ASP.NET ListView controls items.

       1:  Dim items = From lvi In AspNetListViewControl.Items _
       2:              Where CType(lvi.FindControl("DropDownList1"), 
    	                DropDownList).SelectedValue = someIntVar _
       3:     Select lvi)

    Getting a ListViewItem's where the selected value of a DropDownList is set to the value I want.  So simple, yet so powerful.

    The observant among you will notice that this is in, not my native C#!! More on that soon! :)

    Technorati Tags: ,


  • LINQ to SQL: Property Changed / Changing Logging

    Tags: .NET 3.5, LINQ

    I have recently came up against a perfect opportunity to get stuck into LINQ to SQL, its a small project where RAD is top priority.  As often is the case I need to audit changes to properties, normally I would probably do some work in the getter and setter of the property, but of course with LINQ to SQL I don't have access to the properties in the same way.  Looking through the intellisense of the LINQ generated class I can see a PropertyChanged and PropertyChanging event, perfect!  Not exactly.  Here is what is generated for you...

    The event logic...


    The property getter/setter...


    Back to my code...

    As you can see below there are two arguments, as normal! and the PropertyChangingEventArgs has a PropertyName property.  Two problems I have with this; 1. PropertyName is only set on the PropertyChanged event, not PropertyChanging, 2. Is that it? The property name, what about original and current value for starters? Which is exactly what is needed for an audit log.


    Well you could cast out the sender.


    Cool, you now have the class, with your new value but the class doesn't tell you which property has changed, wait, you have the property name, but as a string - yuk!! So in case you haven't figured out where I am going with this, you will need to do some manual and frankly nasty processing in the property changing event to get the the original value and some more manual processing the property changed event to get the current value.  Don't forget that the casting out the sender in the PropertyChanging handler will not be any use as its a reference type and will refer to the same class after the change has happened, so you will have two variables with a reference to the same instance.

    All of this turned out the be a complete waster of thinking as on further investigation I found the GetModifiedMembers method in the "LINQ table" collection, that takes an argument of the type and returns an array of System.Data.Linq.ModifiedMemberInfo objects.  Here you can see...


    Perfect... :)

    .net I salute you!! There is always a way and boy, teasing it out of you is such fun :)


  • 1