[英]Object Orientated Linq2Sql query class
For my internship I'm creating a program that communicates with a database on the background. 对于我的实习,我正在创建一个与后台数据库进行通信的程序。 The program is layered in the MVC(Model-View-Controller) way. 该程序以MVC(模型 - 视图 - 控制器)方式分层。
For the View I want to access the data through something I called the DataAccesLayer(DAL). 对于View我想通过我称之为DataAccesLayer(DAL)的东西来访问数据。 Because that view has minimal knowledge I want it to pass an ID for the query I want to call. 因为该视图具有最少的知识,所以我希望它为我想要调用的查询传递ID。 The calling will be done inside the DAL. 呼叫将在DAL内完成。 Then with the ID I want to ask a class that holds the queries to return the query to then execute it inside the DAL. 然后使用ID我想要一个包含查询的类来返回查询,然后在DAL中执行它。 Picture for visualizing. 用于可视化的图片。
The problem I have now is how to execute the query in my Read function. 我现在遇到的问题是如何在我的Read函数中执行查询。 The code for DAL is the following: DAL的代码如下:
public class DataAccesLayer
{
private Queries queryloader;
private RoadsoftDigitacV8DataContext db;
public DataAccesLayer()
{
queryloader = new Queries();
db = new RoadsoftDigitacV8DataContext();
}
public List Read(int ID)
{
IQueryable query;
query = queryloader.GetQuery(ID);
return query.ToList();
}
}
The Code for the Queries class: Queries类的代码:
public class Queries
{
private Dictionary<int, IQueryable object> queryDict;
private ErrorLoggerWinLog logger;
public Queries()
{
logger = ErrorLoggerWinLog.Instance();
queryDict = new Dictionary<int, IQueryable object>();
queryDict.Add(1, from d in db.Drivers
select d);
}
public object GetQuery(int ID)
{
var query;
if(queryDict.TryGetValue(ID, out query) == false)
{
logger.WriteLine("Queries", "Could not find the query specified", ErrorLoggerWinLog.loggerlevel.Error);
}
return query;
}
}
I'm wondering, is this possible? 我想知道,这可能吗? Right now it doesn't seem to work. 现在它似乎不起作用。 I'm probably forgetting something or missing something important. 我可能忘了什么或遗漏了一些重要的东西。 Does anyone have any experience with this kind of set-up, or should be looking at a totally different solution? 有没有人有这种设置的经验,或者应该看一个完全不同的解决方案?
Edit: Right now it doesn't seem to execute the query, like I'm missing a command in the read function. 编辑:现在它似乎没有执行查询,就像我在读取函数中缺少一个命令。 The Datacontext is filled though, that's being done in a different section of the program. 虽然填充了Datacontext,但它正在程序的不同部分完成。
Edit2: Right now I'm looking into the IRepository Pattern, it's a great learning experience, Thank you all who took time to comment and anwser! 编辑2:现在我正在研究IRepository模式,这是一个很棒的学习经历,非常感谢所有花时间发表评论和回复的人!
At any time your queryDict
has only one element; 您的queryDict
只有一个元素; the only time that GetQuery()
doesn't log an error message and doesn't return null is when you pass 1 to it. GetQuery()
没有记录错误消息并且不返回null的唯一时间是将1传递给它。 as a consequence Read(1)
returns a list of all Drivers, otherwise throws a NullReferenceException
because, well, query
is null. 因此, Read(1)
返回所有驱动程序的列表,否则抛出NullReferenceException
因为query
为null。
You should use something like this: 你应该使用这样的东西:
public class DriversDAL
{
private RoadsoftDigitacV8DataContext db;
public DriversDAL()
{
db = new RoadsoftDigitacV8DataContext();
}
public Driver GetDriver(int ID)
{
return db.Drives.Single(d => d.ID == ID);
}
}
If you want a generic solution, you should use a generic dao and the Repository pattern: 如果您需要通用解决方案,则应使用泛型dao和Repository模式:
How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL 如何使用LINQ to SQL创建通用数据访问对象(DAO)CRUD方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.