DLinqDataModelProvider.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / DynamicData / DynamicData / ModelProviders / DLinqDataModelProvider.cs / 1305376 / DLinqDataModelProvider.cs

                            using System.Collections.Generic; 
using System.Collections.ObjectModel;
using System.Data.Linq;
using System.Linq;
using System.Reflection; 
using LinqMetaTable = System.Data.Linq.Mapping.MetaTable;
using LinqMetaType = System.Data.Linq.Mapping.MetaType; 
 
namespace System.Web.DynamicData.ModelProviders {
    internal sealed class DLinqDataModelProvider : DataModelProvider { 
        private ReadOnlyCollection _roTables;
        private Dictionary _columnLookup = new Dictionary();

        private Func ContextFactory { get; set; } 

        public DLinqDataModelProvider(object contextInstance, Func contextFactory) { 
            ContextFactory = contextFactory; 

            DataContext context = (DataContext)contextInstance ?? (DataContext)CreateContext(); 
            ContextType = context.GetType();

            DLinqTables = new List();
            foreach (PropertyInfo prop in ContextType.GetProperties()) { 
                Type entityType = GetEntityType(prop);
 
                if (entityType != null) { 
                    LinqMetaTable table = GetLinqTable(context, entityType);
                    ProcessTable(table, table.RowType, prop.Name, prop); 
                }
            }

            DLinqTables.ForEach(t => ((DLinqTableProvider)t).Initialize()); 

            _roTables = new ReadOnlyCollection(DLinqTables); 
        } 

        private LinqMetaTable GetLinqTable(DataContext context, Type entityType) { 
            return context.Mapping.GetTables().First(t => t.RowType.Type == entityType);
        }

        private Type GetEntityType(PropertyInfo prop) { 
            //
            if (prop.PropertyType.IsGenericType && 
                prop.PropertyType.GetGenericTypeDefinition() == typeof(Table<>)) 
                return prop.PropertyType.GetGenericArguments()[0];
 
            return null;
        }

        private void ProcessTable(LinqMetaTable table, LinqMetaType rowType, string name, PropertyInfo prop) { 
            DLinqTables.Add(new DLinqTableProvider(this, rowType, name, prop));
 
            foreach (LinqMetaType derivedType in rowType.DerivedTypes) 
                ProcessTable(table, derivedType, derivedType.Name, prop);
        } 

        internal Dictionary ColumnLookup {
            get {
                return _columnLookup; 
            }
        } 
 
        internal List DLinqTables { get; private set; }
 
        public override object CreateContext() {
            return ContextFactory();
        }
 
        public override ReadOnlyCollection Tables {
            get { 
                return _roTables; 
            }
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
´╗┐using System.Collections.Generic; 
using System.Collections.ObjectModel;
using System.Data.Linq;
using System.Linq;
using System.Reflection; 
using LinqMetaTable = System.Data.Linq.Mapping.MetaTable;
using LinqMetaType = System.Data.Linq.Mapping.MetaType; 
 
namespace System.Web.DynamicData.ModelProviders {
    internal sealed class DLinqDataModelProvider : DataModelProvider { 
        private ReadOnlyCollection _roTables;
        private Dictionary _columnLookup = new Dictionary();

        private Func ContextFactory { get; set; } 

        public DLinqDataModelProvider(object contextInstance, Func contextFactory) { 
            ContextFactory = contextFactory; 

            DataContext context = (DataContext)contextInstance ?? (DataContext)CreateContext(); 
            ContextType = context.GetType();

            DLinqTables = new List();
            foreach (PropertyInfo prop in ContextType.GetProperties()) { 
                Type entityType = GetEntityType(prop);
 
                if (entityType != null) { 
                    LinqMetaTable table = GetLinqTable(context, entityType);
                    ProcessTable(table, table.RowType, prop.Name, prop); 
                }
            }

            DLinqTables.ForEach(t => ((DLinqTableProvider)t).Initialize()); 

            _roTables = new ReadOnlyCollection(DLinqTables); 
        } 

        private LinqMetaTable GetLinqTable(DataContext context, Type entityType) { 
            return context.Mapping.GetTables().First(t => t.RowType.Type == entityType);
        }

        private Type GetEntityType(PropertyInfo prop) { 
            //
            if (prop.PropertyType.IsGenericType && 
                prop.PropertyType.GetGenericTypeDefinition() == typeof(Table<>)) 
                return prop.PropertyType.GetGenericArguments()[0];
 
            return null;
        }

        private void ProcessTable(LinqMetaTable table, LinqMetaType rowType, string name, PropertyInfo prop) { 
            DLinqTables.Add(new DLinqTableProvider(this, rowType, name, prop));
 
            foreach (LinqMetaType derivedType in rowType.DerivedTypes) 
                ProcessTable(table, derivedType, derivedType.Name, prop);
        } 

        internal Dictionary ColumnLookup {
            get {
                return _columnLookup; 
            }
        } 
 
        internal List DLinqTables { get; private set; }
 
        public override object CreateContext() {
            return ContextFactory();
        }
 
        public override ReadOnlyCollection Tables {
            get { 
                return _roTables; 
            }
        } 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK