Dataflow.Pipeline
1.0.0
.NET에 대한 간단한 파이프 라인 표현.
일관되게 처리하는 일련의 단계를 나타냅니다.
더 나은 가독성, 확장 성 및 테스트 가능성을 위해 큰 코드를 작은 조각으로 분류 할 수 있습니다.
우선 파이프 라인 단계 사이에 전달되는 유형을 만들어야합니다.
public class InsurancePremiumModel
{
public decimal TotalPremium { get ; set ; }
}PipelineBuilder를 사용하여 파이프 라인을 생성하고 단계로 채우십시오. 예를 들어, 고객 세트의 공통 보험료를 계산하려고한다고 가정합니다.
var customers = GetCustomers ( ) ;
var customersCount = customers . Count ( ) ;
var builder = PipelineBuilder < InsurancePremiumModel >
. StartWith ( ( model , next ) => {
var basePrice = GetBasePrice ( Options . Ambulance , customers ) ;
var ambulancePremium = _ambulanceService . Calculate ( basePrice ) ;
model . TotalPremium += customersCount * ambulancePremium ;
return next . Invoke ( model ) ;
} )
. AddStep ( ( model , next ) => {
var basePrice = GetBasePrice ( Options . Dental , customers ) ;
var dentalPremium = _dentalService . Calculate ( basePrice ) ;
model . TotalPremium += customersCount * dentalPremium ;
return next . Invoke ( model ) ;
} )
. AddStep ( ( model , next ) => {
var basePrice = GetBasePrice ( Options . HomeCare , customers ) ;
var homeCarePremium = _homeCareService . Calculate ( basePrice ) ;
model . TotalPremium += customersCount * homeCarePremium ;
return next . Invoke ( model ) ;
} ) ;인터페이스 ipipelinestep을 구현하여 파이프 라인 단계를 유형으로 설명 할 수 있습니다. 아래의 예에서 우리는 RoundOffstep을 만들어 총 보험료를 마무리합니다.
public class RoundOffStep
: IPipelineStep < PriceModel >
{
public Task < PriceModel > InvokeAsync ( PriceModel input , Func < PriceModel , Task < PriceModel > > next )
{
input . TotalPremium = RoundOff ( input . TotalPremium ) ;
return next . Invoke ( input ) ;
}
private decimal RoundOff ( decimal price )
{
var rem = price % 10 ;
if ( rem == 0 ) return price ;
var result = Math . Round ( price / 10 , MidpointRounding . AwayFromZero ) * 10 ;
return rem < 5 ? result + 10.00m : result ;
}
}따라서 다음과 같은 빌더에 새로 생성 된 단계를 추가 할 수 있습니다.
builder . AddStep < RoundOffStep > ( ) ;파이프 라인 객체 사용 방법을 얻으려면 파이프 라인 빌더의 구축. 그 후 파이프 라인의 메소드 ExecuteSync를 호출하여 추가 된 모든 단계를 수행 할 수 있습니다.
var pipeline = builder . Build ( ) ;
var result = await pipeline . ExecuteAsync ( new InsurancePremiumModel ( ) ) ;