From d841959512c3e300c7e340e86ad5d539c6698f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B8rge=20Nordli?= Date: Tue, 23 Feb 2016 13:09:58 +0100 Subject: [PATCH] Don't include command security proxies in Bifrost/Application. #686 Some code cleanup, including consistent ordering on generated proxies. Original issue: ProCoSys/Bifrost#45 (To get CommandSecurityProxies, reference Bifrost/Security instead of Bifrost/Application.) --- ...en_resolving_a_string_without_any_match.cs | 2 +- .../Applications/ApplicationRouteHandler.cs | 9 ++-- .../Bifrost.Web/Assets/AssetManagerRoute.cs | 5 +- .../Assets/AssetManagerRouteHandler.cs | 12 +---- .../Assets/AssetManagerRouteHttpHandler.cs | 20 +++---- Source/Bifrost.Web/Commands/CommandProxies.cs | 52 ++++++++++--------- .../Commands/CommandSecurityProxies.cs | 35 +++++++------ .../ConfigurationRouteHandler.cs | 7 +-- Source/Bifrost.Web/Hubs/HubProxies.cs | 31 +++++------ .../Bifrost.Web/Proxies/GeneratedProxies.cs | 6 +-- .../Bifrost.Web/Proxies/ProxyRouteHandler.cs | 7 +-- .../Proxies/ProxyRouteHttpHandler.cs | 12 ++--- Source/Bifrost.Web/Read/QueryProxies.cs | 42 ++++++++------- Source/Bifrost.Web/Read/ReadModelProxies.cs | 39 +++++++------- .../Security/SecurityRouteHandler.cs | 7 +-- .../Security/SecurityRouteHttpHandler.cs | 6 +-- .../Services/RestServiceRouteHandler.cs | 9 ++-- Source/Bifrost/Utils/StringMapper.cs | 20 ++----- 18 files changed, 138 insertions(+), 183 deletions(-) diff --git a/Source/Bifrost.Specs/Utils/for_StringMapper/when_resolving_a_string_without_any_match.cs b/Source/Bifrost.Specs/Utils/for_StringMapper/when_resolving_a_string_without_any_match.cs index d5cbbbee7..3502dc02c 100644 --- a/Source/Bifrost.Specs/Utils/for_StringMapper/when_resolving_a_string_without_any_match.cs +++ b/Source/Bifrost.Specs/Utils/for_StringMapper/when_resolving_a_string_without_any_match.cs @@ -25,6 +25,6 @@ public class when_resolving_a_string_without_any_match Because of = () => result = mapper.Resolve(input); - It should_return_empty = () => result.ShouldEqual(string.Empty); + It should_return_null = () => result.ShouldBeNull(); } } diff --git a/Source/Bifrost.Web/Applications/ApplicationRouteHandler.cs b/Source/Bifrost.Web/Applications/ApplicationRouteHandler.cs index cffb3b4c9..49540b39d 100644 --- a/Source/Bifrost.Web/Applications/ApplicationRouteHandler.cs +++ b/Source/Bifrost.Web/Applications/ApplicationRouteHandler.cs @@ -10,8 +10,8 @@ namespace Bifrost.Web.Applications { public class ApplicationRouteHandler : IRouteHandler { - string _url; - Assembly _assembly; + readonly string _url; + readonly Assembly _assembly; IHttpHandler _httpHandler; public ApplicationRouteHandler(string url, Assembly assembly) @@ -22,10 +22,7 @@ public ApplicationRouteHandler(string url, Assembly assembly) public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new ApplicationRouteHttpHandler(_url, _assembly); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new ApplicationRouteHttpHandler(_url, _assembly)); } } } diff --git a/Source/Bifrost.Web/Assets/AssetManagerRoute.cs b/Source/Bifrost.Web/Assets/AssetManagerRoute.cs index fde72ce20..81e0d242f 100644 --- a/Source/Bifrost.Web/Assets/AssetManagerRoute.cs +++ b/Source/Bifrost.Web/Assets/AssetManagerRoute.cs @@ -2,15 +2,14 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Reflection; using System.Web.Routing; namespace Bifrost.Web.Assets { public class AssetManagerRoute : Route { - public AssetManagerRoute(string url) - : base(url, new AssetManagerRouteHandler(url)) + public AssetManagerRoute(string url) + : base(url, new AssetManagerRouteHandler()) { } diff --git a/Source/Bifrost.Web/Assets/AssetManagerRouteHandler.cs b/Source/Bifrost.Web/Assets/AssetManagerRouteHandler.cs index ed424f66c..53e41de83 100644 --- a/Source/Bifrost.Web/Assets/AssetManagerRouteHandler.cs +++ b/Source/Bifrost.Web/Assets/AssetManagerRouteHandler.cs @@ -2,7 +2,6 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Reflection; using System.Web; using System.Web.Routing; @@ -10,20 +9,11 @@ namespace Bifrost.Web.Assets { public class AssetManagerRouteHandler : IRouteHandler { - string _url; IHttpHandler _httpHandler; - public AssetManagerRouteHandler(string url) - { - _url = url; - } - public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new AssetManagerRouteHttpHandler(_url); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new AssetManagerRouteHttpHandler()); } } } diff --git a/Source/Bifrost.Web/Assets/AssetManagerRouteHttpHandler.cs b/Source/Bifrost.Web/Assets/AssetManagerRouteHttpHandler.cs index f054f715a..a637ba8d3 100644 --- a/Source/Bifrost.Web/Assets/AssetManagerRouteHttpHandler.cs +++ b/Source/Bifrost.Web/Assets/AssetManagerRouteHttpHandler.cs @@ -2,38 +2,30 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Linq; -using System.Reflection; using System.Web; using System.Collections.Generic; -using System.IO; -using Newtonsoft.Json; -using System.Text; +using System.Web; using Bifrost.Configuration; +using Newtonsoft.Json; namespace Bifrost.Web.Assets { public class AssetManagerRouteHttpHandler : IHttpHandler { - string _url; - - public AssetManagerRouteHttpHandler(string url) - { - _url = url; - } - - public bool IsReusable { get { return true; } } + public bool IsReusable => true; public void ProcessRequest(HttpContext context) { var assetsManager = Configure.Instance.Container.Get(); IEnumerable assets = new string[0]; var extension = context.Request.Params["extension"]; - if( extension != null ) + if (extension != null) { assets = assetsManager.GetFilesForExtension(extension); if (context.Request.Params["structure"] != null) + { assets = assetsManager.GetStructureForExtension(extension); + } } var serialized = JsonConvert.SerializeObject(assets); context.Response.Write(serialized); diff --git a/Source/Bifrost.Web/Commands/CommandProxies.cs b/Source/Bifrost.Web/Commands/CommandProxies.cs index ff8882ddc..b14ee96cf 100644 --- a/Source/Bifrost.Web/Commands/CommandProxies.cs +++ b/Source/Bifrost.Web/Commands/CommandProxies.cs @@ -17,48 +17,52 @@ namespace Bifrost.Web.Commands { public class CommandProxies : IProxyGenerator { - internal static List _namespacesToExclude = new List(); + internal static List NamespacesToExclude = new List(); - ITypeDiscoverer _typeDiscoverer; - ITypeImporter _typeImporter; - ICodeGenerator _codeGenerator; - WebConfiguration _configuration; + readonly ITypeDiscoverer _typeDiscoverer; + readonly ITypeImporter _typeImporter; + readonly ICodeGenerator _codeGenerator; + readonly WebConfiguration _configuration; public static void ExcludeCommandsStartingWithNamespace(string @namespace) { - _namespacesToExclude.Add(@namespace); + NamespacesToExclude.Add(@namespace); } - public CommandProxies(ITypeDiscoverer typeDiscoverer, ITypeImporter typeImporter, ICodeGenerator codeGenerator, WebConfiguration configuration) + public CommandProxies( + ITypeDiscoverer typeDiscoverer, + ITypeImporter typeImporter, + ICodeGenerator codeGenerator, + WebConfiguration configuration) { _typeDiscoverer = typeDiscoverer; _typeImporter = typeImporter; _codeGenerator = codeGenerator; - _configuration = configuration; } + string ClientNamespace(string @namespace) + { + return _configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace) ?? Namespaces.COMMANDS; + } + public string Generate() { - var typesByNamespace = _typeDiscoverer.FindMultiple().Where(t => !_namespacesToExclude.Any(n => t.Namespace.StartsWith(n))).GroupBy(t=>t.Namespace); + var typesByNamespace = _typeDiscoverer + .FindMultiple() + .Where(t => !t.IsGenericType) + .Where(t => !NamespacesToExclude.Any(n => t.Namespace.StartsWith(n))) + .OrderBy(t => t.FullName) + .GroupBy(t => ClientNamespace(t.Namespace)) + .OrderBy(n => n.Key); var commandPropertyExtenders = _typeImporter.ImportMany(); - var result = new StringBuilder(); - Namespace currentNamespace; - Namespace globalCommands = _codeGenerator.Namespace(Namespaces.COMMANDS); - foreach (var @namespace in typesByNamespace) { - if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) - currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); - else - currentNamespace = globalCommands; - + var currentNamespace = _codeGenerator.Namespace(@namespace.Key); foreach (var type in @namespace) { - if (type.IsGenericType) continue; - var name = type.Name.ToCamelCase(); currentNamespace.Content.Assign(name) .WithType(t => @@ -72,15 +76,15 @@ public string Generate() .WithObservablePropertiesFrom(type, excludePropertiesFrom: typeof(ICommand), observableVisitor: (propertyName, observable) => { foreach (var commandPropertyExtender in commandPropertyExtenders) + { commandPropertyExtender.Extend(type, propertyName, observable); + } })); } - if (currentNamespace != globalCommands) - result.Append(_codeGenerator.GenerateFrom(currentNamespace)); + result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } - result.Append(_codeGenerator.GenerateFrom(globalCommands)); - + return result.ToString(); } } diff --git a/Source/Bifrost.Web/Commands/CommandSecurityProxies.cs b/Source/Bifrost.Web/Commands/CommandSecurityProxies.cs index 3be6f7580..230a70696 100644 --- a/Source/Bifrost.Web/Commands/CommandSecurityProxies.cs +++ b/Source/Bifrost.Web/Commands/CommandSecurityProxies.cs @@ -2,6 +2,8 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ + +using System; using System.Linq; using System.Text; using Bifrost.CodeGeneration; @@ -39,26 +41,28 @@ public CommandSecurityProxies( _commandSecurityManager = commandSecurityManager; } + string ClientNamespace(string @namespace) + { + return _configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace) ?? Namespaces.COMMANDS; + } + public string Generate() { - var typesByNamespace = _typeDiscoverer.FindMultiple().Where(t => !CommandProxies._namespacesToExclude.Any(n => t.Namespace.StartsWith(n))).GroupBy(t => t.Namespace); + var typesByNamespace = _typeDiscoverer + .FindMultiple() + .Where(t => !t.IsGenericType) + .Where(t => !CommandProxies.NamespacesToExclude.Any(t.Namespace.StartsWith)) + .OrderBy(t => t.FullName) + .GroupBy(t => ClientNamespace(t.Namespace)) + .OrderBy(n => n.Key); var result = new StringBuilder(); - Namespace currentNamespace; - Namespace globalCommands = _codeGenerator.Namespace(Namespaces.COMMANDS); - foreach (var @namespace in typesByNamespace) { - if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) - currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); - else - currentNamespace = globalCommands; - + var currentNamespace = _codeGenerator.Namespace(@namespace.Key); foreach (var type in @namespace) { - if (type.IsGenericType) continue; - - var command = _container.Get(type) as ICommand; + var command = Activator.CreateInstance(type) as ICommand; var authorizationResult = _commandSecurityManager.Authorize(command); var name = string.Format("{0}SecurityContext",type.Name.ToCamelCase()); currentNamespace.Content.Assign(name) @@ -76,11 +80,10 @@ public string Generate() ); } - if (currentNamespace != globalCommands) - result.Append(_codeGenerator.GenerateFrom(currentNamespace)); + + result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } - result.Append(_codeGenerator.GenerateFrom(globalCommands)); - + return result.ToString(); } } diff --git a/Source/Bifrost.Web/Configuration/ConfigurationRouteHandler.cs b/Source/Bifrost.Web/Configuration/ConfigurationRouteHandler.cs index 37e6ff6b4..433019784 100644 --- a/Source/Bifrost.Web/Configuration/ConfigurationRouteHandler.cs +++ b/Source/Bifrost.Web/Configuration/ConfigurationRouteHandler.cs @@ -2,8 +2,8 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Web.Routing; using System.Web; +using System.Web.Routing; namespace Bifrost.Web.Configuration { @@ -13,10 +13,7 @@ public class ConfigurationRouteHandler : IRouteHandler public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new ConfigurationRouteHttpHandler(); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new ConfigurationRouteHttpHandler()); } } } diff --git a/Source/Bifrost.Web/Hubs/HubProxies.cs b/Source/Bifrost.Web/Hubs/HubProxies.cs index cb19ad124..8872377c1 100644 --- a/Source/Bifrost.Web/Hubs/HubProxies.cs +++ b/Source/Bifrost.Web/Hubs/HubProxies.cs @@ -16,9 +16,9 @@ namespace Bifrost.Web.Hubs { public class HubProxies : IProxyGenerator { - ITypeDiscoverer _typeDiscoverer; - ICodeGenerator _codeGenerator; - WebConfiguration _configuration; + readonly ITypeDiscoverer _typeDiscoverer; + readonly ICodeGenerator _codeGenerator; + readonly WebConfiguration _configuration; public HubProxies(ITypeDiscoverer typeDiscoverer, ICodeGenerator codeGenerator, WebConfiguration configuration) { @@ -27,22 +27,23 @@ public HubProxies(ITypeDiscoverer typeDiscoverer, ICodeGenerator codeGenerator, _configuration = configuration; } + string ClientNamespace(string @namespace) + { + return _configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace) ?? Namespaces.HUBS; + } public string Generate() { - var typesByNamespace = _typeDiscoverer.FindMultiple().GroupBy(t=>t.Namespace); + var typesByNamespace = _typeDiscoverer + .FindMultiple() + .OrderBy(t => t.FullName) + .GroupBy(t => ClientNamespace(t.Namespace)) + .OrderBy(n => n.Key); var result = new StringBuilder(); - Namespace currentNamespace; - Namespace globalHubs = _codeGenerator.Namespace(Namespaces.HUBS); - foreach (var @namespace in typesByNamespace) { - if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) - currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); - else - currentNamespace = globalHubs; - + var currentNamespace = _codeGenerator.Namespace(@namespace.Key); foreach (var type in @namespace) { if (type.IsGenericType) continue; @@ -60,12 +61,8 @@ public string Generate() ); } - if (currentNamespace != globalHubs) - result.Append(_codeGenerator.GenerateFrom(currentNamespace)); - - + result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } - result.Append(_codeGenerator.GenerateFrom(globalHubs)); return result.ToString(); } diff --git a/Source/Bifrost.Web/Proxies/GeneratedProxies.cs b/Source/Bifrost.Web/Proxies/GeneratedProxies.cs index 9e58793f3..7701e7831 100644 --- a/Source/Bifrost.Web/Proxies/GeneratedProxies.cs +++ b/Source/Bifrost.Web/Proxies/GeneratedProxies.cs @@ -16,9 +16,10 @@ namespace Bifrost.Web.Proxies [Singleton] public class GeneratedProxies { + public string All { get; } + public GeneratedProxies( CommandProxies commandProxies, - CommandSecurityProxies commandSecurityProxies, QueryProxies queryProxies, ReadModelProxies readModelProxies, ServiceProxies serviceProxies, @@ -29,7 +30,6 @@ public GeneratedProxies( { var builder = new StringBuilder(); builder.Append(commandProxies.Generate()); - builder.Append(commandSecurityProxies.Generate()); builder.Append(readModelProxies.Generate()); builder.Append(queryProxies.Generate()); builder.Append(serviceProxies.Generate()); @@ -45,7 +45,5 @@ public GeneratedProxies( All = builder.ToString(); } - - public string All { get; private set; } } } diff --git a/Source/Bifrost.Web/Proxies/ProxyRouteHandler.cs b/Source/Bifrost.Web/Proxies/ProxyRouteHandler.cs index 6d0b776d4..313ab132b 100644 --- a/Source/Bifrost.Web/Proxies/ProxyRouteHandler.cs +++ b/Source/Bifrost.Web/Proxies/ProxyRouteHandler.cs @@ -2,8 +2,8 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Web.Routing; using System.Web; +using System.Web.Routing; namespace Bifrost.Web.Proxies { @@ -13,10 +13,7 @@ public class ProxyRouteHandler : IRouteHandler public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new ProxyRouteHttpHandler(); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new ProxyRouteHttpHandler()); } } } diff --git a/Source/Bifrost.Web/Proxies/ProxyRouteHttpHandler.cs b/Source/Bifrost.Web/Proxies/ProxyRouteHttpHandler.cs index b5db6f30b..5ba58b5d8 100644 --- a/Source/Bifrost.Web/Proxies/ProxyRouteHttpHandler.cs +++ b/Source/Bifrost.Web/Proxies/ProxyRouteHttpHandler.cs @@ -11,15 +11,15 @@ public class ProxyRouteHttpHandler : IHttpHandler { GeneratedProxies _proxies; - public ProxyRouteHttpHandler() - { - } - - public bool IsReusable { get { return true; } } + public bool IsReusable => true; public void ProcessRequest(HttpContext context) { - if (_proxies == null) _proxies = Configure.Instance.Container.Get(); + if (_proxies == null) + { + _proxies = Configure.Instance.Container.Get(); + } + context.Response.ContentType = "text/javascript"; context.Response.Write(_proxies.All); } diff --git a/Source/Bifrost.Web/Read/QueryProxies.cs b/Source/Bifrost.Web/Read/QueryProxies.cs index 1fdb5ca27..500c410c9 100644 --- a/Source/Bifrost.Web/Read/QueryProxies.cs +++ b/Source/Bifrost.Web/Read/QueryProxies.cs @@ -2,8 +2,8 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Text; using System.Linq; +using System.Text; using Bifrost.CodeGeneration; using Bifrost.CodeGeneration.JavaScript; using Bifrost.Execution; @@ -16,33 +16,37 @@ namespace Bifrost.Web.Read { public class QueryProxies : IProxyGenerator { - ITypeDiscoverer _typeDiscoverer; - ICodeGenerator _codeGenerator; - WebConfiguration _configuration; + readonly ITypeDiscoverer _typeDiscoverer; + readonly ICodeGenerator _codeGenerator; + readonly WebConfiguration _configuration; - public QueryProxies(ITypeDiscoverer typeDiscoverer, ICodeGenerator codeGenerator, WebConfiguration configuration) + public QueryProxies( + ITypeDiscoverer typeDiscoverer, + ICodeGenerator codeGenerator, + WebConfiguration configuration) { _typeDiscoverer = typeDiscoverer; _codeGenerator = codeGenerator; _configuration = configuration; } - public string Generate() + string ClientNamespace(string @namespace) { - var typesByNamespace = _typeDiscoverer.FindMultiple(typeof(IQueryFor<>)).GroupBy(t => t.Namespace); + return _configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace) ?? Namespaces.READ; + } + public string Generate() + { + var typesByNamespace = _typeDiscoverer + .FindMultiple(typeof(IQueryFor<>)) + .OrderBy(t => t.FullName) + .GroupBy(t => ClientNamespace(t.Namespace)) + .OrderBy(n => n.Key); var result = new StringBuilder(); - Namespace currentNamespace; - Namespace globalRead = _codeGenerator.Namespace(Namespaces.READ); - foreach (var @namespace in typesByNamespace) { - if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) - currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); - else - currentNamespace = globalRead; - + var currentNamespace = _codeGenerator.Namespace(@namespace.Key); foreach (var type in @namespace) { var name = type.Name.ToCamelCase(); @@ -56,16 +60,14 @@ public string Generate() .Property("_name", p => p.WithString(name)) .Property("_generatedFrom", p => p.WithString(type.FullName)) .Property("_readModel", p => p.WithLiteral(currentNamespace.Name + "." + queryForTypeName)) - .WithObservablePropertiesFrom(type, excludePropertiesFrom: typeof(IQueryFor<>), propertyVisitor: (p) => p.Name != "Query")); + .WithObservablePropertiesFrom(type, excludePropertiesFrom: typeof(IQueryFor<>), propertyVisitor: p => p.Name != "Query")); } - if (currentNamespace != globalRead) - result.Append(_codeGenerator.GenerateFrom(currentNamespace)); + + result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } - result.Append(_codeGenerator.GenerateFrom(globalRead)); return result.ToString(); } - } } diff --git a/Source/Bifrost.Web/Read/ReadModelProxies.cs b/Source/Bifrost.Web/Read/ReadModelProxies.cs index b1f1f98a2..b5bb7557f 100644 --- a/Source/Bifrost.Web/Read/ReadModelProxies.cs +++ b/Source/Bifrost.Web/Read/ReadModelProxies.cs @@ -16,35 +16,37 @@ namespace Bifrost.Web.Read { public class ReadModelProxies : IProxyGenerator { - - ITypeDiscoverer _typeDiscoverer; - ICodeGenerator _codeGenerator; - WebConfiguration _configuration; + readonly ITypeDiscoverer _typeDiscoverer; + readonly ICodeGenerator _codeGenerator; + readonly WebConfiguration _configuration; - public ReadModelProxies(ITypeDiscoverer typeDiscoverer, ICodeGenerator codeGenerator, WebConfiguration configuration) + public ReadModelProxies( + ITypeDiscoverer typeDiscoverer, + ICodeGenerator codeGenerator, + WebConfiguration configuration) { _typeDiscoverer = typeDiscoverer; _codeGenerator = codeGenerator; _configuration = configuration; } - public string Generate() + string ClientNamespace(string @namespace) { - var typesByNamespace = _typeDiscoverer.FindMultiple().GroupBy(t => t.Namespace); + return _configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace) ?? Namespaces.READ; + } + public string Generate() + { + var typesByNamespace = _typeDiscoverer + .FindMultiple() + .OrderBy(t => t.FullName) + .GroupBy(t => ClientNamespace(t.Namespace)) + .OrderBy(n => n.Key); var result = new StringBuilder(); - Namespace currentNamespace; - Namespace globalRead = _codeGenerator.Namespace(Namespaces.READ); - foreach (var @namespace in typesByNamespace) { - if (_configuration.NamespaceMapper.CanResolveToClient(@namespace.Key)) - currentNamespace = _codeGenerator.Namespace(_configuration.NamespaceMapper.GetClientNamespaceFrom(@namespace.Key)); - else - currentNamespace = globalRead; - - + var currentNamespace = _codeGenerator.Namespace(@namespace.Key); foreach (var type in @namespace) { var name = type.Name.ToCamelCase(); @@ -69,10 +71,9 @@ public string Generate() .WithReadModelConvenienceFunctions(type)); } - if (currentNamespace != globalRead) - result.Append(_codeGenerator.GenerateFrom(currentNamespace)); + result.Append(_codeGenerator.GenerateFrom(currentNamespace)); } - result.Append(_codeGenerator.GenerateFrom(globalRead)); + return result.ToString(); } } diff --git a/Source/Bifrost.Web/Security/SecurityRouteHandler.cs b/Source/Bifrost.Web/Security/SecurityRouteHandler.cs index bc1978ee1..b4c029275 100644 --- a/Source/Bifrost.Web/Security/SecurityRouteHandler.cs +++ b/Source/Bifrost.Web/Security/SecurityRouteHandler.cs @@ -2,8 +2,8 @@ * Copyright (c) 2008-2017 Dolittle. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ -using System.Web.Routing; using System.Web; +using System.Web.Routing; namespace Bifrost.Web.Security { @@ -13,10 +13,7 @@ public class SecurityRouteHandler : IRouteHandler public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new SecurityRouteHttpHandler(); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new SecurityRouteHttpHandler()); } } } diff --git a/Source/Bifrost.Web/Security/SecurityRouteHttpHandler.cs b/Source/Bifrost.Web/Security/SecurityRouteHttpHandler.cs index cb25974b1..bda2fad44 100644 --- a/Source/Bifrost.Web/Security/SecurityRouteHttpHandler.cs +++ b/Source/Bifrost.Web/Security/SecurityRouteHttpHandler.cs @@ -10,11 +10,7 @@ namespace Bifrost.Web.Security { public class SecurityRouteHttpHandler : IHttpHandler { - public SecurityRouteHttpHandler() - { - } - - public bool IsReusable { get { return true; } } + public bool IsReusable => true; public void ProcessRequest(HttpContext context) { diff --git a/Source/Bifrost.Web/Services/RestServiceRouteHandler.cs b/Source/Bifrost.Web/Services/RestServiceRouteHandler.cs index b40fce220..3c80aa331 100644 --- a/Source/Bifrost.Web/Services/RestServiceRouteHandler.cs +++ b/Source/Bifrost.Web/Services/RestServiceRouteHandler.cs @@ -10,8 +10,8 @@ namespace Bifrost.Web.Services { public class RestServiceRouteHandler : IRouteHandler { - Type _type; - string _url; + readonly Type _type; + readonly string _url; IHttpHandler _httpHandler; public RestServiceRouteHandler(Type type, string url) @@ -22,10 +22,7 @@ public RestServiceRouteHandler(Type type, string url) public IHttpHandler GetHttpHandler(RequestContext requestContext) { - if (_httpHandler == null) - _httpHandler = new RestServiceRouteHttpHandler(_type, _url); - - return _httpHandler; + return _httpHandler ?? (_httpHandler = new RestServiceRouteHttpHandler(_type, _url)); } } } diff --git a/Source/Bifrost/Utils/StringMapper.cs b/Source/Bifrost/Utils/StringMapper.cs index d06213fb9..235f8ea23 100644 --- a/Source/Bifrost/Utils/StringMapper.cs +++ b/Source/Bifrost/Utils/StringMapper.cs @@ -15,24 +15,16 @@ public class StringMapper : IStringMapper List _mappings = new List(); #pragma warning disable 1591 // Xml Comments - public IEnumerable Mappings { get { return _mappings; } } + public IEnumerable Mappings => _mappings; public bool HasMappingFor(string input) { - foreach (var mapping in Mappings) - if (mapping.Matches(input)) - return true; - - return false; + return Mappings.Any(mapping => mapping.Matches(input)); } public IStringMapping GetFirstMatchingMappingFor(string input) { - foreach (var mapping in Mappings) - if (mapping.Matches(input)) - return mapping; - - return null; + return Mappings.FirstOrDefault(mapping => mapping.Matches(input)); } public IEnumerable GetAllMatchingMappingsFor(string input) @@ -42,11 +34,7 @@ public IEnumerable GetAllMatchingMappingsFor(string input) public string Resolve(string input) { - var mapping = GetFirstMatchingMappingFor(input); - if (mapping != null) - return mapping.Resolve(input); - - return string.Empty; + return GetFirstMatchingMappingFor(input)?.Resolve(input); } public void AddMapping(string format, string mappedFormat)