Exemplo n.º 1
0
        private void generateIdentityMapAndTrackingCode(GeneratedMethod sync, GeneratedMethod @async,
                                                        StorageStyle storageStyle)
        {
            if (storageStyle == StorageStyle.QueryOnly)
            {
                return;
            }

            sync.Frames.MarkAsLoaded();
            async.Frames.MarkAsLoaded();

            if (storageStyle == StorageStyle.Lightweight)
            {
                return;
            }

            sync.Frames.StoreInIdentityMap(_mapping);
            async.Frames.StoreInIdentityMap(_mapping);

            if (storageStyle == StorageStyle.DirtyTracking)
            {
                sync.Frames.StoreTracker();
                async.Frames.StoreTracker();
            }
        }
Exemplo n.º 2
0
 public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async,
                          GeneratedMethod sync, int index,
                          DocumentMapping mapping)
 {
     sync.Frames.DeserializeDocument(mapping, index);
     async.Frames.DeserializeDocumentAsync(mapping, index);
 }
Exemplo n.º 3
0
        public void select_columns_with_hierarchy(StorageStyle style, string[] expected)
        {
            theMapping.SubClasses.AddHierarchy();

            theTable.SelectColumns(style)
            .Select(x => x.Name)
            .ShouldHaveTheSameElementsAs(expected);
        }
Exemplo n.º 4
0
        public void basic_select_columns_with_optimistic_versioning(StorageStyle style, string[] expected)
        {
            theMapping.UseOptimisticConcurrency = true;

            theTable.SelectColumns(style)
            .Select(x => x.Name)
            .ShouldHaveTheSameElementsAs(expected);
        }
Exemplo n.º 5
0
        public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
        {
            if (Member != null)
            {
                return(true);
            }

            return(storageStyle != StorageStyle.QueryOnly && mapping.UseOptimisticConcurrency);
        }
Exemplo n.º 6
0
        public DocumentStorageBuilder(DocumentMapping mapping, StorageStyle style, Func <DocumentOperations, GeneratedType> selectorTypeSource)
        {
            _mapping            = mapping;
            _selectorTypeSource = selectorTypeSource;
            _typeName           = $"{style}{mapping.DocumentType.NameInCode()}DocumentStorage";

            _baseType =
                determineOpenDocumentStorageType(style).MakeGenericType(mapping.DocumentType, mapping.IdType);
        }
Exemplo n.º 7
0
        public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
        {
            if (Member != null)
            {
                return(true);
            }

            return(storageStyle != StorageStyle.QueryOnly);
        }
        public ICacheImplementation Select(TimeSpan durationToCacheItemFor,
                                           StorageStyle cacheStorageStyle,
                                           ExpirationType expirationType)
        {
            Ensure.That(durationToCacheItemFor.IsGreaterThanZero(), "durationToCacheItemFor");

            return _selectorDelegate(durationToCacheItemFor,
                                     cacheStorageStyle,
                                     expirationType,
                                     _implementations);
        }
Exemplo n.º 9
0
        /// <summary>
        ///   Made public to make API more intuitive, even though it opens up
        ///   two routes to do the same thing (this plus the "ICacheKeyCreator.Create"
        ///   method.
        /// </summary>
        public Key(TimeSpan durationToStore, StorageStyle cacheStorageStyle, ExpirationType cacheExpirationType,
                   string friendlyName)
        {
            Ensure.That(durationToStore.IsGreaterThan(TimeSpan.Zero), "durationToStore must be greater than zero")
                .And(friendlyName.IsNotNullOrWhiteSpace(), "friendlyName not supplied")
                .And(friendlyName.Length.IsLessThan(MaxFriendlyNameLength), "friendlyName too long");

            DurationToStore = durationToStore;
            StorageStyle = cacheStorageStyle;
            CacheExpirationType = cacheExpirationType;
            FriendlyName = friendlyName;
        }
Exemplo n.º 10
0
        /// <summary>
        ///   This is the method to be supplied to the constructor of 
        ///   the base type as the delegate to use for cache selection.
        ///   Selects a cache based on the heuristic of duration to 
        ///   store for, followed by the availability of the requested 
        ///   caching functionality. This logic serves as a workable 
        ///   example, rather than a recommended strategy. For example, 
        ///   the fallback case is handled without error or warning.        
        ///   Public rather than protected for testing purposes.
        /// </summary>
        /// <remarks>
        ///   NOTE: BA; should we throw an exception if the user requests 
        ///   a storage strategy that cannot be fulfilled due to 
        ///   limitations of the underlying cache?
        ///   NOTE: BA; Potentially have a flag to indicate strictness of
        ///   choice of cache - i.e. does the cache really need to 
        ///   support all the desired features, or can we substitute 
        ///   them?
        ///   "narrow down choice of caches to those that can be used, 
        ///   then choose the enabled one according to priority".
        /// </remarks>
        /// <param name = "implementations">Ordered by preference of use 
        ///   with highest pref first.</param>
        public static ICacheImplementation SelectCacheImplementation(TimeSpan durationToCacheItemFor,
                                                                     StorageStyle cacheStorageStyle,
                                                                     ExpirationType cacheExpirationType,
                                                                     Dictionary<string, ICacheImplementation>
                                                                         implementations)
        {
            Ensure.That<FriendlyExceptions.ArgumentOutOfRangeException>(durationToCacheItemFor.IsGreaterThanZero(),
                                                                        "durationToCacheFor")
                .And<FriendlyExceptions.ArgumentNullException>(implementations.IsNotNull(), "implementations")
                .And<FriendlyExceptions.ArgumentException>(implementations.Count.Is(2), "implementations")
                .And<MissingCacheImplementationArgumentException>(
                    implementations.ContainsKey(MemcachedImplementationTypeName), "memcached missing")
                .And<MissingCacheImplementationArgumentException>(
                    implementations.ContainsKey(AspDotNetImplementationTypeName), "asp.net data cache missing");

            ICacheImplementation cacheToUse = null;

            //simply tries to select memcached if an extended caching period is requested
            //otherwise attempts to fallback to ASP.NET data cache. This might occur because
            //the caching expiration type is not supported by memcached.
            if (durationToCacheItemFor >= LongTermBoundary)
            {
                if (implementations[MemcachedImplementationTypeName].Supports((cacheExpirationType))
                    && implementations[MemcachedImplementationTypeName].IsEnabled)
                    cacheToUse = implementations[MemcachedImplementationTypeName];

                if (cacheToUse == null
                    && implementations[AspDotNetImplementationTypeName].Supports((cacheExpirationType))
                    && implementations[AspDotNetImplementationTypeName].IsEnabled)
                    cacheToUse = implementations[AspDotNetImplementationTypeName];

                Ensure.That(cacheToUse.IsNotNull(), () => { throw new CacheSelectionException(implementations); });
            }
            else
            {
                if (implementations[AspDotNetImplementationTypeName].Supports((cacheExpirationType))
                    && implementations[AspDotNetImplementationTypeName].IsEnabled)
                    cacheToUse = implementations[AspDotNetImplementationTypeName];

                if (cacheToUse == null
                    && implementations[MemcachedImplementationTypeName].Supports((cacheExpirationType))
                    && implementations[MemcachedImplementationTypeName].IsEnabled)
                    cacheToUse = implementations[MemcachedImplementationTypeName];

                Ensure.That(cacheToUse.IsNotNull(), () => { throw new CacheSelectionException(implementations); });
            }

            return cacheToUse;
        }
Exemplo n.º 11
0
        public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async, GeneratedMethod sync,
                                 int index, DocumentMapping mapping)
        {
            var variableName = "tenantId";
            var memberType   = typeof(string);

            if (Member == null)
            {
                return;
            }

            sync.Frames.Code($"var {variableName} = reader.GetFieldValue<{memberType.FullNameInCode()}>({index});");
            async.Frames.CodeAsync($"var {variableName} = await reader.GetFieldValueAsync<{memberType.FullNameInCode()}>({index}, token);");

            sync.Frames.SetMemberValue(Member, variableName, mapping.DocumentType, generatedType);
            async.Frames.SetMemberValue(Member, variableName, mapping.DocumentType, generatedType);
        }
