将派生类型传递给重构方法

时间:2019-01-10 12:43:14

标签: c# parameter-passing generic-type-argument

在我的EF Fluent API配置中,我有许多从抽象基类派生的类

public abstract class ImageBase
{
    public int ImageId { get; set; }
    public string ImageTitle { get; set; }
    public string ImageFileExtension { get; set; }
    public string ImageDescription { get; set; }
    [NotMapped]
    public string ImageFileName => BuildImageFilename();
    public string ImageUrl { get; set; }

    //set this property to true when part of a collection
    //if it is the main image of the collection
    public bool IsMainImage { get; set; }

    //for consistency, all image filenames are the image title, with spaces replaced with underscores
    //and the image file extension
    private string BuildImageFilename()
    {
        return $"{ImageTitle.Replace(" ", "_")}{ImageFileExtension}";
    }
}

我从中衍生出许多类,下面是一个例子

 public class ArticleImageUri : ImageBase
{
    [Required]
    public int ArticleId { get; set; }
}

在该类的流利API中,我最初拥有以下内容

public void Configure(EntityTypeBuilder<ArticleImageUri> builder)
    {
        //configure primary key
        builder.HasKey(u => new { u.ImageId, u.ArticleId })
            .HasName("PK_ArticleImageUri");

        //configure properties
        builder.Property(u => u.ArticleId)
           .ValueGeneratedNever()
           .IsRequired();

        builder.Property(u => u.ImageId)
          .ValueGeneratedOnAdd()
          .IsRequired();

        builder.Property(u => u.ImageDescription)
           .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Descriptions)
           .IsRequired();

        builder.Property(u => u.ImageFileExtension)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.ShortText)
            .IsRequired();

        builder.Property(u => u.ImageTitle)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
            .IsRequired();

        builder.Property(u => u.ImageFileName)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
            .IsRequired();

        builder.Property(u => u.ImageUrl)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Urls)
            .IsRequired();

        //ConfigurationHelpers.SetColumnSizesForImageClasses(builder);
    }

注意:ConfigurationHelpers.MaxStringLengths是一个静态类,它为列的大小返回一个int值。 我将API代码重构为SetColumnSizesForImageClasses方法。

 public static void SetColumnSizesForImageClasses(EntityTypeBuilder<ArticleImageUri> builder)
    {
        //configure column sizes
        builder.Property(u => u.ImageDescription)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Descriptions)
            .IsRequired();

        builder.Property(u => u.ImageFileExtension)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.ShortText)
            .IsRequired();

        builder.Property(u => u.ImageTitle)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
            .IsRequired();

        builder.Property(u => u.ImageFileName)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
            .IsRequired();

        builder.Property(u => u.ImageUrl)
            .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Urls)
            .IsRequired();
    }

,效果很好;仅针对此类。

我认为,只要将构建器传递进去,它就可以工作,因为构建器已经是类型了,但是不起作用。 例如,我还有其他一些派生图像类型。

 public class MachineImageUri : ImageBase
{
    [Required]

    public int MachineId { get; set; }

    public byte[] ImageThumbnail { get; set; }
}

我希望能够使用重构方法为每个派生类型Fluent API配置中的重复字段定义列大小,但是我不知道如何传递适当的参数。我尝试使用T,我认为这是可行的方法,但无法使用它。

1 个答案:

答案 0 :(得分:1)

由于EntityTypeBuilder<T>是不变的,因此您将无法立即使用Configure方法。换句话说,您仍然需要两个(或更多)不同的EF模型配置。

您可以做的是创建“ helper”方法,该方法将采用具有特定类型约束的任何T

static void ConfigureBase<T>(EntityTypeBuilder<T> builder) where T : ImageBase
{
   builder.Property(u => u.ImageDescription)
        .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Descriptions)
        .IsRequired();

    builder.Property(u => u.ImageFileExtension)
        .HasMaxLength(ConfigurationHelpers.MaxStringLengths.ShortText)
        .IsRequired();

    builder.Property(u => u.ImageTitle)
        .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
        .IsRequired();

    builder.Property(u => u.ImageFileName)
        .HasMaxLength(ConfigurationHelpers.MaxStringLengths.FileNames)
        .IsRequired();

    builder.Property(u => u.ImageUrl)
        .HasMaxLength(ConfigurationHelpers.MaxStringLengths.Urls)
        .IsRequired();
}

然后您将可以通过以下方式重用它:

ArticleImageUri配置:

public void Configure(EntityTypeBuilder<ArticleImageUri> builder)
{
   ConfigureBase<ArticleImageUri>(builder);
   //other specific configurations
}

MachineImageUri配置:

public void Configure(EntityTypeBuilder<MachineImageUri> builder)
{
   ConfigureBase<MachineImageUri>(builder);
   //other specific configurations
}