MarimerLLC / csla

A home for your business logic in any .NET application.
https://cslanet.com
MIT License
1.26k stars 403 forks source link

CslaImplementProperties generates invalid code #4160

Closed rockfordlhotka closed 2 months ago

rockfordlhotka commented 3 months ago

Describe the bug I tried using CslaImplementProperties in the ProjectTracker sample and it didn't work as expected. I wonder if the issue is because there is a custom base class between Csla.BusinessBase and the business class?

The issue appears to be that the generator creates an extra using like this:

using System;
using Csla;
using ;

Version and Platform CSLA version: 9

luizfbicalho commented 2 months ago

Can you copy the class here?

rockfordlhotka commented 2 months ago

Can you copy the class here?

I did the following:

  1. Open the ProjectTracker solution from main
  2. Add the generator to the BusinessLibrary project
  3. Apply the attribute to the ProjectEdit class
  4. Switch some of the properties to use the generator syntax

Failure occurs.

luizfbicalho commented 2 months ago

I changed the ProjectEdit.cs like this

  [Serializable]
  [CslaImplementPropertiesInterface<IProjectEdit>]
  public partial class ProjectEdit : CslaBaseTypes.BusinessBase<ProjectEdit>
  {

    private interface IProjectEdit
    {

      [Browsable(false)]
      [EditorBrowsable(EditorBrowsableState.Never)]
      public byte[] TimeStamp
      {
        get;
        set;
      }

      [Display(Name = "Project id")]
      public int Id
      {
        get;

      }

      [Display(Name = "Project name")]
      [Required]
      [StringLength(50)]
      public string Name
      {
        get;
        set;
      }

      public DateTime? Started
      {
        get;
        set;
      }

      public DateTime? Ended
      {
        get;
        set;
      }

      public string Description
      {
        get;
        set;
      }

      public ProjectResources Resources
      {
        get;

      }
    }

and update the generator to implement like this

//<auto-generated/>
using System;
using Csla;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace ProjectTracker.Library;

public partial class ProjectEdit
{
    public static readonly PropertyInfo<byte[]> TimeStampProperty = RegisterProperty<byte[]>(nameof(TimeStamp));
    [System.ComponentModel.Browsable(false )]
    [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never )]
    public byte[] TimeStamp
    {
        get => GetProperty(TimeStampProperty);
        set => SetProperty(TimeStampProperty, value);
    }
    public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(nameof(Id));
    [System.ComponentModel.DataAnnotations.Display( Name="Project id")]
    public int Id
    {
        get => GetProperty(IdProperty);
        private set => SetProperty(IdProperty, value);
    }
    public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
    [System.ComponentModel.DataAnnotations.Display( Name="Project name")]
    [System.ComponentModel.DataAnnotations.Required( )]
    [System.ComponentModel.DataAnnotations.StringLength(50 )]
    public string Name
    {
        get => GetProperty(NameProperty);
        set => SetProperty(NameProperty, value);
    }
    public static readonly PropertyInfo<DateTime?> StartedProperty = RegisterProperty<DateTime?>(nameof(Started));
    public DateTime? Started
    {
        get => GetProperty(StartedProperty);
        set => SetProperty(StartedProperty, value);
    }
    public static readonly PropertyInfo<DateTime?> EndedProperty = RegisterProperty<DateTime?>(nameof(Ended));
    public DateTime? Ended
    {
        get => GetProperty(EndedProperty);
        set => SetProperty(EndedProperty, value);
    }
    public static readonly PropertyInfo<string> DescriptionProperty = RegisterProperty<string>(nameof(Description));
    public string Description
    {
        get => GetProperty(DescriptionProperty);
        set => SetProperty(DescriptionProperty, value);
    }
    public static readonly PropertyInfo<ProjectResources> ResourcesProperty = RegisterProperty<ProjectResources>(nameof(Resources));
    public ProjectResources Resources
    {
        get => GetProperty(ResourcesProperty);
        private set => SetProperty(ResourcesProperty, value);
    }
}

and updated the tests

rockfordlhotka commented 2 months ago

@luizfbicalho that should fix the non-interface scenario as well? I was using .NET 9 and the latest C#, so didn't use the interface approach.

luizfbicalho commented 2 months ago

I expect that, because it's a shared code that I changed

Edit:

I validated the other generator, the result is correct too