LINQ 2 SQL:部分类
我需要根据 AppSetting 为我的 DataContext 设置 ConnectionString。我试图通过为每个 DataContext 创建一个部分类来做到这一点。以下是我到目前为止所拥有的内容,我想知道我是否忽略了某些内容?
具体来说,我是否正确处理我的 DataContext(处理、过时等)?
这样做会在更新和插入方面遇到问题吗?文件 BLLAspnetdb.cs
至少有用或必要吗?还是所有这些都应该位于生成的 部分类 AspnetdbDataContext
文件中?
简而言之,这是一个可以接受的结构吗?或者这会在我详细阐述它时给我带来问题吗?
dbml 文件名 = Aspnetdb.dbml
部分类文件名 = 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);
}
}
}
我创建的文件名= 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();
}
}
请参阅上一个问题和已接受的答案,了解我如何到达这里的背景... switch-connectionstrings- Between-local- and-remote-with-linq-to-sql
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该处理您的上下文,因为它是一次性的。每当创建新上下文时,请考虑将语句包装在 using 块中。
我可能会将静态“New”属性表示为“Create”方法。属性创建新对象是不正常的,因此需要使用该代码的其他开发人员可能会对这种行为感到惊讶。
除此之外,你的方法将会起作用。当您获取上下文时,确定连接字符串的逻辑将运行,您将获得使用正确的连接字符串构造的上下文。
如果我是你,我不会让你的所有方法和属性都是静态的。它违背了良好的面向对象设计,并使您非常锁定于特定的实现 - 但是,我想这不在问题的范围内。
You should dispose of your context, since it is disposable. Consider wrapping the statements in a using block whenever you create a new context.
I propably would express the static "New" property as a "Create" method instead. It is not normal for properties to be creating new objects, so other developers that need to use the code might be surprised by the behavior.
Other than that, your approach will work. When you acquire your context, the logic to determine the connection string runs, and you will get a context constructed with the correct connection string.
I would not all of your methods and properties be static if I were you. It defies good OO design, and makes you very locked in to a specific implementation - however, I guess that is not in scope for the question.