0

我正在尝试在我的项目中设置一些使用工作单元设计模式的编译查询。这些查询在某些请求期间被多次重用,使它们编译查询有助于加快应用程序的速度。我注意到的一件事是我必须传递我正在使用的数据上下文,而不是能够使用我在工作单元类中设置的存储库。

这是有效的:

        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 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;
                }
            }

当我尝试使用 UnitOfWork 类中设置的表并在编译查询处创建断点时,这些表出于某种原因为空,即使它们是在创建类时设置的。是否有可能做到这一点?还是编译后的查询总是需要传入一个数据上下文?

提前致谢!

4

1 回答 1

1

简短的回答:是的,你必须把它传进去。

您的代码的结构方式是您提供DataContext以及其他参数来执行查询。因为您将它挂在静态属性上,所以您将始终必须传入,DataContext因为它不是静态的。

但是,即使您在非静态上下文中创建了查询,也没有适当的情况DataContext可以创建并与查询一样长。你最终会得到一个长寿的DataContext,这不是它的预期使用方式,并引入了很多问题。

于 2013-09-11T19:18:49.303 回答