Esempio n. 1
0
        public virtual void AddModelPart(object part, MessageContext messageContext, string name = null)
        {
            Guard.NotNull(part, nameof(part));
            Guard.NotNull(messageContext, nameof(messageContext));

            var model = messageContext.Model;

            name = name.NullEmpty() ?? ResolveModelName(part);

            object modelPart = null;

            switch (part)
            {
            case INamedModelPart x:
                modelPart = x;
                break;

            case IModelPart x:
                MergeModelBag(x, model, messageContext);
                break;

            case Order x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Product x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Customer x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Address x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Shipment x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case OrderNote x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case RecurringPayment x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ReturnRequest x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case GiftCard x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case NewsLetterSubscription x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Campaign x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ProductReview x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case BlogComment x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case NewsComment x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ForumTopic x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ForumPost x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case Forum x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case PrivateMessage x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case IEnumerable <GenericAttribute> x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case PollVotingRecord x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ProductReviewHelpfulness x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case ForumSubscription x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            case BackInStockSubscription x:
                modelPart = CreateModelPart(x, messageContext);
                break;

            default:
                var partType = part.GetType();
                modelPart = part;

                if (partType.IsPlainObjectType() && !partType.IsAnonymous())
                {
                    var evt = new MessageModelPartMappingEvent(part);
                    _services.EventPublisher.Publish(evt);

                    if (evt.Result != null && !object.ReferenceEquals(evt.Result, part))
                    {
                        modelPart = evt.Result;
                        name      = evt.ModelPartName.NullEmpty() ?? ResolveModelName(evt.Result) ?? name;
                    }
                    else
                    {
                        modelPart = part;
                    }

                    modelPart = evt.Result ?? part;
                    name      = evt.ModelPartName.NullEmpty() ?? name;
                }

                break;
            }

            if (modelPart != null)
            {
                if (name.IsEmpty())
                {
                    throw new SmartException($"Could not resolve a model key for part '{modelPart.GetType().Name}'. Use an instance of 'NamedModelPart' class to pass model with name.");
                }

                if (model.TryGetValue(name, out var existing))
                {
                    // A model part with the same name exists in model already...
                    if (existing is IDictionary <string, object> x)
                    {
                        // but it's a dictionary which we can easily merge with
                        x.Merge(FastProperty.ObjectToDictionary(modelPart), true);
                    }
                    else
                    {
                        // Wrap in HybridExpando and merge
                        var he = new HybridExpando(existing, true);
                        he.Merge(FastProperty.ObjectToDictionary(modelPart), true);
                        model[name] = he;
                    }
                }
                else
                {
                    // Put part to model as new property
                    model[name] = modelPart;
                }
            }
        }
Esempio n. 2
0
        protected virtual void MergeModelBag(IModelPart part, IDictionary <string, object> model, MessageContext messageContext)
        {
            if (!(model.Get("Bag") is IDictionary <string, object> bag))
            {
                model["Bag"] = bag = new Dictionary <string, object>();
            }

            var source = part as IDictionary <string, object>;

            bag.Merge(source);
        }
Esempio n. 3
0
        protected virtual object CreateModelPart(IEnumerable <GenericAttribute> part, MessageContext messageContext)
        {
            Guard.NotNull(messageContext, nameof(messageContext));
            Guard.NotNull(part, nameof(part));

            var m = new Dictionary <string, object>();

            foreach (var attr in part)
            {
                m[attr.Key] = attr.Value;
            }

            PublishModelPartCreatedEvent <IEnumerable <GenericAttribute> >(part, m);

            return(m);
        }