LINQ 2 SQL: Partial Classes

Posted by Refracted Paladin on Stack Overflow See other posts from Stack Overflow or by Refracted Paladin
Published on 2010-04-13T17:13:22Z Indexed on 2010/04/13 17:33 UTC
Read the original article Hit count: 479

I need to set the ConnectionString for my DataContext's based on an AppSetting. I am trying to do this by creating a Partial Class for each DataContext. The below is what I have so far and I am wondering if I am overlooking something?

Specifically, am I dealing with my DataContext's correctly(disposing, staleness, etc)?

Doing it this way will I have issues with Updates and Inserts? Is the file BLLAspnetdb.cs useful or neccessary in the least or should all of that be in the generated partial class AspnetdbDataContext file?

In short, is this an acceptable structure or will this cause me issues as I elaborate it?

dbml File Name = Aspnetdb.dbml

Partial Class File Name = Aspnetdb.cs

partial class AspnetdbDataContext
{
    public static bool IsDisconnectedUser
    {
        get
        {
            return Convert.ToBoolean(ConfigurationManager.AppSettings["IsDisconnectedUser"]) == true;
        }
    }
    public static AspnetdbDataContext New
    {
        get
        {
            var cs = IsDisconnectedUser ? Settings.Default.Central_aspnetdbConnectionString : Settings.Default.aspnetdbConnectionString;
            return new AspnetdbDataContext(cs);
        }
    }
}

My Created File Name = BLLAspnetdb.cs

public class BLLAspnetdb
{
    public static IList WorkerList(Guid userID)
    {
        var DB = AspnetdbDataContext.New;
        var workers = from user in DB.tblDemographics
                          where user.UserID == userID
                          select new { user.FirstName, user.LastName, user.Phone };

        IList theWorkers = workers.ToList();

        return theWorkers;
    }

    public static String NurseName(Guid? userID)
    {
        var DB = AspnetdbDataContext.New;

        var nurseName = from demographic in DB.tblDemographics
                        where demographic.UserID == userID
                        select demographic.FirstName +" " + demographic.LastName;

        return nurseName.SingleOrDefault();
    }

    public static String SocialWorkerName(Guid? userID)
    {
        var DB = AspnetdbDataContext.New;

        var swName = from demographic in DB.tblDemographics
                        where demographic.UserID == userID
                        select demographic.FirstName + " " + demographic.LastName;

        return swName.SingleOrDefault();
    }
}

see this previous question and the accepted answer for background on how I got to here... switch-connectionstrings-between-local-and-remote-with-linq-to-sql

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about best-practices