TrackableEntities / EntityFrameworkCore.Scaffolding.Handlebars

Scaffold EF Core models using Handlebars templates.
MIT License
210 stars 53 forks source link

AddHandlebarsHelpers not working #31

Closed ufetahu closed 5 years ago

ufetahu commented 6 years ago

I've been trying to generate comments using helpers but it's not working (unlike comments added in .hbs like //comment). Its almost like if the Helper method is not being called. Also, if there is way to check the property name within i.e.: if(parameter == "CreatedOn"){ writer.write("//") } Using

netcoreapp2.1
tonysneed commented 6 years ago

I've been trying to generate comments using helpers but it's not working

Are you calling services.AddHandlebarsHelpers(myHelper)? Can you show me your ScaffoldingDesignTimeServices class?

Is there is way to check the property name

Yes, see here.

ufetahu commented 6 years ago

Hi Anthony,

Thank you for your reply. Attached you will find the ScaffoldingDesignTimeService.cs, DBContext.hbs and the results received from scaffolding in ChronDbContext.cs.

As far as my second question I was thinking whether it’s possible to check the property name within the helper itself, such as:

    void MyHbsHelper(TextWriter writer, object context, object[] parameters)
    {
        foreach (var property in parameters)
        {
            if(property.ToString() == "CreatedBy")
                writer.Write("//");
        }
    }

And in .hbs file

{{spaces 8}}{{my-helper}}public {{property-type}} {{property-name}} { get; set; }

which would comment out all the auditable/trackable properties inherited from another class.

I appreciate you taking your time.

Regards,

Uran

Vizehase commented 6 years ago

I can confirm that issue. I had the same problem when trying to include my own helpers. The method AddHandlebarsHelpers does not seem to find the internal collection in order to append the given helper to it. I succeeded by installing the helpers altogether myself by

      // Register Handlebars helper
        services.AddHandlebarsHelpers(myHelper); // does not work but the following

       services.AddSingleton(
            provider => (IHbsHelperService)
                new HbsHelperService(
                    new Dictionary<string, Action<TextWriter, object, object[]>>
                    {
                        {
                            "spaces",
                            HandlebarsHelpers.SpacesHelper
                        },
                        {
                            "MyHelper",
                            MyHelper
                        }
                    }));
ghost commented 6 years ago

I've also tried the sample project and without changing anything, it is not generating the comments in the classes. I tried it with dotnet core sdks 2.1.300, 2.1.400 and 2.2.100-*

tonysneed commented 5 years ago

Fixed by PR #42. Marked AddHandlebarsHelpers obsolete and added handlebarsHelpers parameter to AddHandlebarsScaffolding.

Fix will be available in v1.7.0 when it is released.

tonysneed commented 5 years ago

@ufetahu @Vizehase @IulianParaian This has been fixed in v1.7.0.