Exemplo n.º 12
0
        public CacheAspect(Type cacheType,
                           Type keyCreationStrategyType,
                           int durationToStoreInSeconds,
                           StorageStyle storageStyle,
                           ExpirationType expirationType)
        {
            Ensure.That<ArgumentNullException>(cacheType.IsNotNull(), "cacheType")
                .And<ArgumentNullException>(keyCreationStrategyType.IsNotNull(), "keyCreationStrategyType")
                .And<ArgumentException>(durationToStoreInSeconds.IsBetween(0, OneYearInSeconds),
                                        "durationToStoreInSeconds");

            _cacheType = cacheType;
            _keyCreationStrategyType = keyCreationStrategyType;
            _durationToStore = TimeSpan.FromSeconds(durationToStoreInSeconds);
            _storageStyle = storageStyle;
            _expirationType = expirationType;
        }
Exemplo n.º 13
0
        public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async, GeneratedMethod sync,
                                 int index, DocumentMapping mapping)
        {
            var variableName = "correlationId";
            var memberType   = typeof(string);

            if (Member == null)
            {
                return;
            }

            generateIfValueIsNotNull(async, sync, index);

            generateCodeToSetValuesOnDocumentFromReader(generatedType, async, sync, index, mapping, variableName, memberType, Member);

            generateCloseScope(async, sync);
        }
Exemplo n.º 14
0
        public DocumentStorage(StorageStyle storageStyle, DocumentMapping document)
        {
            _mapping = document;

            Fields    = document;
            TableName = document.TableName;

            determineDefaultWhereFragment();

            _idType = PostgresqlProvider.Instance.ToParameterType(typeof(TId));

            var table = _mapping.Schema.Table;

            _selectFields = table.SelectColumns(storageStyle).Select(x => $"d.{x.Name}").ToArray();
            var fieldSelector = _selectFields.Join(", ");

            _selectClause = $"select {fieldSelector} from {document.TableName.QualifiedName} as d";

            _loaderSql =
                $"select {fieldSelector} from {document.TableName.QualifiedName} as d where id = :id";

            _loadArraySql =
                $"select {fieldSelector} from {document.TableName.QualifiedName} as d where id = ANY(:ids)";

            if (document.TenancyStyle == TenancyStyle.Conjoined)
            {
                _loaderSql    += $" and {CurrentTenantFilter.Filter}";
                _loadArraySql += $" and {CurrentTenantFilter.Filter}";
            }

            UseOptimisticConcurrency = document.UseOptimisticConcurrency;


            _setter = LambdaBuilder.Setter <T, TId>(document.IdMember) !;

            DeleteFragment = _mapping.DeleteStyle == DeleteStyle.Remove
                ? (IOperationFragment) new HardDelete(this)
                : new SoftDelete(this);

            HardDeleteFragment = new HardDelete(this);

            DuplicatedFields = _mapping.DuplicatedFields;
        }
