繁体   English   中英

抽象工厂映射 EF Core .NET 6

[英]Abstract Factory Mapping EF Core .NET 6

我有很大的疑问。

我在我的支付系统中使用抽象工厂:

public abstract class Payment : Entity

    {
        protected Payment(DateTime paidDate, DateTime expiredDate, decimal total, decimal totalPaid, string payer, Document document, Address address, Email email)
        {
            Number = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10).ToUpper();
            PaidDate = paidDate;
            ExpiredDate = expiredDate;
            Total = total;
            TotalPaid = totalPaid;
            Payer = payer;
            Document = document;
            Address = address;
            Email = email;

            AddNotifications(new Contract<Payment>()
                .Requires()
                .IsLowerOrEqualsThan(0, Total, "Payment.Total", "O total não pode ser zero")
                .IsGreaterOrEqualsThan(Total, TotalPaid, "Payment.Total", "O Valor pago é menor que o valor do pagamento")
                );
        }

        public string Number { get; private set; }
        public DateTime PaidDate { get; private set; }
        public DateTime ExpiredDate { get; private set; }
        public decimal Total { get; private set; }
        public decimal TotalPaid { get; private set; }
        public string Payer { get; private set; }
        public Document Document { get; private set; }
        public Address Address { get; private set; }
        public Email Email { get; private set; }

    }
 public class CreditCardPayment : Payment
    {
        public CreditCardPayment(string cardHolderName, string cardNumber, string lastTransactionNumber, DateTime paidDate, DateTime expiredDate, decimal total, decimal totalPaid, string payer, Document document, Address address, Email email) : base(paidDate, expiredDate, total, totalPaid, payer, document, address, email)
        {
            CardHolderName = cardHolderName;
            CardNumber = cardNumber;
            LastTransactionNumber = lastTransactionNumber;
        }

        public string CardHolderName { get; private set; }
        public string CardNumber { get; private set; }
        public string LastTransactionNumber { get; private set; }
    }

我真正的疑问是如何在 DbSet 部分的 EF Core 中使用 map

因为当我尝试通过抽象实现map class时,上传do.net ef migrations add时报错

public DbSet<CreditCardPayment> creditCardPayment{ get; set; }
No suitable constructor was found for entity type 'CreditCardPayment'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'document', 'address', 'email' in 'CreditCardPayment(string cardHolderName, string cardNumber, string lastTransactionNumber, DateTime paidDate, DateTime expiredDate, decimal total, decimal totalPaid, string payer, Document document, Address address, Email email)'.

他给我带来了我必须做的

public DbSet<Payment> Payments{ get; set; }

还?

在这种情况下,在 EF CORE 中实现抽象工厂模式的最佳方式是什么

在这种情况下,在 EF CORE 中实现抽象工厂模式的最佳方式是什么

EF 始终需要空构造函数,因此您需要添加公共或受保护的空构造函数,以便在创建 DbSet 时能够相应地设置属性。

您的CreditCardPayment也没有将属性传递给基数 class。您可以做的一件事是更改为这样的内容:(为简洁起见,我将省略一些内容)

public abstract class Payment : Entity
{
    protected Payment() { }

    public Payment(DateTime paidDate, DateTime expiredDate, decimal total, decimal totalPaid, string payer, Document document, Address address, Email email)
    {
        // all properties here
    }
}

public class CreditCardPayment : Payment
{
    protected CreditCardPayment() { }

    public CreditCardPayment(DateTime paidDate, DateTime expiredDate, decimal total, decimal totalPaid, string payer, Document document, Address address, Email email)
        : base(paidDate, expiredDate, total, totalPaid, payer, document, address, email)
    {
        // the other 3 properties from credit card payment
    }
}

另一种选择是将 map 付款作为自有类型,而不是使用 inheritance。这将为您提供一个导航属性,并且映射也完全不同。 具有空构造函数的规则仍然适用。

public class CreditCardPaymentConfiguration : IEntityTypeConfiguration<CreditCardPayment>
{
    public void Configure(EntityTypeBuilder<CreditCardPayment> builder)
    {
        builder
            .OwnsOne(x => x.PaymentInfo);
    }
}

public class Payment
{

}

public class CreditCardPayment
{
    public string PropertyX { get; set; }
    public Payment PaymentInfo { get; set; }
}

AddressEmail的策略相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM