Пример #1
0
        private void CreateHandlers()
        {
            var handlers = context.Domain.Handlers;
            var services = context.BuilderConfiguration.Services;

            using (BuildLog.InfoRegion(Strings.LogCreatingX, typeof(DomainHandler).GetShortName())) {
                // HandlerFactory
                handlers.Factory = services.HandlerFactory;

                // NameBuilder
                handlers.NameBuilder = services.NameBuilder;

                // StorageDriver
                handlers.StorageDriver = services.StorageDriver.CreateNew(context.Domain);

                // SequenceQueryBuilder
                handlers.SequenceQueryBuilder = new SequenceQueryBuilder(handlers.StorageDriver);

                // StorageNodeRegistry
                handlers.StorageNodeRegistry = new StorageNodeRegistry();

                // DomainHandler
                handlers.DomainHandler = handlers.Create <DomainHandler>();
                handlers.DomainHandler.BuildHandlers();
            }
        }
Пример #2
0
 private void BuildModelDefinition()
 {
     using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.ModelDefinition)) {
         context.ModelDef = new DomainModelDef(modelDefBuilder, context.Validator);
         using (BuildLog.InfoRegion(Strings.LogDefiningX, Strings.Types))
             modelDefBuilder.ProcessTypes();
     }
 }
Пример #3
0
 public static void Run(BuildingContext context)
 {
     using (BuildLog.InfoRegion(Strings.LogInspectingModelDefinition)) {
         InspectHierarchies(context);
         InspectTypes(context);
         InspectInterfaces(context);
         InspectAbstractTypes(context);
     }
 }
Пример #4
0
 private void BuildModel()
 {
     using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.Model)) {
         ModelBuilder.Run(context);
         var model = context.Model;
         model.Lock(true);
         context.Domain.Model = model;
     }
 }
Пример #5
0
 private void ApplyCustomDefinitions()
 {
     using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.CustomDefinitions))
         using (new BuildingScope(context)) { // Activate context for compatibility with previous versions
             foreach (var module in context.Modules)
             {
                 module.OnDefinitionsBuilt(context, context.ModelDef);
             }
         }
 }
 private void ProcessAll()
 {
     using (BuildLog.InfoRegion(Strings.LogProcessingFixupActions))
         while (context.ModelInspectionResult.Actions.Count > 0)
         {
             var action = context.ModelInspectionResult.Actions.Dequeue();
             BuildLog.Info(string.Format(Strings.LogExecutingActionX, action));
             action.Run(this);
         }
 }
Пример #7
0
        private void InitializeServices()
        {
            var domain = context.Domain;

            using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.KeyGenerators)) {
                var generators    = domain.KeyGenerators;
                var keysToProcess = domain.Model.Hierarchies
                                    .Select(h => h.Key)
                                    .Where(k => k.GeneratorKind != KeyGeneratorKind.None);
                foreach (var keyInfo in keysToProcess)
                {
                    var generator = domain.Services.Demand <KeyGenerator>(keyInfo.GeneratorName);
                    generators.Register(keyInfo, generator);
                    if (keyInfo.IsFirstAmongSimilarKeys)
                    {
                        generator.Initialize(context.Domain, keyInfo.TupleDescriptor);
                    }

                    var temporaryGenerator = domain.Services.Get <TemporaryKeyGenerator>(keyInfo.GeneratorName);
                    if (temporaryGenerator == null)
                    {
                        continue; // Temporary key generators are optional
                    }

                    generators.RegisterTemporary(keyInfo, temporaryGenerator);
                    if (keyInfo.IsFirstAmongSimilarKeys)
                    {
                        temporaryGenerator.Initialize(context.Domain, keyInfo.TupleDescriptor);
                    }
                }

                generators.Lock();
            }

            using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.Validators)) {
                foreach (var type in domain.Model.Types)
                {
                    foreach (var validator in type.Validators)
                    {
                        validator.Configure(domain, type);
                    }

                    foreach (var field in type.Fields)
                    {
                        foreach (var validator in field.Validators)
                        {
                            validator.Configure(domain, type, field);
                        }
                    }
                }
            }

            // Initialize DomainHandler services as well
            domain.Handler.InitializeServices();
        }
        public TypeDef ProcessType(Type type)
        {
            var modelDef = context.ModelDef;

            var typeDef = modelDef.Types.TryGetValue(type);

            if (typeDef != null)
            {
                return(typeDef);
            }

            using (BuildLog.InfoRegion(Strings.LogDefiningX, type.GetFullName())) {
                typeDef = DefineType(type);
                if (modelDef.Types.Contains(typeDef.Name))
                {
                    throw new DomainBuilderException(string.Format(Strings.ExTypeWithNameXIsAlreadyDefined, typeDef.Name));
                }

                HierarchyDef hierarchyDef = null;
                if (typeDef.IsEntity)
                {
                    // HierarchyRootAttribute is required for hierarchy root
                    var hra = type.GetAttribute <HierarchyRootAttribute>(AttributeSearchOptions.Default);
                    if (hra != null)
                    {
                        hierarchyDef = DefineHierarchy(typeDef, hra);
                    }
                }

                ProcessProperties(typeDef, hierarchyDef);

                if (typeDef.IsEntity || typeDef.IsInterface)
                {
                    ProcessIndexes(typeDef);
                }

                if (hierarchyDef != null)
                {
                    BuildLog.Info(Strings.LogHierarchyX, typeDef.Name);
                    modelDef.Hierarchies.Add(hierarchyDef);
                }

                modelDef.Types.Add(typeDef);

                ProcessFullTextIndexes(typeDef);

                var validators = type.GetCustomAttributes(false).OfType <IObjectValidator>();
                foreach (var validator in validators)
                {
                    typeDef.Validators.Add(validator);
                }

                return(typeDef);
            }
        }
Пример #9
0
        /// <summary>
        /// Builds the domain.
        /// </summary>
        /// <param name="builderConfiguration">The builder configuration.</param>
        /// <returns>Built domain.</returns>
        public static Domain Run(DomainBuilderConfiguration builderConfiguration)
        {
            ArgumentValidator.EnsureArgumentNotNull(builderConfiguration, "builderConfiguration");

            var context = new BuildingContext(builderConfiguration);

            using (BuildLog.InfoRegion(Strings.LogBuildingX, typeof(Domain).GetShortName()))
                new DomainBuilder(context).Run();

            return(context.Domain);
        }
Пример #10
0
 private void CreateServices()
 {
     using (BuildLog.InfoRegion(Strings.LogCreatingX, typeof(IServiceContainer).GetShortName())) {
         var domain            = context.Domain;
         var configuration     = domain.Configuration;
         var userContainerType = configuration.ServiceContainerType ?? typeof(ServiceContainer);
         var registrations     = CreateServiceRegistrations(configuration).Concat(KeyGeneratorFactory.GetRegistrations(context));
         var systemContainer   = domain.CreateSystemServices();
         var userContainer     = ServiceContainer.Create(userContainerType, systemContainer);
         domain.Services = new ServiceContainer(registrations, userContainer);
     }
 }
Пример #11
0
 private void BuildModel()
 {
     using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.ActualModel)) {
         context.Model = new DomainModel();
         BuildTypes(GetTypeBuildSequence());
         BuildAssociations();
         FindAndMarkInboundAndOutboundTypes(context);
         IndexBuilder.BuildIndexes(context);
         context.Model.UpdateState();
         ValidateMappingConfiguration();
         BuildDatabaseDependencies();
         BuildPrefetchActions();
     }
 }
Пример #12
0
        private void CreateDomain()
        {
            using (BuildLog.InfoRegion(Strings.LogCreatingX, typeof(Domain).GetShortName())) {
                var services            = context.BuilderConfiguration.Services;
                var useSingleConnection =
                    services.ProviderInfo.Supports(ProviderFeatures.SingleConnection) &&
                    context.BuilderConfiguration.Stage == UpgradeStage.Final;

                context.Domain = new Domain(
                    context.Configuration,
                    context.BuilderConfiguration.UpgradeContextCookie,
                    useSingleConnection ? services.Connection : null);
            }
        }
Пример #13
0
 private void BuildTypes(IEnumerable <TypeDef> typeDefs)
 {
     using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.Types)) {
         // Building types, system fields and hierarchies
         foreach (var typeDef in typeDefs)
         {
             typeBuilder.BuildType(typeDef);
         }
     }
     using (BuildLog.InfoRegion(Strings.LogBuildingX, "Fields"))
         foreach (var typeDef in typeDefs)
         {
             var typeInfo = context.Model.Types[typeDef.UnderlyingType];
             typeBuilder.BuildFields(typeDef, typeInfo);
             typeBuilder.BuildTypeDiscriminatorMap(typeDef, typeInfo);
         }
 }
Пример #14
0
        private void BuildAssociations()
        {
            using (BuildLog.InfoRegion(Strings.LogBuildingX, Strings.Associations)) {
                PreprocessAssociations();
                foreach (var pair in context.PairedAssociations)
                {
                    if (context.DiscardedAssociations.Contains(pair.First))
                    {
                        continue;
                    }
                    if (!context.Model.Associations.Contains(pair.First))
                    {
                        continue;
                    }
                    AssociationBuilder.BuildPairedAssociation(pair.First, pair.Second);
                }

                foreach (var ai in context.DiscardedAssociations)
                {
                    context.Model.Associations.Remove(ai);
                }
                context.DiscardedAssociations.Clear();

                foreach (var association in context.Model.Associations)
                {
                    TryAddForeignKeyIndex(association);
                    if (association.IsPaired)
                    {
                        continue;
                    }
                    if (!association.OnOwnerRemove.HasValue)
                    {
                        association.OnOwnerRemove = association.OwnerField.IsEntitySet ? OnRemoveAction.Clear : OnRemoveAction.None;
                    }
                    if (!association.OnTargetRemove.HasValue)
                    {
                        association.OnTargetRemove = OnRemoveAction.Deny;
                    }
                }

                BuildAuxiliaryTypes(context.Model.Associations);
            }
        }
 public static void Run(BuildingContext context)
 {
     using (BuildLog.InfoRegion(Strings.LogProcessingMappingRules)) {
         new StorageMappingBuilder(context).ProcessAll();
     }
 }
Пример #16
0
        /// <summary>
        /// Builds the <see cref="TypeInfo"/> instance, its key fields and <see cref="HierarchyInfo"/> for hierarchy root.
        /// </summary>
        /// <param name="typeDef"><see cref="TypeDef"/> instance.</param>
        public TypeInfo BuildType(TypeDef typeDef)
        {
            using (BuildLog.InfoRegion(Strings.LogBuildingX, typeDef.UnderlyingType.GetShortName())) {
                var typeInfo = new TypeInfo(context.Model, typeDef.Attributes)
                {
                    UnderlyingType  = typeDef.UnderlyingType,
                    Name            = typeDef.Name,
                    MappingName     = typeDef.MappingName,
                    MappingDatabase = typeDef.MappingDatabase,
                    MappingSchema   = typeDef.MappingSchema,
                    HasVersionRoots = typeDef.UnderlyingType.GetInterfaces().Any(type => type == typeof(IHasVersionRoots)),
                    Validators      = typeDef.Validators,
                };

                if (typeInfo.IsEntity && DeclaresOnValidate(typeInfo.UnderlyingType))
                {
                    typeInfo.Validators.Add(new EntityValidator());
                }

                if (typeDef.StaticTypeId != null)
                {
                    typeInfo.TypeId = typeDef.StaticTypeId.Value;
                }

                context.Model.Types.Add(typeInfo);

                // Registering connections between type & its ancestors
                var node = context.DependencyGraph.TryGetNode(typeDef);
                if (node != null)
                {
                    foreach (var edge in node.OutgoingEdges.Where(e =>
                                                                  e.Kind == EdgeKind.Implementation || e.Kind == EdgeKind.Inheritance))
                    {
                        var baseType = context.Model.Types[edge.Head.Value.UnderlyingType];
                        switch (edge.Kind)
                        {
                        case EdgeKind.Inheritance:
                            context.Model.Types.RegisterInheritance(baseType, typeInfo);
                            break;

                        case EdgeKind.Implementation:
                            context.Model.Types.RegisterImplementation(baseType, typeInfo);
                            break;
                        }
                    }
                }

                if (typeDef.IsEntity)
                {
                    var hierarchyDef = context.ModelDef.FindHierarchy(typeDef);

                    // Is type a hierarchy root?
                    if (typeInfo.UnderlyingType == hierarchyDef.Root.UnderlyingType)
                    {
                        foreach (var keyField in hierarchyDef.KeyFields)
                        {
                            var fieldInfo = BuildDeclaredField(typeInfo, typeDef.Fields[keyField.Name]);
                            fieldInfo.IsPrimaryKey = true;
                        }

                        typeInfo.Hierarchy = BuildHierarchyInfo(typeInfo, hierarchyDef);
                    }
                    else
                    {
                        var root = context.Model.Types[hierarchyDef.Root.UnderlyingType];
                        typeInfo.Hierarchy = root.Hierarchy;
                        foreach (var fieldInfo in root.Fields.Where(f => f.IsPrimaryKey && f.Parent == null))
                        {
                            BuildInheritedField(typeInfo, fieldInfo);
                        }
                    }
                }
                else if (typeDef.IsInterface)
                {
                    var hierarchyDef = context.ModelDef.FindHierarchy(typeDef.Implementors[0]);
                    foreach (var keyField in hierarchyDef.KeyFields)
                    {
                        var fieldInfo = BuildDeclaredField(typeInfo, typeDef.Fields[keyField.Name]);
                        fieldInfo.IsPrimaryKey = true;
                    }
                }

                return(typeInfo);
            }
        }
 public static void Run(BuildingContext context)
 {
     using (BuildLog.InfoRegion(Strings.LogCalculatingDatabaseDependencies)) {
         new DatabaseDependencyBuilder(context).Run();
     }
 }
Пример #18
0
 public static void Run(BuildingContext context)
 {
     using (BuildLog.InfoRegion(Strings.LogValidatingMappingConfiguration)) {
         new StorageMappingValidator(context).ValidateAll();
     }
 }