Exemplo n.º 15
0
        private Type determineOpenDocumentStorageType(StorageStyle style)
        {
            switch (style)
            {
            case StorageStyle.Lightweight:
                return(typeof(LightweightDocumentStorage <,>));

            case StorageStyle.IdentityMap:
                return(typeof(IdentityMapDocumentStorage <,>));

            case StorageStyle.QueryOnly:
                return(typeof(QueryOnlyDocumentStorage <,>));

            case StorageStyle.DirtyTracking:
                return(typeof(DirtyTrackingDocumentStorage <,>));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 16
0
        public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async,
                                 GeneratedMethod sync, int index,
                                 DocumentMapping mapping)

        {
            if (storageStyle == StorageStyle.QueryOnly)
            {
                return;
            }

            sync.Frames.Code($"var id = reader.GetFieldValue<{mapping.IdType.FullNameInCode()}>({index});");
            async.Frames.CodeAsync($"var id = await reader.GetFieldValueAsync<{mapping.IdType.FullNameInCode()}>({index}, token);");

            if (storageStyle != StorageStyle.Lightweight)
            {
                sync.Frames.Code(IdentityMapCode);
                async.Frames.Code(IdentityMapCode);
            }
        }
Exemplo n.º 17
0
        internal ISelectableColumn[] SelectColumns(StorageStyle style)
        {
            // There's some hokey stuff going here, but older code assumes that the
            // order of the selection is data, id, everything else
            var columns = Columns.OfType <ISelectableColumn>().Where(x => x.ShouldSelect(_mapping, style)).ToList();

            var id      = columns.OfType <IdColumn>().SingleOrDefault();
            var data    = columns.OfType <DataColumn>().Single();
            var type    = columns.OfType <DocumentTypeColumn>().SingleOrDefault();
            var version = columns.OfType <VersionColumn>().SingleOrDefault();

            var answer = new List <ISelectableColumn>();

            if (id != null)
            {
                columns.Remove(id);
                answer.Add(id);
            }

            columns.Remove(data);
            answer.Add(data);

            if (type != null)
            {
                columns.Remove(type);

                // Old code might depend on this exact ordering
                answer.Add(type);
            }

            if (version != null)
            {
                columns.Remove(version);
                answer.Add(version);
            }

            answer.AddRange(columns);

            return(answer.ToArray());
        }
Exemplo n.º 18
0
        public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async,
                                 GeneratedMethod sync, int index,
                                 DocumentMapping mapping)
        {
            var versionPosition = index;//mapping.IsHierarchy() ? 3 : 2;


            async.Frames.CodeAsync($"var version = await reader.GetFieldValueAsync<System.Guid>({versionPosition}, token);");
            sync.Frames.Code($"var version = reader.GetFieldValue<System.Guid>({versionPosition});");

            if (storageStyle != StorageStyle.QueryOnly)
            {
                // Store it
                sync.Frames.Code("_versions[id] = version;");
                async.Frames.Code("_versions[id] = version;");
            }


            if (Member != null)
            {
                sync.Frames.SetMemberValue(Member, "version", mapping.DocumentType, generatedType);
                async.Frames.SetMemberValue(Member, "version", mapping.DocumentType, generatedType);
            }
        }
Exemplo n.º 19
0
 protected IdentityMapDocumentStorage(StorageStyle storageStyle, DocumentMapping document) : base(storageStyle, document)
 {
 }
Exemplo n.º 20
0
 public static string DeriveTypeName(DocumentMapping mapping, StorageStyle style)
 {
     return($"{style}{mapping.DocumentType.ToSuffixedTypeName("DocumentStorage")}");
 }
Exemplo n.º 21
0
 public static string DeriveTypeName(DocumentMapping mapping, StorageStyle style)
 {
     return($"{style}{mapping.DocumentType.Name.Sanitize()}DocumentStorage");
 }
Exemplo n.º 22
0
 public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
 {
     return(mapping.Metadata.LastModifiedBy.EnabledWithMember());
 }
Exemplo n.º 23
0
 public void GenerateCode(StorageStyle storageStyle, GeneratedType generatedType, GeneratedMethod async, GeneratedMethod sync,
                          int index, DocumentMapping mapping)
 {
     setMemberFromReader(generatedType, async, sync, index, mapping);
 }
Exemplo n.º 24
0
 public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
 {
     return(storageStyle != StorageStyle.QueryOnly);
 }
Exemplo n.º 25
0
 public void basic_select_columns(StorageStyle style, string[] expected)
 {
     theTable.SelectColumns(style)
     .Select(x => x.Name)
     .ShouldHaveTheSameElementsAs(expected);
 }
Exemplo n.º 26
0
 public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
 {
     return(true);
 }
Exemplo n.º 27
0
 public SelectorBuilder(DocumentMapping mapping, StorageStyle style)
 {
     _mapping = mapping;
     _style   = style;
 }
Exemplo n.º 28
0
 public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
 {
     return(mapping.Metadata.CorrelationId.EnabledWithMember());
 }
Exemplo n.º 29
0
 public bool ShouldSelect(DocumentMapping mapping, StorageStyle storageStyle)
 {
     return(Member != null);
 }
Exemplo n.º 30
0
 /// <summary>
 ///   Enables the ICacheKeyCreator to be injected into 
 ///   types (constructors cannot be constrained by interfaces).
 /// </summary>
 public Key Create(TimeSpan durationToStore, StorageStyle cacheStorageStyle, ExpirationType cacheExpirationType,
                   string friendlyName)
 {
     return new Key(durationToStore, cacheStorageStyle, cacheExpirationType, friendlyName);
 }