控制器中的模式匹配以路由请求

时间:2018-02-27 16:52:25

标签: c# .net visual-studio-2017 asp.net-web-api2 c#-7.0

假设我有一个控制器:

public class MyController : ApiController
{
    [Route("Order")]
    public List<MyResponse> Post([FromBody] Order request)
    {
        //validation
        return stuff;
    }
}

我需要添加一些逻辑:

public class MyController : ApiController
{
    [Route("Order")]
    public List<MyResponse> Post([FromBody] Order request)
    {
        if (request.noOfWidgets==1) return Cat();
        if (request.noOfWidgets==0) return Dog();
    }
}

而不是使用if语句,我们可以使用模式匹配:

public class MyController : ApiController
{
    [Route("Order")]
    public List<MyResponse> Post([FromBody] Order request)
    {
        switch(request.noOfWidgets)
        {
            case(1): return Cat();
            case(0): return Dog();

        }
        return new List<MyResponse>();
    }
}

假设我们的Order对象是一个恶心的混乱:

public class Order
{
        public int noOfWidgets {get;set;}
        public bool IsSelected { get; set; }
        public bool? DisperseCharges { get; set; }
        public bool? IsAdjustable { get; set; }
        public bool? IsPackageChild { get; set; }
        public byte ChargeTypeID { get; set; }
        public byte PriceClass { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime ChargeBeginDate { get; set; }
        public DateTime ChargeEndDate { get; set; }
        public DateTime PeriodBeginDate { get; set; }
        public DateTime PeriodEndDate { get; set; }
        public DateTime QuoteDate { get; set; }
        public DateTime? EndDate { get; set; }
        public DateTime? PurchaseDate { get; set; }
        public DateTime? StepBeginDate { get; set; }
        public DateTime? StepEndDate { get; set; }
        public decimal? AgentCost { get; set; }
        public decimal? Amount { get; set; }
        public decimal? BeginRecurs { get; set; }
        public decimal? BilledAmount { get; set; }
        public decimal? BookedCost { get; set; }
        public decimal? ChargeBeginRecurs { get; set; }
        public decimal? ChargeEndRecurs { get; set; }
        public decimal? ChargeQuantity { get; set; }
        public decimal? ChargeRecurs { get; set; }
        public decimal? DisperseAmount { get; set; }
        public decimal? EndRecurs { get; set; }
        public decimal? ETLCost { get; set; }
        public decimal? Factor { get; set; }
        public decimal? Multiplier { get; set; }
        public decimal? NonPayCharge { get; set; }
        public decimal? NonPayRate { get; set; }
        public decimal? NumberOfPeriodsBilled { get; set; }
        public decimal? OtherCost { get; set; }
        public decimal? RealCost { get; set; }
        public decimal? ScheduleBeginRecurs { get; set; }
        public decimal? ScheduleEndRecurs { get; set; }
        public decimal? ScheduleTotalRecurs { get; set; }
        public decimal? SPIFFCost { get; set; }
        public decimal? StandardCharge { get; set; }
        public decimal? StandardRate { get; set; }
        public decimal? SuspendCharge { get; set; }
        public decimal? SuspendRate { get; set; }
        public Guid RequestUID { get; set; }
        public int ChargeClassID { get; set; }
        public int CycleScheduleID { get; set; }
        public int ElementClassID { get; set; }
        public int ID { get; set; }
        public int ProductComponentID { get; set; }
        public int Quantity { get; set; }
        public int QuoteID { get; set; }
        public int YYYYMMCCID { get; set; }
        public int? AccountID { get; set; }
        public int? ARItemID { get; set; }
        public int? BillingChargeClassID { get; set; }
        public int? BillingFrequencyMask { get; set; }
        public int? BillingNumber { get; set; }
        public int? ChargeDays { get; set; }
        public int? ChargeIsWaivable { get; set; }
        public int? ComponentClassID { get; set; }
        public int? ComponentID { get; set; }
        public int? CycleID { get; set; }
        public int? DaysConnected { get; set; }
        public int? DaysInPeriod { get; set; }
        public int? DepositTemplateID { get; set; }
        public int? DiscountComponentID { get; set; }
        public int? DiscountProductComponentID { get; set; }
        public int? DisperseWeight { get; set; }
        public int? DivisionID { get; set; }
        public int? ERateID { get; set; }
        public int? ExemptionTypeID { get; set; }
        public int? FractionalIsWaivable { get; set; }
        public int? GLMapID { get; set; }
        public int? GroupQuantity { get; set; }
        public int? InitialCycleScheduleID { get; set; }
        public int? IsCredit { get; set; }
        public int? ItemID { get; set; }
        public int? ItemPriceID { get; set; }
        public int? LocationID { get; set; }
        public int? NumberOfRecurrences { get; set; }
        public int? Occurrence { get; set; }
        public int? ParentItemID { get; set; }
        public int? ParentProductComponentID { get; set; }
        public int? PatronageTypeID { get; set; }
        public int? PCode { get; set; }
        public int? PriceID { get; set; }
        public int? PriceListID { get; set; }
        public int? PricePlanID { get; set; }
        public int? PriceStepID { get; set; }
        public int? PriceStepScheduleID { get; set; }
        public int? PriceStepTierID { get; set; }
        public int? PriceWeight { get; set; }
        public int? ProductID { get; set; }
        public int? ProviderID { get; set; }
        public int? ReceivableID { get; set; }
        public int? ReportAreaID { get; set; }
        public int? RevenueGLID { get; set; }
        public int? RootItemID { get; set; }
        public int? RootProductComponentID { get; set; }
        public int? RunningQuantityEnd { get; set; }
        public int? RunningQuantityStart { get; set; }
        public int? SalesRegionID { get; set; }
        public int? SegmentID { get; set; }
        public int? ServiceID { get; set; }
        public int? StepMaxQuantity { get; set; }
        public int? StepMinQuantity { get; set; }
        public int? TaxAreaID { get; set; }
        public int? TaxChargeTypeID { get; set; }
        public int? TaxCountType { get; set; }
        public int? TotalDisperseWeight { get; set; }
        public string AccountUID { get; set; }
        public string BillingFrequency { get; set; }
        public string BillingMethod { get; set; }
        public string ChargeClass { get; set; }
        public string ChargeDescription { get; set; }
        public string ChargeType { get; set; }
        public string FractionalizationMethod { get; set; }
        public string ItemStatus { get; set; }
        public string PriceScheduleAlignment { get; set; }
        public string PriceScheduleBehavior { get; set; }
        public string PrintMethod { get; set; }
        public string QuoteDescription { get; set; }
        public string ReceivableAccount { get; set; }
        public string RevenueAccount { get; set; }
        public string RoundingMethod { get; set; }
        public string TaxServType { get; set; }
        public string TaxTranType { get; set; }
        public string WaiveFractional { get; set; }
        public string WaiverMethod { get; set; }
}

我如何接受常规object,然后在其上接受模式匹配,如下所示:

public class MyController : ApiController
{
    [Route("Order")]
    public object Post([FromBody] object request) //note the change to object type rather than Order type
    {
        switch(object)
        {
            case Order order when order.noOfWidgets == 1: return Cat();
            case Order order when order.noOfWidgets == 0: return Dog();
            case Order order when order.noOfWidgets == 3 && TaxTranType == "something": return Elephant();

        }
        return new List<MyResponse>();
    }
}

您可以将api控制器和object的匹配模式转换为Order这样的具体对象,然后根据模式匹配确定要返回的类型吗?

1 个答案:

答案 0 :(得分:2)

  

你可以将api控制器object的匹配模式转换为Order这样的具体对象,然后根据模式匹配确定要返回的 type 吗?

假设requestOrder的实例,那么确实是模式匹配的工作原理。 Give something similar a try

C#7的模式匹配相当有限,但它支持类型模式,如果x is T y类型为switch(x) ... case T y,则匹配xT

然而,我不确定你想要做什么。如果request是从HTML创建的某个仲裁对象,则模式匹配不会将其转换为Order。所以,如果那是你所追求的,那么答案就是“不”。