Skip to content

[Feature] Add an option to generate a Guid primary key value on the entity class #549

@ArnaudB88

Description

@ArnaudB88

In our company, we use your generator for many years now with many developers. Over the years, we added some features so it works better with our solutions. As a sign of appreciation, I want to suggest some features because I think it will help some other developers as well.

Feature description:

Add an option to generate a new Guid value for the primary key property in the generated entity classes.

Feature motivation & example usage:

This allows using the primary key property before changes are saved on the dbContext.
We use the CQRS pattern with MediatR and database changes are saved automatically in the 'code pipeline'. When we have functionality which add for example a 'Project' entity, we also return the Id of the created entity to the requester (in our case we return it as a web api result). We contruct the response before database changes are done, so we already need the Id. This feature allows us to use the Id before the 'database save changes'.

Another example: when adding entities, this feature allows using the Id of the 'entities to be added' for other method calls. Sometimes we are not allowed to pass entities to other layers of the application (by architectural guidelines, seperation of concern). But the primary key value (Guid), is allowed to be passed as a value. This feature allows using the already generated value.

The option is a boolean, true enables the feature.

YAML usage:

data:
  entity:
    generatePkValue: true

Code generation result:

using System;
using System.Collections.Generic;

namespace Company.Project.Domain.Entities;

public partial class Project
{
    public Project()
    {
        #region Generated Constructor
        Id = Guid.NewGuid();
        #endregion
    }

    #region Generated Properties
    public Guid Id { get; set; }
    #endregion
}

Please review the feature request and evaluate if you think this is an addition to your repo.
(pull request will be added)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions