Created
July 23, 2014 18:18
-
-
Save ctigeek/7f752024df530666642a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void GetValueIObservable2() | |
{ | |
var repo = new AccountRepository(); | |
var accounts = repo.GetAccountWithSubs("123123123"); | |
//create a cold observable from IEnumerable... | |
var accountObservable = accounts.ToObservable(); | |
//you can filter an IObservable just like an IEnumerable... | |
//.Where(a => a.AccountNumber.ToString().StartsWith("2")); | |
//create an observer that has the appropriate delegates... In this case we are only using the OnNext | |
var observer = Observer.Create<Account>(acc => | |
{ | |
Console.WriteLine(acc.AccountNumber); | |
}); | |
//subscribe to the observable... Since this is a cold observable that wraps an IEnumerable, the act of calling subscribe will iterate through the IEnumerable and call OnNext delegate for each. | |
var subscription = accountObservable.Subscribe(observer); | |
// instead of explicitly creating an observer, you can use this extension method when subscribing to just pass in the delegates. | |
//var subscription = accountObservable.Subscribe( | |
// acc => Console.WriteLine(acc.AccountNumber), | |
// ex => { }, | |
// () => { }); | |
} | |
public class AccountRepository | |
{ | |
public IEnumerable<Account> GetAccountWithSubs(string accountNumber) | |
{ | |
yield return GetAccountAsync(accountNumber).Result; | |
for (int i = 1000; i < 1200; i++) | |
{ | |
yield return GetAccountAsync(i.ToString()).Result; | |
} | |
} | |
public async Task<Account> GetAccountAsync(string accountNumber) | |
{ | |
await Task.Delay(30); | |
return new Account() {AccountNumber = accountNumber}; | |
} | |
} | |
public class Account | |
{ | |
public string AccountNumber { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment