Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 存储库模式和作为优化SQL的组合/连接实体_C#_Design Patterns_Repository Pattern - Fatal编程技术网

C# 存储库模式和作为优化SQL的组合/连接实体

C# 存储库模式和作为优化SQL的组合/连接实体,c#,design-patterns,repository-pattern,C#,Design Patterns,Repository Pattern,我正在一个比平常更难操作的系统之上构建一个存储库系统(参考a) 无论如何 在这一点上,我的数据模型相当简单:我有几个国家,每个国家都有0个或更多的机场。以下是我的基本存储库: 公共抽象类存储库:IRepository,其中T:Entity,new() { 受保护的SimpleSQLManager SQLManager=DatabaseManager.Instance.SQLManager; 公共虚拟IQueryable GetAll() { IQueryable all=SQLManager.T

我正在一个比平常更难操作的系统之上构建一个存储库系统(参考a)

无论如何

在这一点上,我的数据模型相当简单:我有几个国家,每个国家都有0个或更多的机场。以下是我的基本存储库:

公共抽象类存储库:IRepository,其中T:Entity,new()
{
受保护的SimpleSQLManager SQLManager=DatabaseManager.Instance.SQLManager;
公共虚拟IQueryable GetAll()
{
IQueryable all=SQLManager.Table().AsQueryable();
全部归还;
}
公共虚拟IQueryable GetAll(表达式谓词)
{
IQueryable all=SQLManager.Table().Where(谓词).AsQueryable();
全部归还;
}
公共T GetById(字符串表名,int-id)
{
返回SQLManager.Query(“从“+tableName+”中选择*,其中Id=?”,Id)[0];
}
}
请忽略丑陋的
GetById()
实现;我在Unity3D(Mono的).NET库上运行这个程序,其中似乎有一个程序,使得目前无法正确执行。不管怎样,这都不是问题所在。:)

现在,一个普通的EntityRepository如下所示(本例中为CountryRepository):

公共类CountryRepository:存储库
{
公共覆盖IQueryable GetAll()
{
返回base.GetAll().OrderBy(c=>c.Name);
}
公共国家/地区GetById(int id)
{
returnbase.GetById(“国家”,id);
}
}
国家实体如下所示:

公共类国家/地区:实体
{
公共可流动机场()
{
返回RepositoryFactory.AirportRepository.GetByCountry(此);
}
}
然后,在我的应用程序中,我可以执行以下操作:

foreach(RepositoryFactory.CountryRepository.GetAll()中的国家c)
{
foreach(c.Airports()中的机场a)
{
// ...
}
}
…这很好用;我很高兴一切都被抽象掉了等等:)

问题在于上述代码为每个国家创建了一个数据库选择,这是非常无效的。这就是我不确定前进方向的地方。我知道如何使用普通的老SQL来实现这一点,但我想采用Linq(或其他“非SQL”)方式

有人能给我指出正确的方向吗


谢谢

在SimpleSQL的文档中,我没有看到任何让SQLLite生成联接的东西——类似于实体框架的
Include
方法

也就是说,您只需通过2个查询将所有机场和国家/地区带入内存,并手动将它们相互挂钩,如下所示:

var airports = RepositoryFactory.AirportRepository.GetAll().ToList();
var countries = RepositoryFactory.CountryRepository.GetAll().ToList();
countries.ForEach(c => c.Airports = airports.Where(a => a.CountryId == c.Id));
请注意,您需要向country类添加属性:

public IEnumerable<Airport> Airports {get;set;}
public IEnumerable机场{get;set;}

我不喜欢,但考虑到你的环境,这可能是唯一的办法。您可以使用泛型进一步抽象连接/映射逻辑,但这是基本思想。

这很有效,而且速度更快,但我也不喜欢它。不过,我想我会暂时坚持下去,除非有人想出更好的办法。好交易。我希望有更多使用SQLLite经验的人有一个更干净的解决方案。也许有一个更好的sql lite ORM会有更多的功能。你问错了问题。为什么需要汇总国家内的机场?哪个命令处理用例需要处理一个国家及其所有机场?域模型不应该是为了满足查询需求而构建的,而应该是commands.Hmm。我不太明白你的意思?你能详细说明一下吗?