Exemple #1
0
        public void GetEntity_TypeWithoutDiscriminator()
        {
            _readerMock
            .Expect(mock => mock.GetValue(1))
            .Return("Peter");
            _readerMock
            .Expect(mock => mock.GetValue(2))
            .Return(21);
            _reverseMappingResolverMock
            .Expect(mock => mock.GetMetaDataMembers(typeof(PersonTestClass)))
            .Return(_metaModel.GetTable(typeof(PersonTestClass)).RowType.DataMembers.Where(dataMember => !dataMember.IsAssociation).ToArray());

            var columnIDs = new[]
            {
                new ColumnID("FirstName", 1),
                new ColumnID("Age", 2)
            };

            var rowWrapper = new RowWrapper(_readerMock, _reverseMappingResolverMock);

            var instance = rowWrapper.GetEntity <PersonTestClass> (columnIDs);


            _readerMock.VerifyAllExpectations();
            Assert.That(new PersonTestClass("Peter", 21), Is.EqualTo(instance));
        }
 /// <summary>
 /// Gets the column members.
 /// </summary>
 /// <param name="entityType">Type of the entity.</param>
 /// <param name="metaModel">The meta model.</param>
 /// <returns></returns>
 protected virtual IEnumerable <MemberInfo> GetColumnMembers(Type entityType, MetaModel metaModel)
 {
     foreach (var dataMember in metaModel.GetTable(entityType).RowType.PersistentDataMembers)
     {
         yield return(dataMember.Member);
     }
 }
        private Dictionary <MetaDataMember, Func <object, object> > GetGetterTableMap(Type tableType)
        {
            AttributeMappingSource mappingSource = new AttributeMappingSource();
            MetaModel mapping = mappingSource.GetModel(tableType);
            MetaTable table   = mapping.GetTable(tableType);

            Dictionary <MetaDataMember, Func <object, object> > mappedTable = new Dictionary <MetaDataMember, Func <object, object> >();

            foreach (MetaDataMember dataMember in table.RowType.PersistentDataMembers)
            {
                MemberInfo memberInfo = dataMember.Member;
                Expression <Func <object, object> > getter = null;
                if (memberInfo is FieldInfo)
                {
                    getter = (Expression <Func <object, object> >)(o => ((FieldInfo)memberInfo).GetValue(o));
                }
                else if (memberInfo is PropertyInfo)
                {
                    getter = (Expression <Func <object, object> >)(o => ((PropertyInfo)memberInfo).GetGetMethod().Invoke(o, new object[0]));
                }
                else
                {
                    throw new ApplicationException("GetTableMap could not determine lambda expression for " + memberInfo.GetType() + ".");
                }
                mappedTable.Add(dataMember, getter.Compile());
            }

            return(mappedTable);
        }
        private string GetTableName(Type tableType)
        {
            AttributeMappingSource mappingSource = new AttributeMappingSource();
            MetaModel mapping = mappingSource.GetModel(tableType);
            MetaTable table   = mapping.GetTable(tableType);

            return(table.TableName);
        }
 public override MetaTable GetTable(Type rowType)
 {
     if (_customtables.ContainsKey(rowType))
     {
         return(_customtables[rowType]);
     }
     if (_tableNames.ContainsKey(rowType))
     {
         MetaTable      orgtable   = _orgmodel.GetTable(rowType);
         MetaType       orgrowtype = orgtable.RowType;
         CustomMetaType newRowType = new CustomMetaType(orgrowtype, this);
         _customtables.Add(rowType, new CustomMetaTable(orgtable, this, newRowType, _tableNames[rowType]));
         newRowType.MetaTable = _customtables[rowType];
         return(newRowType.MetaTable);
     }
     return(_orgmodel.GetTable(rowType));
 }
        private ReadOnlyCollection <MetaDataMember> GetTableMetaDataMembers(ITable table, Type dataObjectType)
        {
            DataContext context         = ((ITable)table).Context;
            MetaModel   contextMetaData = context.Mapping;
            MetaTable   tableMetaData   = contextMetaData.GetTable(dataObjectType);
            MetaType    rowMetaData     = tableMetaData.Model.GetMetaType(dataObjectType);

            return(rowMetaData.DataMembers);
        }
