Autogenerate C# WebApi from Models - c#

Does anyone know of any tools that can generate a C# WebApi REST Interface for a model?
What I would like is to define my model and add attributes that describe properties of the resource in the context of standard REST architecture. After defining the model and adding attributes, the hypothetical tool would generate all the code needed for implementation.

I'm not 100% sure if this tool will work for your very specific case, but it is a code generator nonetheless and appears to be relatively versatile.
http://www.codesmithtools.com/product/generator
I'm assuming you already searched for a code generator to suit your need so if this one doesn't work for you, you may have to face the music and write one yourself.
A co-worker I work with here wrote a generator in C# that pulled schema/data structure info from a SQL database, took that info, and simply output C# code to a .cs file.
I know this isn't the best answer, but hopefully it helps in at least some way. :)

Use OData to create simple rest services(GET, POST, PUT, DELETE). OData is being widely accepted by many UI components which allow us to use dynamic querying at request level.
You can simply replace the Model Names and DTO Names in an odata controller to use it to other model's controller.

Related

C# gRPC service without proto file

I'm looking for a way to communicate between different C# (only) apps via gRPC. Is there any way to provide service definitions and contracts by C# interfaces and POCO classes (attributed by ProtoMember, etc.), instead of creating a proto file and generating a code out of it? I need this to work on .net framework 4.7.2, i.e. working with grpc.core rather than grpc.dotnet.
PS: The main idea why I want this is to avoid the code generation part which seems to be excessive here. Also need to be able to specify custom attributes to my POCO classes, like DisplayAttribute and stuff.
Alright, looks like I found the answer https://github.com/grpc/grpc-dotnet/issues/68
protobuf-net.Grpc seems like the lib I was looking for
You can also check ServiceStack gRPC which provides code-first development.

Do I have to really create multiple models?

MS stack developer historically.
I have committed to retooling to the following stack
angular -> ms web.api2 -> C# business objects -> sql server
Being old, I develop the database from requirements and use Codesmith to generate the business logic layer. (yes, I have heard of entity framework. even tried it once).
As I embrace Angular and web API 2
I find that Angular wants me to write a model on the front end. This seems to be just a data structure, I cant even add helper methods to it
So I also often write a class with helper methods that takes an instance of the model. Kind of ugly,but it does marry structure and logic.
I find that Web API2 wants me to write a model. This again seems to be just a data structure. I am exploring the dynamic data type, but really this doesn't buy me much. Instead of writing a class, I'm writing a mapping function.
The question is this:
Is there any way around having 3+ copies of each class spread across the stack?
Codesmith is a very capable code generator... it can gen multiple files... but...
If its just a couple data members, and 3 places, I can copy paste edit and get it done.
Just seems to me that now committing to keeping a data structure in synch in 3 different environments is setting oneself up for a lot of work.
I have spent the last 15 years trying to shove as much code as I can into a framework of inheritable classes so I can keep things DRY.
Am I missing something? Are there any patterns that can be suggested?
[I know this isn't a question tailored for SO, but it is where all the smart people shop. Downvote me if you feel honor bound to do so.]
Not entirely familiar with how CodeSmith generates it's classes, but if they are just plain-old-CLR-objects that serialize nicely, you can have WebApi return them directly to your Angular application. There are purists that will frown upon this, but depending on the application, there may be a justification.
Then, in the world of Angular, you have a few options, again, depending on your requirements/justification, and your application - again, purists will definitely frown upon some of the options.
create classes that match what's coming down from the server (more correct method)
Treat everything as "any", lose type safety, and just access properties as you need them i.e. don't create the model. (obviously less correct method)
find a code generation tool that will explore API end points to determine what they return, and generate your typescript classes for you.
Personally, using Entity Framework, I (manually) create my POCO's for database interraction, have a "view"/DTO class that WebAPI would then send back to the client, and a definition of the object in Typescript, but I am a control freak, and don't like generated code.

Alternative to override getters and setters

I have some sensitive data which I want to keep encrypted in the db and decrypt it on the fly in the code. Since it's an existing application, I would like the encryption/decryption process to run as low as possible in the pipeline, so I don't have to amend my services level. My first solution was amending getters and setters for selected properties, to run those through my encryption helper. Then I thought it would be nice to have this wrapped in an attribute. Found Post Sharp extension that could do it, but seems to be an overkill for this scenario. Are there any other alternatives to achieve what I want to do?
Depending on how you use EF, you could build a wrapper on top of EF DbContext, a generic repository in which you could inject some hooks (interceptors).
One of those interceptors would be something like ICryptographyInterceptor which would handle based on the entity type the logic for encryption (on insert) or decrypt (on retrieve) and it would not pollute your business logic or models since these interceptors would handle this task.
An existing implementation can be found here.
If the project is too complex for this kind of changes, maybe a ICryptographyService for transforming entities would be a better solution.

How do I validate data in C# based on a Struts validation.xml?

I have a validation.xml file from Struts, and am going to implement a server-side validation in .NET based on it. The validation.xml file is accompanied with a validationMessages.properties file. Are there any .NET libraries which are capable of performing a validation based on a Struts validation file?
In case this has never been done I'll have to either create such a class, since the validation file is too long and complex to be implemented as mere C# logic. Which begs the question: How would I even begin?
The end-goal is to be able to populate a C# class with properties for all fields, execute a validation method with that class as a parameter and have it return a list of validation error messages (or no errors in case of success).
I'd be surprised if anything like that existed; it's relatively unusual to move from Java -> .NET.
First, see if there are any custom validators. That code would need to be duplicated.
Then pick apart the different forms (or actions, depending on how they did validation). Put each of those into a C# class (but see below) rather than one giant one. I'm not sure what you mean by "A C# class with properties for all fields"; personally I'd go more granular.
Or just use an existing C# validation package and do a translator from Apache Commons Validation to the C# configuration (or code).
It should be a relatively straight-forward process since the validation config is well-known and documented, and all the code is available.

How to properly add comments to generated class properties?

I have some classes generated by EF out of an Oracle database. Those class will be used to build a restful Web API with Help Pages. I wonder how do I add comments to the properties for those generated classes. I can edit those generated class files, but if I have to remap them, they'll be gone.
I tried creating DTO classes for the generated classes and use AutoMapper, but that quickly go out of hand since I have so many classes to create DTO for, and the worse is that the derived class will end up with two properties and that makes the Help Page not very helpful.
I hope C# lets me to redefine a property of a class, but I know that's not possible, I wonder what's the least painful way to add comments to generated classes.
You don't need to place comments in code: it can be done in an XML file.
This is an old MSDN article, but check the resulting XML document at the bottom of the article.
That is, you can create your own XML document and distribute it along with your assemblies as auto-generated ones.

Categories