简体   繁体   中英

C# Bundle Namespaces

I have written a C#-Solution which includes many different projects. As this soltion acts as a backend, I'd like to write an API for it. The problem is that there is a relatively big amount of namespaces that the client would have to add in order to get access to necessary interfaces and classes. Is there a way to "bundle" namespaces into one a single namespace (or at least fewer namespaces) so that the Client does not have to add that many using-directives or do I have to completely restructure my backend?

Thanks alot.

Assuming you mean create an ASP.NET WebApi application then the namespaces shouldn't matter. Any consuming application will receive the data as JSON without namespaces.

You could consider using a tool like Swagger which acts as a cross platform auto-documenting api tool. From a .net point of view there are two popular libraries NSwag and Swashbuckle which help get you started. NSwag also includes tools for creating client classes by inspecting the swagger.json file like the old WSDL definitions.

As long as your JSON objects have the same property names, any client will happily deserialize them into their own namespaces classes.

The other option which might be cleaner if you are only consuming this api internally would be to reproduce all the classes you wish to output via the api in a separate class library. The api is then responsible for mapping between the existing namespaces/classes and the new ones. Tools like Automapper can help a lot with this. It has the added advantage that you can customise the mapping so that only exactly the data that is required is emitted by the api, and all extraneous data is not.

This class library could then be shared between applications either as a project reference, dll or (best if you have the right setup) a NuGet package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM