LINQ 2 SQL: Partial Classes

I need to set ConnectionString for my AppSetting based DataContext. I am trying to do this by creating a Partial Class for each DataContext. Below is what I have so far and I am wondering if I am missing something?

In particular, am I using my DataContext correctly (recycling, persistence, etc.)?

Doing this will I have problems with updates and inserts? Is the file BLLAspnetdb.cs

useful or necessary at least, or should it all be in the generated file partial class AspnetdbDataContext

?

In short, is this an acceptable framework, or will it cause me problems when I design it?

dbml File name = Aspnetdb.dbml

File name Partial class = 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 generated 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 accepted answer for background on how I got here ... switch-connectionstrings-between-local-and-remote-with-linq-to-sql

+2


a source to share


1 answer


You have to get rid of your context as it is disposable. Consider wrapping statements in a used block whenever you create a new context.

I could use the static property "New" instead of the "Create" method. This is not normal for new object creation properties, so other developers who need to use the code might be surprised at the behavior.



Other than that, your approach will work. When you acquire your context, the logic for defining the connection string is executed and you will get the context built with the correct connection string.

I wouldn't all of your methods and properties be static if I were you. It doesn't follow good OO design and makes you very implementation-specific - however, I guess that's not the question.

+1


a source







All Articles