Exemple #7
0
        public void SetDataObject(object dataObject, DetailsView detailsView)
        {
            DataObject = dataObject;
            TypeDescriptionProvider typeDescriptionProvider;

            CustomTypeDescriptor = dataObject as ICustomTypeDescriptor;
            Type dataObjectType;

            if (CustomTypeDescriptor == null)
            {
                dataObjectType          = dataObject.GetType();
                typeDescriptionProvider = TypeDescriptor.GetProvider(DataObject);
                CustomTypeDescriptor    = typeDescriptionProvider.GetTypeDescriptor(DataObject);
            }
            else
            {
                dataObjectType          = GetEntityType();
                typeDescriptionProvider = new TrivialTypeDescriptionProvider(CustomTypeDescriptor);
            }

            // Set the context type and entity set name on ourselves. Note that in this scenario those
            // concepts are somewhat artificial, since we don't have a real context.

            // Set the ContextType to the dataObjectType, which is a bit strange but harmless
            Type contextType = dataObjectType;

            ((IDynamicDataSource)this).ContextType = contextType;

            // We can set the entity set name to anything, but using the
            // DataObjectType makes some Dynamic Data error messages clearer.
            ((IDynamicDataSource)this).EntitySetName = dataObjectType.Name;

            MetaModel model = null;

            try {
                model = MetaModel.GetModel(contextType);
            }
            catch {
                model = new MetaModel();
                model.RegisterContext(
                    new SimpleModelProvider(contextType, dataObjectType, dataObject),
                    new ContextConfiguration()
                {
                    MetadataProviderFactory = (type => typeDescriptionProvider)
                });
            }

            MetaTable table = model.GetTable(dataObjectType);

            if (detailsView != null)
            {
                detailsView.RowsGenerator = new AdvancedFieldGenerator(table, false);
            }
        }
        public void SetDataObject(object dataObject, DetailsView detailsView) {

            DataObject = dataObject;
            TypeDescriptionProvider typeDescriptionProvider;

            CustomTypeDescriptor = dataObject as ICustomTypeDescriptor;
            Type dataObjectType;

            if (CustomTypeDescriptor == null) {
                dataObjectType = dataObject.GetType();
                typeDescriptionProvider = TypeDescriptor.GetProvider(DataObject);
                CustomTypeDescriptor = typeDescriptionProvider.GetTypeDescriptor(DataObject);
            }
            else {
                dataObjectType = GetEntityType();
                typeDescriptionProvider = new TrivialTypeDescriptionProvider(CustomTypeDescriptor);
            }

            // Set the context type and entity set name on ourselves. Note that in this scenario those
            // concepts are somewhat artificial, since we don't have a real context. 

            // Set the ContextType to the dataObjectType, which is a bit strange but harmless
            Type contextType = dataObjectType;

            ((IDynamicDataSource)this).ContextType = contextType;

            // We can set the entity set name to anything, but using the
            // DataObjectType makes some Dynamic Data error messages clearer.
            ((IDynamicDataSource)this).EntitySetName = dataObjectType.Name;

            MetaModel model = null;
            try {
                model = MetaModel.GetModel(contextType);
            }
            catch {
                model = new MetaModel();
                model.RegisterContext(
                    new SimpleModelProvider(contextType, dataObjectType, dataObject),
                    new ContextConfiguration() {
                        MetadataProviderFactory = (type => typeDescriptionProvider)
                    });
            }

            MetaTable table = model.GetTable(dataObjectType);

            if (detailsView != null) {
                detailsView.RowsGenerator = new AdvancedFieldGenerator(table, false);
            }
        }
Exemple #9
0
        private Type GetMemberType(MemberExpression m)
        {
            AttributeMappingSource mappingSource = new AttributeMappingSource();
            MetaModel mapping = mappingSource.GetModel(m.Member.DeclaringType);
            MetaTable table   = mapping.GetTable(m.Member.DeclaringType);

            foreach (MetaDataMember dataMember in table.RowType.PersistentDataMembers)
            {
                if (dataMember.Name == m.Member.Name)
                {
                    return(dataMember.Type);
                }
            }
            return(null);
        }
 public override MetaTable GetTable(Type rowType)
 {
     if (mappingSource.GetMetaTableName != null)
     {
         var typeName  = "System.Data.Linq.Mapping.AttributedMetaTable";
         var bf        = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance;
         var attribute = new TableAttribute {
             Name = mappingSource.GetMetaTableName(rowType)
         };
         var args      = new object[] { source, attribute, rowType };
         var metaTable = typeof(DataContext).Assembly.CreateInstance(typeName, false, bf, null, args, CultureInfo.CurrentCulture, null) as MetaTable;
         return(metaTable);
     }
     return(source.GetTable(rowType));
 }
Exemple #11
0
        public static List <MetaDataMember> ReadProperties(this DataContext dc, Type type, ref WSStatus statusLines)
        {
            lock (ReadPropertiesLock)
            {
                try
                {
                    AttributeMappingSource ams = new System.Data.Linq.Mapping.AttributeMappingSource();
                    MetaModel model            = ams.GetModel(dc.GetType());
                    var       meta             = model.GetTable(type);

                    return(meta.RowType.DataMembers.Where(x => x.IsPersistent).ToList());//TODO@ANDVO : avoid using '.ToList()' to speed up execution
                }
                catch (Exception) { }
                return(null);
            }
        }
        private void RegisterNotification(object entity, object entityOriginalState, MetaModel metaModel)
        {
            if (modifiedProperties.ContainsKey(entity))
            {
                return;
            }
            modifiedProperties[entity] = null;

            var entityChanged = entity as INotifyPropertyChanged;

            if (entityChanged != null)
            {
                entityChanged.PropertyChanged += OnPropertyChangedEvent;
            }

            var entityChanging = entity as INotifyPropertyChanging;

            if (entityChanging != null)
            {
                entityChanging.PropertyChanging += OnPropertyChangingEvent;
            }

            // then check all properties, and note them as changed if they already did
            if (!ReferenceEquals(entity, entityOriginalState)) // only if we specified another original entity
            {
                foreach (var dataMember in metaModel.GetTable(entity.GetType()).RowType.PersistentDataMembers)
                {
                    var memberInfo = dataMember.Member;
                    if (entityOriginalState == null ||
                        IsPropertyModified(memberInfo.GetMemberValue(entity),
                                           memberInfo.GetMemberValue(entityOriginalState)))
                    {
                        SetPropertyChanged(entity, memberInfo.Name);
                    }
                }
            }
        }
Exemple #13
0
    public void BuildColumns(DataGrid targetGrid)
    {
        MetaTable metaTable = model.GetTable(typeof(TEntity));

        // Decision whether to auto-generated columns still rests with the caller.
        targetGrid.Columns.Clear();
        foreach (var metaColumn in metaTable.Columns.Where(x => x.GetType().Name == "MetaColumn" && x.Scaffold))
        {
            switch (metaColumn.ColumnType.Name)
            {
            case "Boolean":
                targetGrid.Columns.Add(new DataGridCheckBoxColumn {
                    Binding = new Binding(metaColumn.Name), Header = metaColumn.DisplayName
                });
                break;

            default:
                targetGrid.Columns.Add(new DynamicDataGridTextColumn {
                    MetaColumn = metaColumn, Binding = new Binding(metaColumn.Name), Header = metaColumn.DisplayName
                });
                break;
            }
        }
    }
Exemple #14
0
 public static MetaTable GetMetaTable(string tablename)
 {
     return(_DefualtModel.GetTable(tablename));
 }
Exemple #15
0
    // Use GetTable methods.
    public string GetAdresses(int index)
    {
        // Get the default data model.
        MetaModel model = MetaModel.Default;

        MetaTable mTable;

        switch (index)
        {
        case 0:
            // <Snippet41>
            // Get the metatable for the table with the
            // specified entity type.
            mTable = model.GetTable(typeof(CustomerAddress));
            // </Snippet41>
            break;

        case 1:
            // <Snippet42>
            // Get the metatable for the table with the
            // specified table name.
            mTable = model.GetTable("CustomerAddresses");
            // </Snippet42>
            break;

        case 2:
            // <Snippet43>
            // Get the metatable for the table with the
            // specified table name and the specified data
            // context.
            mTable = model.GetTable("CustomerAddresses", typeof(AdventureWorksLTDataContext));
            // </Snippet43>
            break;

        default:
            mTable = model.GetTable(typeof(CustomerAddress));
            break;
        }

        // The following code dislays the actual value
        // (adress) associated with a customer and link
        // to the related Addresses table.
        MetaForeignKeyColumn fkColumn =
            (MetaForeignKeyColumn)mTable.GetColumn("Address");

        Customer row = (Customer)GetDataItem();


        StringBuilder addressList = new StringBuilder();

        foreach (CustomerAddress childRow in row.CustomerAddresses)
        {
            addressList.Append(childRow.AddressType);
            addressList.Append(":<br/>");
            addressList.Append("<a href='");
            addressList.Append(fkColumn.GetForeignKeyDetailsPath(childRow.Address));
            addressList.Append("'>");
            addressList.Append(childRow.Address.AddressLine1);
            addressList.Append("</a><br/><br/>");
        }

        return(addressList.ToString());
    }
Exemple #16
0
 public override MetaTable GetTable(Type rowType)
 {
     return(new CustomMetaTable(model.GetTable(rowType), model));
 }