Esempio n. 1
0
        public void EntityCreated(Entity e)
        {
            EntityRow entity = e.CastToType <EntityRow>();

            if (entity.IsNotNull() && entity.Row.IsNull())
            {
                if (entity.Table.IsNotNull())
                {
                    if (_dataSet.Tables.Contains(entity.Table.TableName))
                    {
                        DataRow row = _dataSet.Tables[entity.Table.TableName].NewRow();
                        _dataSet.Tables[entity.Table.TableName].Rows.Add(row);
                        entity.Row = row;
                    }
                }
            }
        }
Esempio n. 2
0
        public void Intercept(Castle.DynamicProxy.IInvocation invocation)
        {
            EntityRow entity = invocation.InvocationTarget.CastToType <EntityRow>();

            if (entity.IsNotNull() && entity.Row.IsNotNull())
            {
                string propertyName = invocation.Method.Name;
                if (propertyName.StartsWith("get_") || propertyName.StartsWith("set_"))
                {
                    propertyName = propertyName.Substring(4, propertyName.Length - 4);
                }
                if (invocation.Method.Name.StartsWith("get_"))
                {
                    KeyValuePair <string, EntityProperty> column = entity.GetDataTableColumn(propertyName);
                    if (!string.IsNullOrEmpty(column.Key))
                    {
                        if (entity.Row.Table.Columns.Contains(column.Key))
                        {
                            if (entity.Row[column.Key] != System.DBNull.Value)
                            {
                                invocation.ReturnValue = entity.Row[column.Key];
                            }
                            else
                            {
                                invocation.ReturnValue = column.Value.PropertyType.GetDefaultValue();
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        invocation.Proceed();
                    }
                }
                else if (invocation.Method.Name.StartsWith("set_") && invocation.Arguments.Count() > 0)
                {
                    KeyValuePair <string, EntityProperty> column = entity.GetDataTableColumn(propertyName);
                    if (!string.IsNullOrEmpty(column.Key))
                    {
                        if (entity.Row.Table.Columns.Contains(column.Key))
                        {
                            if (invocation.Arguments[0] != null)
                            {
                                entity.Row[column.Key] = invocation.Arguments[0];
                            }
                            else
                            {
                                entity.Row[column.Key] = System.DBNull.Value;
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        invocation.Proceed();
                    }
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }