【问题标题】:Using UnitOfWork Pattern With Compiled Queries对编译查询使用 UnitOfWork 模式
【发布时间】:2013-09-11 18:38:47
【问题描述】:

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

以下是有效的:

        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 类中设置的表并在编译查询处创建断点时,这些表出于某种原因为空,即使它们是在创建类时设置的。是否有可能做到这一点?还是编译后的查询总是需要传入一个datacontext?

提前致谢!

【问题讨论】:

    标签: c# linq-to-sql unit-of-work linq.compiledquery


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多