protected override ContextDataSourceContextData CreateContext(DataSourceOperation operation)
        {
            var tablePropertyInfo = this.ContextType.GetProperty(this.EntitySetName);

            var result = new ContextDataSourceContextData(Activator.CreateInstance(this.ContextType));
            result.EntitySet = tablePropertyInfo.GetValue(result.Context, null);

            return result;
        }
Ejemplo n.º 2
0
        protected override object GetSource(QueryContext context)
        {
            ContextDataSourceContextData contextData = CreateContext(DataSourceOperation.Select);

            if (contextData != null)
            {
                // Set the current context
                Context   = contextData.Context;
                EntitySet = contextData.EntitySet;
                return(EntitySet);
            }

            return(null);
        }
Ejemplo n.º 3
0
        protected override int ExecuteDelete(IDictionary keys, IDictionary oldValues)
        {
            ContextDataSourceContextData contextData = null;

            try {
                contextData = CreateContext(DataSourceOperation.Delete);
                if (contextData != null)
                {
                    // Set the current context
                    Context   = contextData.Context;
                    EntitySet = contextData.EntitySet;
                    return(base.ExecuteDelete(keys, oldValues));
                }
            }
            finally {
                DisposeContext();
            }

            return(-1);
        }
        private ContextDataSourceContextData CreateContextAndTableForEdit(DataSourceOperation operation)
        {
            ContextDataSourceContextData contextData = CreateContextAndTable(operation);

            // context data may be null or incomplete if an exception was handled
            if (contextData != null)
            {
                if (contextData.Context == null)
                {
                    return(null);
                }
                if (contextData.EntitySet == null)
                {
                    DisposeContext(contextData.Context);
                    return(null);
                }
                ValidateContextType(contextData.Context.GetType(), false);
                ValidateTableType(contextData.EntitySet.GetType(), false);
            }
            return(contextData);
        }
Ejemplo n.º 5
0
        protected override int ExecuteInsert(IDictionary values)
        {
            ContextDataSourceContextData contextData = null;

            try {
                contextData = CreateContext(DataSourceOperation.Insert);
                if (contextData != null)
                {
                    // Set the current context
                    Context   = contextData.Context;
                    EntitySet = contextData.EntitySet;

                    return(base.ExecuteInsert(values));
                }
            }
            finally {
                DisposeContext();
            }

            return(-1);
        }
        private ContextDataSourceContextData CreateContextAndTableForSelect()
        {
            _isNewContext = false;

            if (_selectContexts == null)
            {
                _selectContexts = new List <ContextDataSourceContextData>();
            }
            else if (_reuseSelectContext && _selectContexts.Count > 0)
            {
                return(_selectContexts[_selectContexts.Count - 1]);
            }

            // context data may be null if an exception was handled
            ContextDataSourceContextData contextData = CreateContextAndTable(DataSourceOperation.Select);

            if (contextData != null)
            {
                if (contextData.Context != null)
                {
                    ValidateContextType(contextData.Context.GetType(), true);
                }
                if (contextData.EntitySet != null)
                {
                    ValidateTableType(contextData.EntitySet.GetType(), true);
                }

                _selectContexts.Add(contextData);

                // context may not be dlinq context or may be null if table was static.
                DataContext dlinqContext = contextData.Context as DataContext;
                if ((dlinqContext != null) && _isNewContext)
                {
                    dlinqContext.ObjectTrackingEnabled = EnableObjectTracking;
                }
                // don't reuse dlinq contexts that cache data or exterior changes will not be reflected.
                _reuseSelectContext = (dlinqContext == null) || !EnableObjectTracking;
            }
            return(contextData);
        }
        private ContextDataSourceContextData CreateContextAndTable(DataSourceOperation operation)
        {
            ContextDataSourceContextData contextData = null;
            bool eventFired = false;

            try {
                LinqDataSourceContextEventArgs contextEventArgs = new LinqDataSourceContextEventArgs(operation);
                OnContextCreating(contextEventArgs);

                contextData = new ContextDataSourceContextData(contextEventArgs.ObjectInstance);
                Type       contextType     = null;
                MemberInfo tableMemberInfo = null;
                if (contextData.Context == null)
                {
                    // construct the context unless accessing a static table for Select.
                    contextType     = ContextType;
                    tableMemberInfo = GetTableMemberInfo(contextType);
                    if (tableMemberInfo != null)
                    {
                        if (MemberIsStatic(tableMemberInfo))
                        {
                            if (operation != DataSourceOperation.Select)
                            {
                                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                                                                  AtlasWeb.LinqDataSourceView_TableCannotBeStatic, TableName, contextType.Name, _owner.ID));
                            }
                        }
                        else
                        {
                            contextData.Context = CreateContext(contextType);
                            _isNewContext       = true;
                        }
                    }
                }
                else
                {
                    // use the manually constructed context.
                    tableMemberInfo = GetTableMemberInfo(contextData.Context.GetType());
                }

                // fetch the table from the context.
                if (tableMemberInfo != null)
                {
                    FieldInfo field = tableMemberInfo as FieldInfo;
                    if (field != null)
                    {
                        contextData.EntitySet = field.GetValue(contextData.Context);
                    }
                    PropertyInfo property = tableMemberInfo as PropertyInfo;
                    if (property != null)
                    {
                        contextData.EntitySet = property.GetValue(contextData.Context, null);
                    }
                }
                if (contextData.EntitySet == null)
                {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                                                      AtlasWeb.LinqDataSourceView_TableNameNotFound, TableName, contextType.Name, _owner.ID));
                }
            }
            catch (Exception e) {
                eventFired = true;
                LinqDataSourceStatusEventArgs createdEventArgs = new LinqDataSourceStatusEventArgs(e);
                OnContextCreated(createdEventArgs);
                OnException(new DynamicValidatorEventArgs(e, DynamicDataSourceOperation.ContextCreate));
                // CreateContextAndTable will return null if this exception is handled.
                if (!createdEventArgs.ExceptionHandled)
                {
                    throw;
                }
            }
            finally {
                if (!eventFired)
                {
                    // contextData can be null if exception thrown from ContextCreating handler.
                    object context = (contextData == null) ? null : contextData.Context;
                    LinqDataSourceStatusEventArgs createdEventArgs = new LinqDataSourceStatusEventArgs(context);
                    OnContextCreated(createdEventArgs);
                }
            }
            return(contextData);
        }
        private ContextDataSourceContextData CreateContextAndTable(DataSourceOperation operation) {
            ContextDataSourceContextData contextData = null;            
            bool eventFired = false;
            try {
                LinqDataSourceContextEventArgs contextEventArgs = new LinqDataSourceContextEventArgs(operation);
                OnContextCreating(contextEventArgs);

                contextData = new ContextDataSourceContextData(contextEventArgs.ObjectInstance);
                Type contextType = null;
                MemberInfo tableMemberInfo = null;
                if (contextData.Context == null) {
                    // construct the context unless accessing a static table for Select.
                    contextType = ContextType;
                    tableMemberInfo = GetTableMemberInfo(contextType);
                    if (tableMemberInfo != null) {
                        if (MemberIsStatic(tableMemberInfo)) {
                            if (operation != DataSourceOperation.Select) {
                                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                    AtlasWeb.LinqDataSourceView_TableCannotBeStatic, TableName, contextType.Name, _owner.ID));
                            }
                        }
                        else {
                            contextData.Context = CreateContext(contextType);
                            _isNewContext = true;                            
                        }
                    }
                }
                else {
                    // use the manually constructed context.
                    tableMemberInfo = GetTableMemberInfo(contextData.Context.GetType());
                }

                // fetch the table from the context.
                if (tableMemberInfo != null) {
                    FieldInfo field = tableMemberInfo as FieldInfo;
                    if (field != null) {
                        contextData.EntitySet = field.GetValue(contextData.Context);
                    }
                    PropertyInfo property = tableMemberInfo as PropertyInfo;
                    if (property != null) {
                        contextData.EntitySet = property.GetValue(contextData.Context, null);
                    }
                }
                if (contextData.EntitySet == null) {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                        AtlasWeb.LinqDataSourceView_TableNameNotFound, TableName, contextType.Name, _owner.ID));
                }
            }
            catch (Exception e) {
                eventFired = true;
                LinqDataSourceStatusEventArgs createdEventArgs = new LinqDataSourceStatusEventArgs(e);
                OnContextCreated(createdEventArgs);
                OnException(new DynamicValidatorEventArgs(e, DynamicDataSourceOperation.ContextCreate));
                // CreateContextAndTable will return null if this exception is handled.
                if (!createdEventArgs.ExceptionHandled) {
                    throw;
                }
            }
            finally {
                if (!eventFired) {
                    // contextData can be null if exception thrown from ContextCreating handler.
                    object context = (contextData == null) ? null : contextData.Context;
                    LinqDataSourceStatusEventArgs createdEventArgs = new LinqDataSourceStatusEventArgs(context);
                    OnContextCreated(createdEventArgs);
                }
            }
            return contextData;
        }