/// <summary>
        /// Looks for an object implementing a view for the given view model
        /// inside the given dependency injection container.
        /// </summary>
        /// <param name="viewModel">The view model whose view is looked for.</param>
        /// <param name="diContainer">The dependency injection container.</param>
        /// <returns>Instance of <see cref="IViewModelViewMatch"/> if search was successful;
        /// otherwise null.</returns>
        private static IViewModelViewMatch Match(object viewModel, IComponentContext diContainer)
        {
            // When looking for a view in the container, watch all registrations
            // connected with the type. Then look for ViewModelAttribute
            // among all type's properties and constructors

            var serviceTypes = diContainer
                               .ComponentRegistry
                               .Registrations
                               .SelectMany(r => r.Services)
                               .OfType <IServiceWithType>()
                               .Select(s => s.ServiceType);

            foreach (var serviceType in serviceTypes)
            {
                var propertyMatch = ViewModelPropertyMatch.TryMatch(
                    viewModel,
                    serviceType,
                    () => diContainer.Resolve(serviceType));

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

                var parameterMatch = ViewModelParameterMatch.TryMatch(
                    viewModel,
                    serviceType,
                    pi => diContainer.Resolve(serviceType, new NamedParameter(pi.Name, viewModel)));

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

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Tries to find a match between a property of the given type
        /// and the given view model.
        /// </summary>
        /// <param name="viewModel">The view model of a view being looked for.</param>
        /// <param name="viewType">The type of an object which properties are analyzed.</param>
        /// <param name="viewFactory">The view factory called when a match happens.</param>
        /// <returns>The result of search of an object implementing a view for the given view model;
        /// otherwise null.</returns>
        public static ViewModelPropertyMatch TryMatch(object viewModel, Type viewType, Func <object> viewFactory)
        {
            Contract.Requires <ArgumentNullException>(viewModel != null);
            Contract.Requires <ArgumentNullException>(viewType != null);
            Contract.Requires <ArgumentNullException>(viewFactory != null);

            var viewModelProperty = viewType
                                    .GetProperties()
                                    .Where(pi => pi.IsDefined(typeof(ViewModelAttribute), false))
                                    .FirstOrDefault(pi => ViewModelPropertyMatch.IsPropertyMatchToModel(pi, viewModel));

            if (viewModelProperty == null)
            {
                return(null);
            }

            // Создаем представление и задаем ему модель.
            var match = new ViewModelPropertyMatch(viewFactory(), viewModelProperty);

            match.ChangeModel(viewModel);

            return(match);
        }
Пример #3
0
 /// <summary>
 /// Gets whether the object implements a view for the given model.
 /// </summary>
 /// <param name="viewModel">The view model to be checked.</param>
 /// <returns>true, if the given object implements a view for the given model;
 /// otherwise false.</returns>
 public bool IsMatchToModel(object viewModel)
 {
     return(ViewModelPropertyMatch.IsPropertyMatchToModel(this._viewModelProperty, viewModel));
 }