Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectFactory"/> class.
        /// </summary>
        /// <param name="metaPopulation">
        /// The meta population.
        /// </param>
        /// <param name="assembly">
        /// The assembly.
        /// </param>
        /// <param name="namespace">
        /// The namespace
        /// </param>
        public ObjectFactory(IMetaPopulation metaPopulation, Type instance)
        {
            var assembly = instance.GetTypeInfo().Assembly;

            var types = assembly.GetTypes()
                        .Where(type => type.Namespace != null &&
                               type.Namespace.Equals(instance.Namespace) &&
                               type.GetTypeInfo().ImplementedInterfaces.Contains(typeof(ISessionObject)))
                        .ToArray();

            var extensionMethods = (from type in assembly.ExportedTypes
                                    where type.GetTypeInfo().IsSealed&& !type.GetTypeInfo().IsGenericType&& !type.IsNested
                                    from method in type.GetTypeInfo().DeclaredMethods
                                    where method.IsStatic && method.IsDefined(typeof(ExtensionAttribute), false)
                                    select method).ToArray();


            this.MetaPopulation = metaPopulation;
            this.Namespace      = instance.Namespace;

            var validationLog = metaPopulation.Validate();

            if (validationLog.ContainsErrors)
            {
                throw new Exception(validationLog.ToString());
            }

            metaPopulation.Bind(types, extensionMethods);

            this.typeByObjectType           = new Dictionary <IObjectType, Type>();
            this.objectTypeByType           = new Dictionary <Type, IObjectType>();
            this.objectTypeByName           = new Dictionary <string, IObjectType>();
            this.objectTypeByObjectTypeId   = new Dictionary <Guid, IObjectType>();
            this.contructorInfoByObjectType = new Dictionary <IObjectType, ConstructorInfo>();

            var typeByName = types.ToDictionary(type => type.Name, type => type);

            foreach (var objectType in metaPopulation.Composites)
            {
                var type = typeByName[objectType.Name];

                this.typeByObjectType[objectType]            = type;
                this.objectTypeByType[type]                  = objectType;
                this.objectTypeByName[type.Name]             = objectType;
                this.objectTypeByObjectTypeId[objectType.Id] = objectType;

                if (objectType is IClass)
                {
                    var parameterTypes = new[] { typeof(Session) };
                    var constructor    = type.GetTypeInfo().GetConstructor(parameterTypes);
                    if (constructor == null)
                    {
                        throw new ArgumentException(objectType.Name + " has no Allors constructor.");
                    }

                    this.contructorInfoByObjectType[objectType] = constructor;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectFactory"/> class.
        /// </summary>
        /// <param name="metaPopulation">
        /// The meta population.
        /// </param>
        /// <param name="assembly">
        /// The assembly.
        /// </param>
        /// <param name="namespace">
        /// The namespace
        /// </param>
        public ObjectFactory(IMetaPopulation metaPopulation, Assembly assembly, string @namespace)
        {
            this.MetaPopulation = metaPopulation;
            this.Assembly       = assembly;
            this.Namespace      = @namespace;

            var validationLog = metaPopulation.Validate();

            if (validationLog.ContainsErrors)
            {
                throw new Exception(validationLog.ToString());
            }

            metaPopulation.Bind(assembly);

            this.typeByObjectType           = new Dictionary <IObjectType, Type>();
            this.objectTypeByType           = new Dictionary <Type, IObjectType>();
            this.objectTypeByName           = new Dictionary <string, IObjectType>();
            this.objectTypeByObjectTypeId   = new Dictionary <Guid, IObjectType>();
            this.contructorInfoByObjectType = new Dictionary <IObjectType, ConstructorInfo>();

            var types = assembly.GetTypes().Where(type =>
                                                  type.Namespace != null &&
                                                  type.Namespace.Equals(@namespace) &&
                                                  type.GetInterfaces().Contains(typeof(ISessionObject)));

            var typeByName = types.ToDictionary(type => type.Name, type => type);

            foreach (var objectType in metaPopulation.Composites)
            {
                var type = typeByName[objectType.Name];

                this.typeByObjectType[objectType]            = type;
                this.objectTypeByType[type]                  = objectType;
                this.objectTypeByName[type.Name]             = objectType;
                this.objectTypeByObjectTypeId[objectType.Id] = objectType;

                if (objectType is IClass)
                {
                    var parameterTypes = new[] { typeof(Session) };
                    var constructor    = type.GetConstructor(parameterTypes);
                    if (constructor == null)
                    {
                        throw new ArgumentException(objectType.Name + " has no Allors constructor.");
                    }

                    this.contructorInfoByObjectType[objectType] = constructor;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectFactory"/> class.
        /// </summary>
        /// <param name="environment">
        /// The domain.
        /// </param>
        /// <param name="assembly">
        /// The assembly.
        /// </param>
        /// <param name="namespace">
        /// The namespace
        /// </param>
        public ObjectFactory(IMetaPopulation metaPopulation, Assembly assembly, string @namespace)
        {
            this.metaPopulation = metaPopulation;
            this.assembly = assembly;
            this.ns = @namespace;

            var validationLog = metaPopulation.Validate();
            if (validationLog.ContainsErrors)
            {
                throw new Exception(validationLog.ToString());
            }

            metaPopulation.Bind(assembly);

            this.typeByObjectType = new Dictionary<IObjectType, Type>();
            this.objectTypeByType = new Dictionary<Type, IObjectType>();
            this.objectTypeByObjectTypeId = new Dictionary<Guid, IObjectType>();
            this.contructorInfoByObjectType = new Dictionary<IObjectType, ConstructorInfo>();

            var types = assembly.GetTypes().Where(type =>
                type.Namespace != null &&
                type.Namespace.Equals(@namespace) &&
                type.GetInterfaces().Contains(typeof(IObject)));

            var typeByName = types.ToDictionary(type => type.Name, type => type);

            foreach (var objectType in metaPopulation.Composites)
            {
                var type = typeByName[objectType.Name];

                this.typeByObjectType[objectType] = type;
                this.objectTypeByType[type] = objectType;
                this.objectTypeByObjectTypeId[objectType.Id] = objectType;

                if (objectType is IClass)
                {
                    var parameterTypes = new[] { typeof(IStrategy) };
                    var constructor = type.GetConstructor(parameterTypes);
                    if (constructor == null)
                    {
                        throw new ArgumentException(objectType.Name + " has no Allors constructor.");
                    }

                    this.contructorInfoByObjectType[objectType] = constructor;
                }
            }
        }