/// <summary>
        ///     Wraps the specified view-model to a specified type.
        /// </summary>
        /// <param name="viewModel">The specified view-model.</param>
        /// <param name="wrapperType">The specified type to wrap.</param>
        /// <param name="dataContext">The specified <see cref="IDataContext" />, if any.</param>
        /// <returns>
        ///     An instance of <see cref="IViewModel" />.
        /// </returns>
        public IViewModel Wrap(IViewModel viewModel, Type wrapperType, IDataContext dataContext)
        {
            Should.NotBeNull(viewModel, "viewModel");
            Should.NotBeNull(wrapperType, "wrapperType");
            List <WrapperRegistration> list;

            if (_registrations.TryGetValue(wrapperType, out list))
            {
                for (int i = 0; i < list.Count; i++)
                {
                    WrapperRegistration registration = list[i];
                    if (registration.Condition(viewModel, dataContext))
                    {
                        return(WrapInternal(viewModel, registration.Type, dataContext));
                    }
                }
            }
            var wrapper = WrapToDefaultWrapper(viewModel, wrapperType, dataContext);

            if (wrapper == null)
            {
                throw ExceptionManager.WrapperTypeNotSupported(wrapperType);
            }
            return(wrapper);
        }
Example #2
0
        public object Wrap(object item, Type wrapperType, IDataContext dataContext)
        {
            Should.NotBeNull(item, "item");
            Should.NotBeNull(wrapperType, "wrapperType");
            if (wrapperType.IsInstanceOfType(item))
            {
                return(item);
            }
            dataContext = dataContext.ToNonReadOnly();
            object wrapper = null;
            List <WrapperRegistration> list;

            if (_registrations.TryGetValue(wrapperType, out list))
            {
                dataContext.AddOrUpdate(ItemToWrapConstant, item);
                var type = item.GetType();
                for (int i = 0; i < list.Count; i++)
                {
                    WrapperRegistration registration = list[i];
                    if (registration.Condition(type, dataContext))
                    {
                        wrapper = WrapInternal(item, registration, dataContext);
                    }
                }
                dataContext.Remove(ItemToWrapConstant);
            }
            if (wrapper == null)
            {
                wrapper = WrapToDefaultWrapper(item, wrapperType, dataContext);
            }
            if (wrapper == null)
            {
                throw ExceptionManager.WrapperTypeNotSupported(wrapperType);
            }
            return(wrapper);
        }