Inheritance: OpenRasta.Configuration.MetaModel.ConfigurationModel
 public void ForMediaType(string mediaType)
 {
     var useBuilder = (IFluentTarget)_anchor;
     var resourceModel = new ResourceModel();
     resourceModel.ContentEncodings.Add(new ContentEncodingSchemeMetaModel(_scheme, mediaType));
     useBuilder.Repository.ResourceRegistrations.Add(resourceModel);
 }
 public void SetUp()
 {
     _discoverer = new ResourceMetadataDiscoverer(new OperationGrouperByUri());
     _model = new ResourceModel();
     _model.Uris.Add(new UriModel { Name = "Test", Uri = "/test-with-attributes" });
     _model.Handlers.Add(new HandlerModel(new ReflectionBasedType(new ReflectionBasedTypeSystem(), typeof(TestHandler))));
     AddHandlerResourceType(typeof (OperationResult));
 }
        public ResourceMetadata Discover(ResourceModel resource)
        {
            var metadata = new ResourceMetadata {Uris = resource.Uris};

            var filteredHandlers = resource.Handlers.Where(x => !x.Type.StaticType.IsAbstract);
            foreach (var handler in filteredHandlers)
            {
                IndexHandler(resource, handler, metadata);
            }

            return metadata;
        }
        public static HasTheUri TheUri(this IHas anchor, string uri)
        {
            var target = (IFluentTarget)anchor;

            var uriModel = new UriModel { Uri = uri };
            var resourceModel = new ResourceModel
                {
                    Uris = { uriModel },
                    Handlers = { new ReflectionBasedTypeSystem().FromClr<WebFormsDefaultHandler>() }
                };
            target.Repository.ResourceRegistrations.Add(resourceModel);
            return new HasTheUri(resourceModel, uriModel);
        }
        public OperationGroup Group(ResourceModel resourceModel, UriModel uriModel, OperationMetadata operation)
        {
            var operationCanBeGroupedByUri = _groupRegex.Match(uriModel.Uri);

            if (!operationCanBeGroupedByUri.Success)
            {
                return new OperationGroup { Name = "everything else", Path = "misc" };
            }

            string resourceName = operationCanBeGroupedByUri.Groups[1].Value.ToLower();

            return new OperationGroup
            {
                Name = string.Format("Operations about {0}", resourceName),
                Path = resourceName
            };
        }
        private void IndexHandler(ResourceModel resource, HandlerModel handler, ResourceMetadata metadata)
        {
            var exclusions = new List<string> { "ToString", "GetType", "GetHashCode", "Equals" };

            foreach (var uri in metadata.Uris)
            {
                var candidateMethods = handler.Type.StaticType.GetMethods()
                    .Where(x => x.IsPublic && !exclusions.Contains(x.Name) && !x.IsSpecialName);

                foreach (var publicMethod in candidateMethods)
                {
                    var operation = new OperationMetadata(uri, resource.ResourceKey as IType);

                    if (DiscoveryRules.All(x => x.Discover(publicMethod, operation)))
                    {
                        metadata.Add(operation);
                        operation.Group = _grouper.Group(resource, uri, operation);
                    }
                }
            }
        }
        public OperationGroup Group(ResourceModel resourceModel, UriModel uriModel, OperationMetadata operation)
        {
            if (IsUnknownReturnType(operation.ReturnType))
            {
                return new OperationGroup { Name = "Unknown", Path = "unknown" };                
            }

            if (IsEnumerableButNotAString(operation))
            {
                var collectionType = operation.ReturnType.GetElementType();
                if (operation.ReturnType.IsGenericType)
                {
                    collectionType = operation.ReturnType.GetGenericArguments()[0];
                }

                return new OperationGroup
                {
                    Name = "Collection of " + collectionType.Name,
                    Path = collectionType.Name.ToLower() + "[]"
                };
            }

            return new OperationGroup {Name = operation.ReturnType.Name, Path = operation.ReturnType.Name.ToLower()};
        }
        public static void ConventionsFrom(this IUses uses, object source)
        {
            var assembly = source.GetType().Assembly;
            // try to find a base namespace that has a child Handlers and a child Resources namespace
            var allTypes = assembly.GetExportedTypes();
            var allNamespaces = allTypes.Select(x => x.Namespace).ToList();
            var rootNamespace =
                    (
                            from @namespace in allNamespaces
                            let resourcesLocation = @namespace.IndexOf(RESOURCES_MIDDLE)
                            where resourcesLocation != -1
                            let root = @namespace.Substring(0, resourcesLocation)
                            // root is Blah whe Blah.Resources exists
                            where allNamespaces.Any(x => x.StartsWith(root + ".Handlers."))
                            select root
                    ).Union(
                            from @namespace in allNamespaces
                            where @namespace.EndsWith(RESOURCES_SUFFIX)
                            let root = @namespace.Substring(0, @namespace.Length - RESOURCES_SUFFIX.Length)

                            where allNamespaces.Any(x => x.StartsWith(root + ".Handlers"))
                            select root
                            ).FirstOrDefault();

            if (rootNamespace == null) return;

            var resourcesToRegister = from resourceType in allTypes
                                      where resourceType.Namespace.StartsWith(rootNamespace + RESOURCES_SUFFIX)
                                      select new
                                      {
                                          Path = resourceType.Namespace.Length == rootNamespace.Length
                                                         ? Enumerable.Empty<string>()
                                                         : resourceType.Namespace.Substring(rootNamespace.Length + RESOURCES_SUFFIX.Length).Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries),
                                          Type = resourceType,
                                          Handler =
                                          (
                                                  from handlerType in allTypes
                                                  where handlerType.Namespace.StartsWith(rootNamespace + ".Handlers") &&
                                                        handlerType.Name.StartsWith(resourceType.Name + "Handler")
                                                  select handlerType
                                          ).FirstOrDefault()
                                      };
            var registrar = (IFluentTarget)uses;
            foreach (var resource in resourcesToRegister)
            {
                var resourceModel = new ResourceModel
                {
                        ResourceKey = registrar.TypeSystem.FromClr(resource.Type),
                        Uris =
                                {
                                        new UriModel
                                        {
                                                Uri =
                                                        (resource.Path.Count() > 0 ? string.Join("/",resource.Path.Select(x=>x.ToLowerInvariant()).ToArray()) : string.Empty) +
                                                        "/" +
                                                        ConvertResourceNameToUri(resource.Type.Name)
                                        }
                                }
                };
                if (resource.Handler != null)
                    resourceModel.Handlers.Add(registrar.TypeSystem.FromClr(resource.Handler));
                registrar.Repository.ResourceRegistrations.Add(resourceModel);
            }
        }
示例#9
0
 public HasTheUri(ResourceModel model, UriModel uriModel)
 {
     _model = model;
     _uriModel = uriModel;
 }
示例#10
0
 public ClassDefinition(IEnumerable <ClassProperty> properties, string resourceTypeName, ResourceModel resourceRegistration)
 {
     Properties           = properties;
     ResourceTypeName     = resourceTypeName;
     ResourceRegistration = resourceRegistration;
 }