private static string FilterAndRecomputeAfterSave(KeepSynchronizedOnChangedItemsInfo info, string uniqueName)
        {
            string recomputeCall;

            if (FilterIsEnumerableGuid(info.UpdateOnChange.FilterType))
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(filteredNew.Union(filterKeepSynchronizedOnChangedItems{0}Old).ToList());";
            else if (info.UpdateOnChange.FilterType == "Rhetos.Dom.DefaultConcepts.FilterSubtype")
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(new Rhetos.Dom.DefaultConcepts.FilterSubtype
                    {{
                        Ids = filteredNew.Ids.Union(filterKeepSynchronizedOnChangedItems{0}Old.Ids).ToList(),
                        Subtype = filteredNew.Subtype,
                        ImplementationName = filteredNew.ImplementationName
                    }});";
            else if (info.UpdateOnChange.FilterType == "FilterAll" || info.UpdateOnChange.FilterType == "Rhetos.Dom.DefaultConcepts.FilterAll")
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(new Rhetos.Dom.DefaultConcepts.FilterAll());";
            else
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(filterKeepSynchronizedOnChangedItems{0}Old);
                _domRepository.{1}.{2}.{5}(filteredNew);";

            return string.Format(
            @"            " + OverrideRecomputeTag(info) + @"
            {{
                var filteredNew = filterLoadKeepSynchronizedOnChangedItems{0}(inserted.Concat(updated).ToList());
                " + recomputeCall + @"

                // Workaround to restore NH proxies after using NHSession.Clear() when saving data in Recompute().
                for (int i=0; i<inserted.Length; i++) inserted[i] = _executionContext.NHibernateSession.Load<{3}.{4}>(inserted[i].ID);
                for (int i=0; i<updated.Length; i++) updated[i] = _executionContext.NHibernateSession.Load<{3}.{4}>(updated[i].ID);
            }}
            ",
                uniqueName,
                info.KeepSynchronized.EntityComputedFrom.Target.Module.Name,
                info.KeepSynchronized.EntityComputedFrom.Target.Name,
                info.UpdateOnChange.DependsOn.Module.Name,
                info.UpdateOnChange.DependsOn.Name,
                EntityComputedFromCodeGenerator.RecomputeFunctionName(info.KeepSynchronized.EntityComputedFrom));
        }
        private static string FilterAndRecomputeAfterSave(KeepSynchronizedOnChangedItemsInfo info, string uniqueName)
        {
            return string.Format(
            @"            {{
                var filteredNew = _filterLoadKeepSynchronizedOnChangedItems{0}(inserted.Concat(updated).ToArray());
                _domRepository.{1}.{2}.{6}(filterKeepSynchronizedOnChangedItems{0}Old{3});
                _domRepository.{1}.{2}.{6}(filteredNew{3});

                // Workaround to restore NH proxies after using NHSession.Clear() when saving data in Recompute().
                for (int i=0; i<inserted.Length; i++) inserted[i] = _executionContext.NHibernateSession.Load<{4}.{5}>(inserted[i].ID);
                for (int i=0; i<updated.Length; i++) updated[i] = _executionContext.NHibernateSession.Load<{4}.{5}>(updated[i].ID);
            }}
            ",
                uniqueName,
                info.EntityComputedFrom.Target.Module.Name,
                info.EntityComputedFrom.Target.Name,
                !string.IsNullOrWhiteSpace(info.FilterSaveExpression) ? (", _filterSaveKeepSynchronizedOnChangedItems" + uniqueName) : "",
                info.UpdateOnChange.DependsOn.Module.Name,
                info.UpdateOnChange.DependsOn.Name,
                EntityComputedFromInfo.RecomputeFunctionName(info.EntityComputedFrom));
        }
        private static string FilterAndRecomputeAfterSave(KeepSynchronizedOnChangedItemsInfo info, string uniqueName)
        {
            string recomputeCall;

            if (FilterIsEnumerableGuid(info.UpdateOnChange.FilterType))
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(filteredNew.Union(filterKeepSynchronizedOnChangedItems{0}Old).ToList());";
            else if (info.UpdateOnChange.FilterType == "Rhetos.Dom.DefaultConcepts.FilterSubtype")
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(new Rhetos.Dom.DefaultConcepts.FilterSubtype
                    {{
                        Ids = filteredNew.Ids.Union(filterKeepSynchronizedOnChangedItems{0}Old.Ids).ToList(),
                        Subtype = filteredNew.Subtype,
                        ImplementationName = filteredNew.ImplementationName
                    }});";
            else if (info.UpdateOnChange.FilterType == "FilterAll" || info.UpdateOnChange.FilterType == "Rhetos.Dom.DefaultConcepts.FilterAll")
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(new Rhetos.Dom.DefaultConcepts.FilterAll());";
            else
                recomputeCall =
                @"_domRepository.{1}.{2}.{5}(filterKeepSynchronizedOnChangedItems{0}Old);
                    _domRepository.{1}.{2}.{5}(filteredNew);";
            
            return string.Format(
                OverrideRecomputeTag(info) + @"
                {{
                    var filteredNew = filterLoadKeepSynchronizedOnChangedItems{0}(inserted.Concat(updated));
                    " + recomputeCall + @"
                }}

                ",
                uniqueName,
                info.KeepSynchronized.EntityComputedFrom.Target.Module.Name,
                info.KeepSynchronized.EntityComputedFrom.Target.Name,
                info.UpdateOnChange.DependsOn.Module.Name,
                info.UpdateOnChange.DependsOn.Name,
                EntityComputedFromCodeGenerator.RecomputeFunctionName(info.KeepSynchronized.EntityComputedFrom));
        }
 public static string OverrideRecomputeTag(KeepSynchronizedOnChangedItemsInfo info)
 {
     return string.Format("/*OverrideRecompute {0}.{1}*/",
         info.KeepSynchronized.GetKey(),
         info.UpdateOnChange.GetAlternativeKey());
 }
        private static string FilterSaveFunction(KeepSynchronizedOnChangedItemsInfo info, string uniqueName)
        {
            return string.Format(
            @"        private static readonly Func<IEnumerable<{0}.{1}>, IEnumerable<{0}.{1}>> _filterSaveKeepSynchronizedOnChangedItems{2} =
            {3};

            ",
                info.EntityComputedFrom.Target.Module.Name, info.EntityComputedFrom.Target.Name, uniqueName, info.FilterSaveExpression);
        }