Esempio n. 1
0
        public static string GetCrmEntitySetSchemaMap(this Type crmDataContextType, string crmEntityName)
        {
            return(ObjectCacheManager.Get("microsoft.xrm.portal:entity-schema-map:json:{0}:{1}".FormatWith(crmDataContextType.FullName, crmEntityName), cache =>
            {
                EntitySetInfo entitySetInfo;

                if (!OrganizationServiceContextInfo.TryGet(crmDataContextType, crmEntityName, out entitySetInfo) ||
                    entitySetInfo.Entity == null ||
                    entitySetInfo.Entity.EntityLogicalName == null)
                {
                    throw new InvalidOperationException(@"Unable to retrieve entity set information for entity name ""{0}"".".FormatWith(crmEntityName));
                }

                var properties = entitySetInfo.Entity.AttributesByLogicalName.Values;

                var schemaMap = properties.ToDictionary(info => info.CrmPropertyAttribute.LogicalName, info => info.Property.Name);

                var json = schemaMap.SerializeByJson(new Type[] { });

                return json;
            }));
        }
Esempio n. 2
0
        public override bool TryAssert(OrganizationServiceContext context, Entity entity, CrmEntityRight right, CrmEntityCacheDependencyTrace dependencies)
        {
            var info = CacheInfoFactory.GetCacheInfo(context, entity, right);

            if (!info.IsCacheable)
            {
                return(UnderlyingProvider.TryAssert(context, entity, right, dependencies));
            }

            Stopwatch stopwatch = null;

            return(ObjectCacheManager.Get(info.Key,
                                          cache =>
            {
                stopwatch = Stopwatch.StartNew();

                var value = UnderlyingProvider.TryAssert(context, entity, right, dependencies);

                stopwatch.Stop();

                return value;
            },
                                          (cache, value) =>
            {
                if (dependencies.IsCacheable)
                {
                    cache.Insert(info.Key, value, dependencies);

                    if (stopwatch != null)
                    {
                        cache.AddCacheItemTelemetry(info.Key, new CacheItemTelemetry {
                            Duration = stopwatch.Elapsed
                        });
                    }
                }
            }));
        }
Esempio n. 3
0
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "Begin");

            if (CanSort)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort);
            }

            if (CanPage)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Page);
            }

            if (CanRetrieveTotalRowCount)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
            }

            // merge SelectParameters
            IOrderedDictionary parameters = SelectParameters.GetValues(Context, Owner);

            string sortExpression = GetNonNullOrEmpty(
                arguments.SortExpression,
                parameters[_sortExpressionParameterName] as string,
                SortExpression);
            string entityName = GetNonNullOrEmpty(
                parameters[_entityNameParameterName] as string,
                EntityName);
            string attributeName = GetNonNullOrEmpty(
                parameters[_attributeNameParameterName] as string,
                AttributeName);

            var metadataFlags = MetadataFlags;

            if (parameters.Contains(_metadataFlagsParameterName))
            {
                metadataFlags = (parameters[_metadataFlagsParameterName] as string).ToEnum <EntityFilters>();
            }

            var entityFlags = EntityFlags;

            if (parameters.Contains(_entityFlagsParameterName))
            {
                entityFlags = (parameters[_entityFlagsParameterName] as string).ToEnum <EntityFilters>();
            }

            // raise pre-event
            CrmMetadataDataSourceSelectingEventArgs selectingArgs = new CrmMetadataDataSourceSelectingEventArgs(
                Owner,
                arguments,
                entityName,
                attributeName,
                metadataFlags,
                entityFlags,
                sortExpression);

            OnSelecting(selectingArgs);

            if (selectingArgs.Cancel)
            {
                Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "Cancel");
                return(null);
            }

            // merge event results
            arguments.RaiseUnsupportedCapabilitiesError(this);
            sortExpression = selectingArgs.SortExpression;
            entityName     = selectingArgs.EntityName;
            attributeName  = selectingArgs.AttributeName;
            metadataFlags  = selectingArgs.MetadataFlags;
            entityFlags    = selectingArgs.EntityFlags;

            if (CancelSelectOnNullParameter && string.IsNullOrEmpty(entityName) && string.IsNullOrEmpty(attributeName))
            {
                Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "CancelSelectOnNullParameter");
                return(null);
            }

            IEnumerable result       = null;
            int         rowsAffected = 0;

            try
            {
                if (Owner.CacheParameters.Enabled)
                {
                    var cacheKey = GetCacheKey(metadataFlags, entityFlags, entityName, attributeName, sortExpression);

                    result = ObjectCacheManager.Get(cacheKey,
                                                    cache =>
                    {
                        var metadata = ExecuteSelect(entityName, attributeName, sortExpression, entityFlags, metadataFlags, out rowsAffected);
                        return(metadata);
                    },
                                                    (cache, metadata) =>
                    {
                        if (metadata != null)
                        {
                            var dependencies = GetCacheDependencies(metadata);
                            cache.Insert(cacheKey, metadata, dependencies);
                        }
                    });
                }
                else
                {
                    result = ExecuteSelect(entityName, attributeName, sortExpression, entityFlags, metadataFlags, out rowsAffected);
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Tracing.FrameworkError("CrmMetadataDataSourceView", "ExecuteSelect", "{0}\n\n{1}", ex.Detail.InnerXml, ex);
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmMetadataDataSourceView", "ExecuteSelect", "Exception: {0}", e);

                // raise post-event with exception
                CrmMetadataDataSourceStatusEventArgs selectedExceptionArgs = new CrmMetadataDataSourceStatusEventArgs(0, e);
                OnSelected(selectedExceptionArgs);

                if (!selectedExceptionArgs.ExceptionHandled)
                {
                    throw;
                }

                return(result);
            }

            // raise post-event
            CrmMetadataDataSourceStatusEventArgs selectedArgs = new CrmMetadataDataSourceStatusEventArgs(rowsAffected, null);

            OnSelected(selectedArgs);

            Tracing.FrameworkInformation("CrmMetadataDataSourceView", "ExecuteSelect", "End");

            return(result);
        }