AddHandlebarsScaffolding with Entity Framework

148 Views Asked by At

I try to make a custom class with the scaffold command.

This is my model of custom class Class.hbs

{{> imports}}
{{using-base-class}}

namespace {{namespace}}
{
{{#if comment}}
    /// <summary>
    {{comment}}
    ///
</summary>
{{/if}}
{{#each class-annotations}}
{{{class-annotation}}}
{{/each}}
    public partial class {{class}} {{base-class}}
    {
{{{> constructor}}}
{{{> properties}}}
    }
}

My C# code:

    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        services.AddHandlebarsScaffolding(options =>
        {
            options.TemplateData = new Dictionary<string, object>
            {
                { "using-base-class", "using TEST_NET_7.Interface;" },
                { "base-class", ": IEntityId" }
            };
        });
    }

enter image description here

The command working well with this configuration. It's adding my class to all generated classes, but now I want to ignore some class on scaffold. Like the entity class.

How I can do that ?

1

There are 1 best solutions below

1
TyDialexno On BEST ANSWER

This work fine for me.

using HandlebarsDotNet;
using Microsoft.EntityFrameworkCore.Design;
using Newtonsoft.Json;

namespace ScaffoldingSample
{
    public class CustomScaffoldingDesignTimeServices : IDesignTimeServices
    {
        private readonly List<string> IgnoreListClass = new()
        {
            "AspNetRole",
            "AspNetRoleClaim",
            "AspNetUser",
            "AspNetUserClaim",
            "AspNetUserLogin",
            "AspNetUserToken"
        };

        public void ConfigureDesignTimeServices(IServiceCollection services)
        {
            /// This help me to debug app.
            // System.Diagnostics.Debugger.Launch();

            services.AddHandlebarsScaffolding(options =>
            {
                options.TemplateData = new Dictionary<string, object>
                {

                };
            });

            Handlebars.RegisterHelper("base-class", WriteBaseClass);
            Handlebars.RegisterHelper("using-base-class", WriteUsingBaseClass);
        }

        void WriteBaseClass(EncodedTextWriter writer, Context context, Arguments parameters)
        {
            var obj = JsonConvert.DeserializeObject<ObjectContext>(JsonConvert.SerializeObject(context.Value));

            /// Filter of class, write or not
            if (!IgnoreListClass.Contains(obj?.@class ?? string.Empty))
                writer.Write(": IEntityId");
        }

        void WriteUsingBaseClass(EncodedTextWriter writer, Context context, Arguments parameters)
        {
            var obj = JsonConvert.DeserializeObject<ObjectContext>(JsonConvert.SerializeObject(context.Value));
            
            /// Filter of class, write or not
            if (!IgnoreListClass.Contains(obj?.@class ?? string.Empty))
                writer.Write("using TEST_NET_7.Interface;");
        }
    }

    public class ObjectContext
    {
        public string @class { get; set; } = string.Empty;
    }
}