GithubHelp home page GithubHelp logo

strongbytes / boilerplate-generator Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 6.0 774 KB

Generate ASP.NET Core boilerplate code using Roslyn, starting from a model entity

Home Page: https://github.com/Strongbytes/Boilerplate-Generator

License: MIT License

C# 100.00%
net-core net-5 cqrs-architectural-pattern mediator-pattern roslyn-generator automapper-profiles unit-of-work-pattern repository-pattern autofac-container asp-net-core

boilerplate-generator's Introduction

Generate ASP.NET Core boilerplate code using Roslyn, starting from a model entity

NOTE: Compatible with Visual Studio 2019 16.9+

image

  • The extension is created to work together with this modular architecture, that is the base for every Strongbytes new project;
  • You should be familiar with how CQRS Pattern works together with Mediator Pattern to reduce dependencies between objects and separate read and update operations for a data store. We use MediatR in our modular architecture to accomplish this requirement;
  • You should be familiar with Repository and Unit of Work Pattern. We implemented a Base Repository and a Base Unit of Work in the modular architecture, that can be easily extended;
  • You should be familiar with IoC. We use Autofac IoC as the IoC Container;
  • We use AutoMapper to simplify object mapping;

Usage:

Open your solution and right-click any existing model (eg. LearningPath.cs). Choose Generate Boilerplate Code... from the menu:

image

A new Window will appear, that contains 3 sections:

image

Entity Hierarchy Section

Contains a TreeView of the selected entity hierarchy (including all containing models and the base class).

Inside this TreeView you can select which properties cab be used to generate the "Domain Model"
Target Module is used to specify which application module will contain all the generated files (except for Controller and DbContext);
Controller Location us used to specify which module is the main application module (the one where all Controllers are located);
You can specify if you want for the genrated CQRS files to use Unit of Work and Repository implementations (also generated by the extension). If this option is not selected, all CQRS classes will have the application DbContext as a dependency;
You can specify which Queries and which Commands should be generated;
Note: In order for "GetPaginated" operation to be available, the project has to implement IPaginatedDataQuery and IPaginatedDataResponse<T>;

image

After you fill all the required data, the "Generate Code" button is available.

Directory Structure Section

Contains a TreeView of all the generated (or updated) classes;

All classes are mappend to the corresponding directories;

image

Generated Files:

LearningPathDomainModel.cs is the generated class that will be sent to the client as the result for all Controller Requests. It contains all the selected properties from Entity Hierarchy. The same properties are used to generated models specific for CQRS Commands (in this case only for CreateLearningPathRequestModel.cs)

image

ApplicationDbContext.cs will append (if doesn't already exist) the DbSet;

image

LearningPathsController.cs is the generated API Controller, that contains all the selected Queries and Commands;

image

LearningPathsMapper.cs contains all the generated object mappings. If the file does not exist, it will be created from scratch. If the file exists, the extension will find the existing mappings and will not replace them;

image

ILearningPathsRepository.cs is the generated Repository Interface, that implements IBaseRepository<LearningPath>;

image

LearningPathsRepository.cs is the implementation of the ILearningPathsRepository interface, that inherits from BaseRepository<LearningPath>

image

IUnitOfWork.cs is an interface that contains all the existing repositories (as interfaces). If the new generated repository (ILearningPathsRepository) does not exist as a property, will be appended. This interface implements IBaseUnitOfWork

image

UnitOfWork.cs is the class that implements IUnitOfWork interface.

image

LearningPathsModule.cs is the Autofac module that registers all the services required for the Target Module. The extension will automatically register ILearningPathsRepository, LearningPathsRepository, IUnitOfWork and UnitOfWork (if not already registered);

image

Commands CreateLearningPathCommand.cs, DeleteLearningPathCommand.cs and Queries GetLearningPathByIdQuery.cs, GetPaginatedLearningPathsQuery.cs implement IRequest<> from MediatR. Every class of this type has a specific CQRS Handler;

image image

All CQRS Handlers provide a Handle() method, where you will need to write your business logic. All handlers have the same dependencies (IUnitOfWork / DbContext for data access and IMapper for objects mapping)

image

Extensibility

In order to add a new autogenerated class to the extension, folow these steps:

  1. Create a new class that inherits BaseGenericGeneratorModel (eg. EntityRepositoryInterfaceGeneratorModel.cs). Make sure that your class ends with GeneratorModel, to match the naming guidelines for the other models;
  2. Set the Kind property to a new enum value (eg. EntityRepositoryInterface), that needs to be added to AssetKind enum;
  3. Go to MetadataGenerationService and add two new entries:

    AssetToCompilationUnitNameMapping (the name of the generated asset), exaple:

    { AssetKind.EntityRepositoryInterface, $"{CommonTokens.I}{BaseEntityPluralizedName}{CommonTokens.Repository}" }
    

    AssetToNamespaceMapping (the containing namespace), example:

     {
           AssetKind.EntityRepositoryInterface,
           new NamespaceDefinitionModel
           {
               Content = $"{NamespaceTokens.Domain}.{NamespaceTokens.Repositories}",
           }
      }
    
  4. Override UsingsBuilder and include all the required usings;
  5. Override CompilationUnitDefinition in order to:

Set Compilation Unit Type (choose between class or interface generation);

Set Access Modifier (eg: public, internal);

Set Class Attributes;

Set Inheritance Types;

public override CompilationUnitDefinitionModel CompilationUnitDefinition => new CompilationUnitDefinitionModel
{
     Type = SyntaxKind.InterfaceDeclaration,
     DefinedInheritanceTypes = new string[]
     {
         $"{CommonTokens.IBaseRepository}<{_viewModelBase.EntityTree.PrimaryEntityName()}>"
     }
};

boilerplate-generator's People

Contributors

ionutdanila avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

boilerplate-generator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.