Example #1
0
        public async Task <IShape> CreateAsync(string shapeType, Func <Task <IShape> > shapeFactory, Action <ShapeCreatingContext> creating, Action <ShapeCreatedContext> created)
        {
            ShapeDescriptor shapeDescriptor;

            (await GetShapeTableAsync()).Descriptors.TryGetValue(shapeType, out shapeDescriptor);

            var creatingContext = new ShapeCreatingContext
            {
                New          = this,
                ShapeFactory = this,
                ShapeType    = shapeType,
                OnCreated    = new List <Func <ShapeCreatedContext, Task> >(),
                CreateAsync  = shapeFactory
            };

            creating?.Invoke(creatingContext);

            // "creating" events may add behaviors and alter base type
            foreach (var ev in _events)
            {
                ev.Creating(creatingContext);
            }

            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.CreatingAsync)
                {
                    await ev(creatingContext);
                }
            }

            // Create the new instance
            var createdContext = new ShapeCreatedContext
            {
                New          = creatingContext.New,
                ShapeFactory = creatingContext.ShapeFactory,
                ShapeType    = creatingContext.ShapeType,
                Shape        = await creatingContext.CreateAsync()
            };

            var shape = createdContext.Shape as IShape;

            if (shape == null)
            {
                throw new InvalidOperationException("Invalid base type for shape: " + createdContext.Shape.GetType().ToString());
            }

            if (shape.Metadata == null)
            {
                shape.Metadata = new ShapeMetadata();
            }

            ShapeMetadata shapeMetadata = shape.Metadata;

            shape.Metadata.Type = shapeType;

            // Merge wrappers if there are any
            if (shapeDescriptor != null && shapeMetadata.Wrappers.Count + shapeDescriptor.Wrappers.Count > 0)
            {
                shapeMetadata.Wrappers.AddRange(shapeDescriptor.Wrappers);
            }

            // "created" events provides default values and new object initialization
            foreach (var ev in _events)
            {
                ev.Created(createdContext);
            }

            if (shapeDescriptor != null)
            {
                foreach (var ev in shapeDescriptor.CreatedAsync)
                {
                    await ev(createdContext);
                }
            }

            if (creatingContext != null)
            {
                foreach (var ev in creatingContext.OnCreated)
                {
                    await ev(createdContext);
                }
            }

            created?.Invoke(createdContext);

            return(createdContext.Shape);
        }
Example #2
0
 public virtual void Creating(ShapeCreatingContext context)
 {
 }