Пример #1
0
        // REVIEW: Prefers composition over extension?
        public virtual Type Resolve(Type viewType)
        {
            Require.NotNull(viewType, nameof(viewType));

            Trace.TraceInformation("[PresenterTypeResolver] Attempting to resolve '{0}'", viewType.FullName);

            var candidatePrefixes = GetCandidatePrefixesFromInterfaces(viewType)
                                    .Append(GetCandidatePrefixFromViewName(viewType.Name));

            // We also look into the view namespace and into the assembly where the view is defined.
            var nameSpaces = _defaultNamespaces
                             .Append(viewType.Namespace)
                             .Append(new AssemblyName(viewType.Assembly.FullName).Name);

            var presenterTypes
                = from candidatePrefix in candidatePrefixes.Distinct()
                  from template in
                  (from nameSpace in nameSpaces.Distinct()
                   from template in _presenterNameTemplates
                   select template.Replace("{namespace}", nameSpace))
                  let typeName = template.Replace("{presenter}", candidatePrefix + "Presenter")
                                 let presenterType = _buildManager.GetType(
                      typeName,
                      throwOnError: false,
                      ignoreCase: true)
                                                     where presenterType != null && typeof(Narvalo.Mvp.IPresenter).IsAssignableFrom(presenterType)
                                                     select presenterType;

            return(presenterTypes.FirstOrDefault());
        }
        static ConventionSearchResult PerformSearch(IView viewInstance, IEnumerable<string> viewInstanceSuffixes, IEnumerable<string> presenterTypeFullNameFormats, IBuildManager buildManager)
        {
            var viewType = viewInstance.GetType();
            var presenterType = default(Type);

            // Use the base type for pages & user controls as that is the code-behind file
            // TODO: Ensure using BaseType still works in WebSite projects with code-beside files instead of code-behind files
            if (viewType.Namespace == "ASP" &&
                (typeof(Page).IsAssignableFrom(viewType) || typeof(Control).IsAssignableFrom(viewType)))
            {
                viewType = viewType.BaseType;
            }

            // Get presenter type name from view instance type name
            var presenterTypeNames = new List<string> { GetPresenterTypeNameFromViewTypeName(viewType, viewInstanceSuffixes) };

            // Get presenter type names from implemented IView interfaces
            presenterTypeNames.AddRange(GetPresenterTypeNamesFromViewInterfaceTypeNames(viewType.GetViewInterfaces()));

            // Create candidate presenter type full names
            var candidatePresenterTypeFullNames = GenerateCandidatePresenterTypeFullNames(viewType, presenterTypeNames, presenterTypeFullNameFormats);

            // Ask the build manager to load each type until one is found
            var messages = new List<string>();
            foreach (var typeFullName in candidatePresenterTypeFullNames.Distinct())
            {
                presenterType = buildManager.GetType(typeFullName, false);

                if (presenterType == null)
                {
                    messages.Add(string.Format(
                        CultureInfo.InvariantCulture,
                        "could not find a presenter with type name {0}",
                        typeFullName
                    ));
                    continue;
                }

                if (!typeof(IPresenter).IsAssignableFrom(presenterType))
                {
                    messages.Add(string.Format(
                        CultureInfo.InvariantCulture,
                        "found, but ignored, potential presenter with type name {0} because it does not implement IPresenter",
                        typeFullName
                    ));
                    presenterType = null;
                    continue;
                }

                messages.Add(string.Format(
                    CultureInfo.InvariantCulture,
                    "found presenter with type name {0}",
                    typeFullName
                ));
                break;
            }

            return new ConventionSearchResult(
                "ConventionBasedPresenterDiscoveryStrategy:\r\n"  +
                    string.Join("\r\n", messages.Select(m => "- " + m).ToArray()),
                presenterType
            );
        }
 public Type GetType(string typeName, bool throwOnError, bool ignoreCase)
 {
     ArgumentUtility.CheckNotNullOrEmpty("typeName", typeName);
     return(_innerBuildManager.GetType(typeName, throwOnError, ignoreCase));
 }
Пример #4
0
        static ConventionSearchResult PerformSearch(IView viewInstance, IEnumerable <string> viewInstanceSuffixes, IEnumerable <string> presenterTypeFullNameFormats, IBuildManager buildManager)
        {
            var viewType      = viewInstance.GetType();
            var presenterType = default(Type);

            // Use the base type for pages & user controls as that is the code-behind file
            // TODO: Ensure using BaseType still works in WebSite projects with code-beside files instead of code-behind files
            if (viewType.Namespace == "ASP" &&
                (typeof(Page).IsAssignableFrom(viewType) || typeof(Control).IsAssignableFrom(viewType)))
            {
                viewType = viewType.BaseType;
            }

            // Get presenter type name from view instance type name
            var presenterTypeNames = new List <string> {
                GetPresenterTypeNameFromViewTypeName(viewType, viewInstanceSuffixes)
            };

            // Get presenter type names from implemented IView interfaces
            presenterTypeNames.AddRange(GetPresenterTypeNamesFromViewInterfaceTypeNames(viewType.GetViewInterfaces()));

            // Create candidate presenter type full names
            var candidatePresenterTypeFullNames = GenerateCandidatePresenterTypeFullNames(viewType, presenterTypeNames, presenterTypeFullNameFormats);

            // Ask the build manager to load each type until one is found
            var messages = new List <string>();

            foreach (var typeFullName in candidatePresenterTypeFullNames.Distinct())
            {
                presenterType = buildManager.GetType(typeFullName, false);

                if (presenterType == null)
                {
                    messages.Add(string.Format(
                                     CultureInfo.InvariantCulture,
                                     "could not find a presenter with type name {0}",
                                     typeFullName
                                     ));
                    continue;
                }

                if (!typeof(IPresenter).IsAssignableFrom(presenterType))
                {
                    messages.Add(string.Format(
                                     CultureInfo.InvariantCulture,
                                     "found, but ignored, potential presenter with type name {0} because it does not implement IPresenter",
                                     typeFullName
                                     ));
                    presenterType = null;
                    continue;
                }

                messages.Add(string.Format(
                                 CultureInfo.InvariantCulture,
                                 "found presenter with type name {0}",
                                 typeFullName
                                 ));
                break;
            }

            return(new ConventionSearchResult(
                       "ConventionBasedPresenterDiscoveryStrategy:\r\n" +
                       string.Join("\r\n", messages.Select(m => "- " + m).ToArray()),
                       presenterType
                       ));
        }
 public void GetType_WithValidTypeName_ReturnsType()
 {
     Assert.That(
         _buildManager.GetType("Remotion.Web.Infrastructure.IBuildManager, Remotion.Web", true, false),
         Is.EqualTo(typeof(IBuildManager)));
 }