English 中文(简体)
采用追溯性推广法(Rx)创建教育、科学和技术部客户软件
原标题:Creating a REST client API using Reactive Extensions (Rx)

我试图接手关于被动延期的正确使用案例(Rx)。 不断出现的例子有:倡议活动(拖拉和下台,绘画),以及乐施会适合网络服务电话等同步应用/业务的建议。

我在申请中工作,我需要为教育、科学和技术部的服务写一个小客户的意向书。 我需要打电话四点,三点是获取一些参考数据(港口、航空公司和地位),第四点是给你一个机场飞行时间的主要服务。

我设立了介绍三个参考数据服务的班级,这些班级方法就是这样:

public Observable<IEnumerable<Airport>> GetAirports()
public Observable<IEnumerable<Airline>> GetAirlines()
public Observable<IEnumerable<Status>> GetStatuses()
public Observable<IEnumerable<Flights>> GetFlights(string airport)

在我的GetFlights方法中,我要求每个飞行员都把飞机离开的机场和飞行作业的航空公司划出。 为此,我需要获得GetAirports和GetAirlines的数据。 每一机场、航空和状态都将加到Dictionar(例如,Dictionary),以便我能够方便地确定每次飞行时间的参照点。

flight.Airport = _airports[flightNode.Attribute("airport").Value]
flight.Airline = _airlines[flightNode.Attribute("airline").Value]
flight.Status = _statuses[flightNode.Attribute("status").Value]

我目前的执行情况现在就是如此:

public IObservable<IEnumerable<Flight>> GetFlightsFrom(Airport fromAirport)
{
    var airports = new AirportNamesService().GetAirports();
    var airlines = new AirlineNamesService().GetAirlines();
    var statuses = new StatusService().GetStautses();


    var referenceData = airports
        .ForkJoin(airlines, (allAirports, allAirlines) =>
                            {
                                Airports.AddRange(allAirports);
                                Airlines.AddRange(allAirlines);
                                return new Unit();
                            })
        .ForkJoin(statuses, (nothing, allStatuses) =>
                            {
                                Statuses.AddRange(allStatuses);
                                return new Unit();
                            });

    string url = string.Format(_serviceUrl, 1, 7, fromAirport.Code);

    var flights = from data in referenceData
                    from flight in GetFlightsFrom(url)
                    select flight;

    return flights;
}

private IObservable<IEnumerable<Flight>> GetFlightsFrom(string url)
{
    return WebRequestFactory.GetData(new Uri(url), ParseFlightsXml);
}

目前的执行以Serge的答复为基础,利用ForkJoin确保按顺序执行,而且我参考数据在飞行前装上。 这一执行比像我以前一样,不得不发动一场“雷蒂达塔·洛达德”活动,更是leg。

最佳回答

我认为,如果你收到来自每个教育、科学和技术部要求的实体名单,那么你的呼吁应当有一点不同的签字——你在收复时没有看到每个价值,你正在观察完成打电话的活动。 因此,对于机场来说,应当有签名:

public IObservable<Aiports> GetAirports()

下一步是将头三个平行进行,等待所有三个部分:

var ports_lines_statuses = 
    Observable.ForkJoin(GetAirports(), GetAirlines(), GetStatuses());

第三个步骤是把上述物品与GetFlights(GetFlights)合为一体:

var decoratedFlights = 
  from pls in ports_lines_statuses
  let airport = MyAirportFunc(pls)
  from flight in GetFlights(airport)
  select flight;

EDIT:我仍然不理解为什么你的服务回来。

IObservable<Airport> 

而不是

IObservable<IEnumerable<Airport>>

AFAIK, from the REST call you get all entities at once - but maybe you do paging? Anyway, if you want RX do the buffering you could use .BufferWithCount() :

    var allAirports = new AirportNamesService()
        .GetAirports().BufferWithCount(int.MaxValue); 
...

然后可以申请ForkJoin:

var ports_lines_statuses =  
    allAirports
        .ForkJoin(allAirlines, PortsLinesSelector)
        .ForkJoin(statuses, ...

港口——线路——状态将包括一个单一的活动,讨论时间表,其中将包含所有参考数据。

EDIT:使用新的受污染清单(仅限最新释放):

allAiports = airports.Start(); 
allAirlines = airlines.Start();
allStatuses = statuses.Start();

...
whenReferenceDataLoaded =
  Observable.Join(airports.WhenCompleted()
                 .And(airlines.WhenCompleted())
                 .And(statuses.WhenCompleted())
                 Then((p, l, s) => new Unit())); 



    public static IObservable<Unit> WhenCompleted<T>(this IObservable<T> source)
    {
        return source
            .Materialize()
            .Where(n => n.Kind == NotificationKind.OnCompleted)
            .Select(_ => new Unit());
    }
问题回答

此处的使用情况是按数字计算的。 如果你想说,在出现新的飞行时通知你,然后在可观察的范围之内填平了基于REST的拉动。 基因可能具有一定价值。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签