zijianhuang / webapiclientgen

Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
MIT License
167 stars 38 forks source link

Add code gen option for specifying the C# client file name #93

Closed ChristianWeyer closed 4 years ago

ChristianWeyer commented 4 years ago

We would need an easy way to have another file generated rather than WebApiClientAuto.cs.

Thanks.

zijianhuang commented 4 years ago

WebApiClientGen generates client codes for all nominated service functions of an ASP.NET Web API. Generally it is advised that you create one assembly for one set of client codes, thus having 1 identical client file name is adequate. And if you have a client app that need to interact multiple ASP.NET Web APIs, then just create multiple client CS projects resulting multiple client assemblies.

In contrast, for TyprScript client file, there's an option: TSFile. This is because of the way that JavaScript files get organized.

public class JSPlugin
    {
        public string AssemblyName { get; set; }

        public string TargetDir { get; set; }

        public string TSFile { get; set; }

        /// <summary>
        /// HTTP content type used in POST of HTTP of NG2. so text/plain could be used to avoid preflight in CORS.
        /// </summary>
        public string ContentType { get; set; }

        /// <summary>
        /// True to have "export namespace"; false to have "namespace". jQuery wants "namespace".
        /// </summary>
        public bool AsModule { get; set; }

        public string ClientNamespaceSuffix { get; set; } = ".Client";

        public string ContainerNameSuffix { get; set; }
    }

Do you have other use cases that really mandate custom client file name?

ChristianWeyer commented 4 years ago

HI - thanks for your fast and very detailed answer.

Indeed, one of the simplest reasons is naming conventions. Thanks!

zijianhuang commented 4 years ago

WebApiClientAuto is significant with the Suffix Auto, and I would generally exclude files with suffix Auto from static code analysis like SourceMonitor which give metrics of overall quality of the application codes, since it makes no sense to count generated codes into hand-crafted codes. May I learn what naming conventions in your org that may make defining custom cs name become significant?

ChristianWeyer commented 4 years ago

Very interesting ideas you are bringing up.

In a former life, I did a lot of code-gen stuff. E.g. https://docs.microsoft.com/en-us/archive/msdn-magazine/2009/october/schema-based-development-with-windows-communication-foundation and https://channel9.msdn.com/Shows/Endpoint/endpointtv-WCF-Contract-First-with-WSCFblue

One thing I learned there is to give people the options they need. And the freedom of generating code into files and/or folders of their choice was at the top of the list ;-)

Thanks.

zijianhuang commented 4 years ago

WebApiClientGenCore 3.7.3 released.

    /// <summary>
    /// Client APIs as output for C#, jQuery and NG etc.
    /// </summary>
    public class CodeGenOutputs
    {
        /// <summary>
        /// Assuming the client API project is the sibling of Web API project. Relative path to the WebApi project should be fine.
        /// </summary>
        public string ClientLibraryProjectFolderName { get; set; }

        /// <summary>
        /// File to be generated under Client Library Project Folder. The default is WebApiClientAuto.cs.
        /// </summary>
        public string FileName { get; set; } = "WebApiClientAuto.cs";
ChristianWeyer commented 4 years ago

Bravo! Thank you.

zijianhuang commented 4 years ago

Easy, please just star this project and respective CodeProject articles.