Exemple #1
0
        private static void WithDbMetadata(
            Action
            <IEnumerable <Policy>, IEnumerable <Entity>, IEnumerable <EntityCardViewFieldBinding>,
             IEnumerable <EntityListViewFieldBinding>, IEnumerable <EntityLookupViewFieldBinding>,
             IEnumerable <EntityReferenceFieldBinding> > init)
        {
            var entityMappingTypes = typeof(PropertiesOwnerMap).Assembly.GetExportedTypes();
            var abacMappingTypes   = typeof(PolicyMap).Assembly.GetExportedTypes();

            var entityMappingMapper = new ModelMapper();

            entityMappingMapper.AddMappings(entityMappingTypes);
            entityMappingMapper.AddMappings(abacMappingTypes);

            var cfg = GetDbConfiguration();

            cfg.AddMapping(entityMappingMapper.CompileMappingForAllExplicitlyAddedEntitiesFixxed(entityMappingTypes));

            using (var factory = cfg.BuildSessionFactory())
                using (var session = factory.OpenSession())
                {
                    //var policies = session.Query<Policy>().ToFuture();
                    //var entities = session.Query<PropertiesOwner>().FetchMany(po => po.Properties).ToFuture();
                    //var cardViewFieldBindings = session.Query<EntityCardViewFieldBinding>().ToFuture();
                    //var listViewFieldBindings = session.Query<EntityListViewFieldBinding>().ToFuture();
                    //var lookupViewFieldBindings = session.Query<EntityLookupViewFieldBinding>().ToFuture();
                    //var refFieldBindings = session.Query<EntityReferenceFieldBinding>().ToList();

                    //init(policies, entities.OfType<Entity>(), cardViewFieldBindings, listViewFieldBindings, lookupViewFieldBindings,
                    //    refFieldBindings);

                    // Задача - вычитать метаданные за меньшее кол-во запросов с максимальной скоростью
                    // ниже приведено 2 варианта решения:
                    // * большими блоками с явным fetch-ем
                    // * малыми блоками с fetch-ем на hibernate
                    // По результатам тестов пока быстрее читать малыми блоками

                    //var sw = new Stopwatch();
                    //sw.Start();

                    var policies = session.Query <Policy>().ToFuture();

                    /*
                     * // Вариант 1
                     * var associations = session.Query<Association>()
                     *  .Fetch(i => i.RightRole).ThenFetch(i => i.Owner)
                     *  .Fetch(i => i.LeftRole).ThenFetch(i => i.Owner)
                     *  .ToList();
                     * var entities = session.Query<Entity>()
                     *  .FetchMany(po => po.Properties).ThenFetchMany(i => i.Validators)
                     *  .FetchMany(i => i.JoinTables).ThenFetch(i => i.JoinToEntity)
                     *  .ToList();
                     */

                    // Вариант 2
                    var subselects =
                        session.Query <SubselectEntity>().FetchMany(i => i.Properties).FetchMany(i => i.SyncTables).ToList();
                    var entities            = session.Query <TableEntity>().FetchMany(i => i.JoinTables).ToList();
                    var associations        = session.Query <Association>().Fetch(i => i.RightRole).Fetch(i => i.LeftRole).ToList();
                    var propsWithValidators = session.Query <Property>().FetchMany(i => i.Validators).ToList();
                    var propertiesOwners    = session.Query <PropertiesOwner>().FetchMany(i => i.Properties).ToList();
                    var bindings            = session.Query <EntityPropertyBinding>().ToList();

                    init(policies, propertiesOwners.OfType <Entity>(),
                         bindings.OfType <EntityCardViewFieldBinding>(),
                         bindings.OfType <EntityListViewFieldBinding>(),
                         bindings.OfType <EntityLookupViewFieldBinding>(),
                         bindings.OfType <EntityReferenceFieldBinding>());
                }
        }