AutoEntityGenerator – my first visual studio extension.

(First published on What The # Do I Know?)

TL;DR – There are links to the Visual Studio Marketplace and the GitHub repository at the end.

It all started when I watched a YouTube video by Amichai Mantinband called Controllers From Scratch Using .NET 8…


This content originally appeared on DEV Community and was authored by Zohar Peled

(First published on What The # Do I Know?)

TL;DR – There are links to the Visual Studio Marketplace and the GitHub repository at the end.

It all started when I watched a YouTube video by Amichai Mantinband called Controllers From Scratch Using .NET 8,
where he shows how to properly design and build an ASP.NET Controller as a part of his ASP.NET 8 REST API Tutorial. (As a side point, I highly recommend subscribing to his YouTube channel).

One of the things he talked about in this video, was the structure of end points he follows and recommend – and this structure has three parts:

Mapping from request object to domain model
Acting on the domain model
Mapping the result of that action to a response object
The example he shows is creating a product, so let me quickly show you the code for this example:

public IActionResult Create(CreateProductRequest request)
{
    // mapping request to domain
    var product = request.ToDomain();

    // acting on domain object
    _productService.Create(product);

    // mapping domain to response and return to client
    return CreatedAtAction(
        actionName: nameof(Get),
        routeValues: new { ProductId: product.Id },
        value: ProductResponse.FromDomain(product)
    );
}

Of course, in this video he creates the domain model, the different request and response DTOs, and the mapping methods between them – and this is what gave me the idea for my extension – which is a very simple idea: What if, instead of having to manually write all these DTOs and mappings, we would have a light bulb action that will generate all this code for us with just a few mouse clicks, using a simple form like the “Extract interface” form?

Armed with this idea and zero knowledge about visual studio extensions, I’ve called ChatGPT for help (and then also involved Claude.AI). The beginning was somewhat difficult since both of them sent me off to somewhat misleading directions, but after a few trials and errors, I’ve managed to get started with a simple POC that shows a light bulb menu item.
After that POC up and running it was time to start focusing on what I wanted the extension to do.

The first step was to get the menu item to only show when using the light bulb on a class or record definition – which was quite simple. Then I’ve used a lot of ChatGPT’s help with getting the information I needed from the code – the namespace, the class (or record) name, its constructors,
public properties, type parameters and other relevant information,
all collected into a class I’ve called Entity (and a bunch of other classes to go along with it such as Property, Namespace and so on):

public class Entity
{
    public Project Project { get; set; }
    public Namespace Namespace { get; set; }
    public string Name { get; set; }
    public List<Constructor> Constructors { get; set; }
    public string SourceFilePath { get; set; }
    public List<Property> Properties { get; set; }
    public List<string> TypeParameters { get; set; }
    public List<string> GenericConstraints { get; set; }
}

Getting the entity information was basically a simple mapping between Microsoft.CodeAnalysis.CSharp.Syntax definitions to my own definitions, but it is a crucial step of the process.
The next step was to build the UI, which contains a form where the user can select which properties they want to include in the DTO, how to name it, what would be its file name, what directory to save it to, and which direction to generate the mapping.

The next step was to use the information from the UI to generate a new Entity object that represents the DTO, use then use that entity and the original one to generate the code for both the DTO and the mappings, and finally, save that code in new files in the user-selected directory.

It took me about 3-4 weeks of part-time work (about 25-30 work hours a week) to get the first version to a point I feel good enough about it to release it to the public – and there’s still quite a bit of work left to do on it, but IMHO it is good enough to work with and therefore good enough to be published.

As of now, it can only be installed on Visual Studio Community 2019 or 2022, but I plan to make it available for Professional and Enterprise editions as well in the (hopefully not too distant) future.

It’s completely open source and free to use, you can check it out on GitHub or in the Visual Studio Marketplace.

If you’ve tried it, I would very much appreciate any feedback, good or bad.


This content originally appeared on DEV Community and was authored by Zohar Peled


Print Share Comment Cite Upload Translate Updates
APA

Zohar Peled | Sciencx (2024-07-16T10:56:00+00:00) AutoEntityGenerator – my first visual studio extension.. Retrieved from https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/

MLA
" » AutoEntityGenerator – my first visual studio extension.." Zohar Peled | Sciencx - Tuesday July 16, 2024, https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/
HARVARD
Zohar Peled | Sciencx Tuesday July 16, 2024 » AutoEntityGenerator – my first visual studio extension.., viewed ,<https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/>
VANCOUVER
Zohar Peled | Sciencx - » AutoEntityGenerator – my first visual studio extension.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/
CHICAGO
" » AutoEntityGenerator – my first visual studio extension.." Zohar Peled | Sciencx - Accessed . https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/
IEEE
" » AutoEntityGenerator – my first visual studio extension.." Zohar Peled | Sciencx [Online]. Available: https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/. [Accessed: ]
rf:citation
» AutoEntityGenerator – my first visual studio extension. | Zohar Peled | Sciencx | https://www.scien.cx/2024/07/16/autoentitygenerator-my-first-visual-studio-extension/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.