Beispiel #1
0
        private IDictionary <string, EntitySetInfo> LoadEntitySets(Func <PropertyInfo, EntityInfo, string> keySelector)
        {
            var dataContextPublicProperties = ContextType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

            var entitySetTypes =
                from property in dataContextPublicProperties
                let propertyType = property.PropertyType
                                   where propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(IQueryable <>)
                                   let entityType = propertyType.GetGenericArguments().First()
                                                    select new EntitySetInfo(property, new EntityInfo(entityType));

            var entitySets = new Dictionary <string, EntitySetInfo>();

            foreach (var type in entitySetTypes)
            {
                var key = keySelector(type.Property, type.Entity);

                if (!string.IsNullOrEmpty(key))
                {
                    entitySets.Add(key, type);
                }
            }

            return(entitySets);
        }
Beispiel #2
0
 private void BuildFieldsFromBase(Type contextType)
 {
     foreach (var f in ContextType.GetProperties())
     {
         if (!_fieldsByKey.ContainsKey(f.Name))
         {
             var parameter = Expression.Parameter(ContextType);
             this.AddField(new Field(f.Name, Expression.Lambda(Expression.Property(parameter, f.Name), parameter), string.Empty, string.Empty));
         }
     }
     foreach (var f in ContextType.GetFields())
     {
         if (!_fieldsByKey.ContainsKey(f.Name))
         {
             var parameter = Expression.Parameter(ContextType);
             this.AddField(new Field(f.Name, Expression.Lambda(Expression.Field(parameter, f.Name), parameter), string.Empty, string.Empty));
         }
     }
 }
Beispiel #3
0
        private void BuildFieldsFromBase()
        {
            if (IsEnum)
            {
                foreach (var item in Enum.GetNames(this.ContextType))
                {
                    this.AddField(new Field(item, null, "", Name, ContextType));
                }
                return;
            }
            foreach (var f in ContextType.GetProperties())
            {
                if (!_fieldsByName.ContainsKey(f.Name))
                {
                    //Get Description from ComponentModel.DescriptionAttribute
                    string description = string.Empty;
                    var    d           = (System.ComponentModel.DescriptionAttribute)f.GetCustomAttribute(typeof(System.ComponentModel.DescriptionAttribute), false);
                    if (d != null)
                    {
                        description = d.Description;
                    }

                    var parameter = Expression.Parameter(ContextType);
                    this.AddField(new Field(SchemaGenerator.ToCamelCaseStartsLower(f.Name), Expression.Lambda(Expression.Property(parameter, f.Name), parameter), description, null));
                }
            }
            foreach (var f in ContextType.GetFields())
            {
                if (!_fieldsByName.ContainsKey(f.Name))
                {
                    //Get Description from ComponentModel.DescriptionAttribute
                    string description = string.Empty;
                    var    d           = (System.ComponentModel.DescriptionAttribute)f.GetCustomAttribute(typeof(System.ComponentModel.DescriptionAttribute), false);
                    if (d != null)
                    {
                        description = d.Description;
                    }

                    var parameter = Expression.Parameter(ContextType);
                    this.AddField(new Field(SchemaGenerator.ToCamelCaseStartsLower(f.Name), Expression.Lambda(Expression.Field(parameter, f.Name), parameter), description, null));
                }
            }
        }
Beispiel #4
0
        public DLinqDataModelProvider(object contextInstance, Func <object> contextFactory)
        {
            ContextFactory = contextFactory;

            DataContext context = (DataContext)contextInstance ?? (DataContext)CreateContext();

            ContextType = context.GetType();

            DLinqTables = new List <TableProvider>();
            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 <TableProvider>(DLinqTables);
        }
Beispiel #5
0
        public IContext LoadContext()
        {
            var propertyDictionary = PropertyLoader.Load(ContextType);
            // inject property into context
            var contextProperties = from PropertyInfo propertyInfo
                                    in ContextType.GetProperties()
                                    where propertyInfo.PropertyType == typeof(string) &&
                                    AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                    select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

            foreach (var property in contextProperties)
            {
                property.Item1.SetValue(Context, propertyDictionary[property.Item2.Name]);
            }

            var contextBuilder = new InjectorContext.InjectorContextBuilder();
            // initialize models
            var priorityQueue    = new SimplePriorityQueue <int, InstantiateModel>();
            var methodsInContext = from MethodInfo methodInfo
                                   in ContextType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                   where AttributeHelper.IsMethodMarked <Instantiate>(methodInfo)
                                   select methodInfo;

            foreach (var methodInfo in methodsInContext)
            {
                var attr  = methodInfo.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(methodInfo) : AddModel(methodInfo, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            var namespaceToScan = AttributeHelper.GetNamespaceToScan(ContextType);
            var assembly        = Assembly.GetCallingAssembly();

            var scanTypes = from Type type
                            in assembly.GetTypes()
                            where AttributeHelper.IsTypeMarked <Instantiate>(type) && namespaceToScan.Contains(type.Namespace)
                            select type;

            // create models to instantiate
            foreach (var scanType in scanTypes)
            {
                var attr  = scanType.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(scanType) : AddModel(scanType, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            // Create Objects
            var previousCount = priorityQueue.Count;

            while (!priorityQueue.IsEmpty)
            {
                var model = priorityQueue.Dequeue();
                if (CanInstantiate(model))
                {
                    var instance = Instantiate(model);
                    // property injection
                    if (model.IsConstructorType)
                    {
                        var properties = from PropertyInfo propertyInfo in model.Type.GetProperties()
                                         where AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                         select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

                        foreach (var property in properties)
                        {
                            var name = property.Item2.Name;
                            property.Item1.SetValue(instance, propertyDictionary[name]);
                        }
                    }
                    ObjectStorage[model.Name] = instance;
                }
                else
                {
                    UpdateWeight(model);
                    priorityQueue.Enqueue(model.Weight, model);
                }

                if (previousCount <= priorityQueue.Count)
                {
                    throw new DependencyNotFoundException(model);
                }

                previousCount = priorityQueue.Count;
            }

            // Property Injection
            foreach (var instance in ObjectStorage.Values)
            {
                var instanceType = instance.GetType();
                var properties   = from PropertyInfo propertyInfo
                                   in instanceType.GetProperties()
                                   where AttributeHelper.IsPropertyMarked <Autowired>(propertyInfo)
                                   select propertyInfo;
                foreach (var property in properties)
                {
                    var attr = property.GetCustomAttribute <Autowired>();
                    property.SetValue(instance,
                                      !string.IsNullOrEmpty(attr?.Name)
                            ? ObjectStorage[attr.Name]
                            : ObjectStorage[NamingHelper.ConvertToCamelCase(property.Name)]);
                }
            }

            // invoke post constructor
            foreach (var name in ObjectStorage.Keys)
            {
                var model = ModelStorage[name];
                if (!model.IsConstructorType)
                {
                    continue;
                }
                var type             = model.Type;
                var instance         = ObjectStorage[name];
                var postConstructors =
                    from MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                    where AttributeHelper.IsMethodMarked <PostConsturctor>(method)
                    select method;
                foreach (var postConstructor in postConstructors)
                {
                    postConstructor.Invoke(instance, null);
                }
            }
            // Build
            foreach (var pair in ObjectStorage)
            {
                contextBuilder.Add(pair.Key, pair.Value);
            }

            return(contextBuilder.Build());
        }