protected virtual void OnContextCreating(LinqDataSourceContextEventArgs e)
 {
     if (ContextCreating != null)
     {
         ContextCreating(this, e);
     }
 }
        protected virtual void OnContextCreating(LinqDataSourceContextEventArgs e)
        {
            EventHandler <LinqDataSourceContextEventArgs> handler = (EventHandler <LinqDataSourceContextEventArgs>)Events[EventContextCreating];

            if (handler != null)
            {
                handler(this, e);
            }
        }
        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);
        }
Пример #4
0
		protected virtual void OnContextCreating (LinqDataSourceContextEventArgs e)
		{
			if (ContextCreating != null)
				ContextCreating (this, e);
		}
 protected void LinqDataSource1_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
 {
     e.ObjectInstance = new PhotoAlbumDataSource(this.CurrentPath);
 }
 protected void lds_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
 {
     e.ObjectInstance = new PubsDataContext();
 }
 protected void ShoppingCartLinqDataSource_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
 {
     e.ObjectInstance = new ShoppingCartContext(Context.User.Identity.Name);
 }
 protected virtual void OnContextCreating(LinqDataSourceContextEventArgs e) {
     EventHandler<LinqDataSourceContextEventArgs> handler = (EventHandler<LinqDataSourceContextEventArgs>)Events[EventContextCreating];
     if (handler != null) {
         handler(this, e);
     }
 }
        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;
        }