C# 在编译查询中使用UnitOfWork模式
我正在尝试在我的项目中设置一些编译查询,该项目使用工作单元设计模式。在某些请求期间,这些查询会被多次重用,使它们成为编译后的查询有助于提高应用程序的速度。我注意到的一件事是,我必须传入我正在使用的datacontext,而不能使用我在工作单元类中设置的存储库 以下是有效的方法:C# 在编译查询中使用UnitOfWork模式,c#,linq-to-sql,unit-of-work,linq.compiledquery,C#,Linq To Sql,Unit Of Work,Linq.compiledquery,我正在尝试在我的项目中设置一些编译查询,该项目使用工作单元设计模式。在某些请求期间,这些查询会被多次重用,使它们成为编译后的查询有助于提高应用程序的速度。我注意到的一件事是,我必须传入我正在使用的datacontext,而不能使用我在工作单元类中设置的存储库 以下是有效的方法: private static Func<PivotalDataContext, Binary, Binary, Int32?, IQueryable<Location>> GetL
private static Func<PivotalDataContext, Binary, Binary, Int32?, IQueryable<Location>> GetLocationFunc
{
get
{
Func<PivotalDataContext, Binary, Binary, Int32?, IQueryable<Location>> func =
CompiledQuery.Compile(
(PivotalDataContext db, Binary destCityId, Binary stateId, Int32? cityNeoId) =>
(
(from cityDest in db.Destination_Cities
where
cityDest.Destination_City_Id == destCityId || cityDest.Neo_Id == cityNeoId
join destCountry in db.Destination_Countries
on cityDest.Destination_Country_Id equals destCountry.Destination_Country_Id into country
from destCountry in country.DefaultIfEmpty()
join destCont in db.Destination_Continents
on destCountry.Destination_Continent_Id equals destCont.Destination_Continent_Id into continent
from destCont in continent.DefaultIfEmpty()
select new Location
{
CityName = destCity.Name,
CountryName = destCountry.Name,
ContinentName = destContinent.Name
})));
return func;
}
}
private static Func GetLocationFunc
{
得到
{
Func Func=
CompiledQuery.Compile(
(PivotalDataContext数据库,二进制destCityId,二进制stateId,Int32?cityNeoId)=>
(
(来自目的地城市数据库中的cityDest)
哪里
cityDest.Destination_City_Id==destCityId | | cityDest.Neo_Id==cityNeoId
加入db中的目的地国家/地区。目的地国家/地区
在cityDest.Destination\u Country\u Id等于destCountry.Destination\u Country\u Id
来自country.DefaultIfEmpty()中的destCountry
在db.Destination\u大陆加入destCont
在destCountry.Destination\u containment\u Id等于destCont.Destination\u containment\u Id到containment
来自大陆的destCont.DefaultIfEmpty()
选择新位置
{
CityName=destCity.Name,
CountryName=destCountry.Name,
大陆名称=大陆名称
})));
返回函数;
}
}
以下是我想要的:
建造商:
private static IRepository<Destination_Country> _destCountryRepo;
private static IRepository<Destination_Continent> _destContinentRepo;
private static IRepository<Destination_City> _destinationCityRepo;
public LocationService()
{
_destCountryRepo = UnitOfWork.DestCountryRepo;
_destCityRepo = UnitOfWork.DestCityRepo;
_destContinentRepo = UnitOfWork.DestContinentRepo;
_destCountryRepo = UnitOfWork.DestCountryRepo;
}
private static Func<PivotalDataContext, Binary, Binary, Int32?, IQueryable<Location>> GetLocationFunc
{
get
{
Func<PivotalDataContext, Binary, Binary, Int32?, IQueryable<Location>> func =
CompiledQuery.Compile(
(PivotalDataContext db, Binary destCityId, Binary stateId, Int32? cityNeoId) =>
(
(from cityDest in _destCityRepo.Table
where
cityDest.Destination_City_Id == _destCityId || cityDest.Neo_Id == cityNeoId
join destCountry in _destCountryRepo.Table
on cityDest.Destination_Country_Id equals destCountry.Destination_Country_Id into country
from destCountry in country.DefaultIfEmpty()
join destCont in _destContinentRepo.Table
on destCountry.Destination_Continent_Id equals destCont.Destination_Continent_Id into continent
from destCont in continent.DefaultIfEmpty()
select new Location
{
CityName = destCity.Name,
CountryName = destCountry.Name,
ContinentName = destContinent.Name
})));
return func;
}
}
private static IRepository\u destCountryRepo;
私人静态回购;
私人静态IRepository目的地报告;
公共场所服务
{
_destCountryRepo=UnitOfWork.destCountryRepo;
_destCityRepo=UnitOfWork.destCityRepo;
_destContinentRepo=UnitOfWork.destContinentRepo;
_destCountryRepo=UnitOfWork.destCountryRepo;
}
以下是已编译的查询(我已将对表的调用从datacontext替换为在构造函数中设置的表):
private static Func GetLocationFunc
{
得到
{
Func Func=
CompiledQuery.Compile(
(PivotalDataContext数据库,二进制destCityId,二进制stateId,Int32?cityNeoId)=>
(
(来自_destyrepo.Table中的cityDest
哪里
cityDest.Destination_City_Id==_destCityId | | cityDest.Neo_Id==cityNeoId
将destCountry加入到destCountryRepo.Table中
在cityDest.Destination\u Country\u Id等于destCountry.Destination\u Country\u Id
来自country.DefaultIfEmpty()中的destCountry
在_destcontrepo.Table中加入destCont
在destCountry.Destination\u containment\u Id等于destCont.Destination\u containment\u Id到containment
来自大陆的destCont.DefaultIfEmpty()
选择新位置
{
CityName=destCity.Name,
CountryName=destCountry.Name,
大陆名称=大陆名称
})));
返回函数;
}
}
当我尝试使用UnitOfWork类中设置的表并在编译的查询中创建断点时,出于某种原因,这些表是空的,即使它们是在创建类时设置的。有可能这样做吗?还是编译后的查询总是需要传入datacontext
提前谢谢 简短回答:是的,你必须通过考试 代码的结构方式是提供
DataContext
以及其他参数来执行查询。由于要挂起静态属性,因此必须始终传入DataContext
,因为它不是静态的
但是,即使您在非静态上下文中创建了查询,也没有一种适当的情况下会创建DataContext
,并且与查询一样长。您将得到一个长寿命的DataContext
,这不是它的用途,并且会带来很多问题