Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. public async Task<ActionResult> MyController(string Id) {
  2. . . . .
  3. MyModel model = new MyModel();
  4. var client = new ForceClient(instanceUrl, accessToken, apiVersion);
  5. var qry = await client.QueryAsync<MyModel.SFOpportunity>(
  6. "SELECT Name, StageName FROM Opportunity where Id='" + Id + "'");
  7. model.Opportunity = qry.Records.FirstOrDefault();
  8. . . . .
  9. return View(viewName, myModel);
  10. }
  11.  
  12. public async Task<ActionResult> MyController(string Id) {
  13. . . . .
  14. MyModel myModel = await Task.Run(() =>
  15. {
  16. var result = new MyModel(Id);
  17. return result;
  18.  
  19. }); // =====> Step 1
  20. . . . .
  21. return View(viewName, myInfoView);
  22. }
  23.  
  24. public class MyModel
  25. {
  26. public SFOpportunity Opportunity { get; set; }
  27. public MyModel(string id)
  28. {
  29. setOpportunityAsync(id);
  30. }
  31.  
  32. private async void setOpportunityAsync(string id)
  33. {
  34. . . .
  35. var client = new ForceClient(instanceUrl, accessToken, apiVersion);
  36. var qry = await client.QueryAsync<MyModel.SFOpportunity>(
  37. "SELECT Name, StageName FROM Opportunity where Id='" + id + "'"); // ======> Step2
  38. Opportunity = qry.Records.FirstOrDefault(); // =====> step3
  39. }
  40.  
  41. public class HasConstructorWithAsyncCall
  42. {
  43. public HasConstructorWithAsyncCall()
  44. {
  45. MarkConstructorFinishedAsync();
  46. }
  47.  
  48. public bool ConstructorHasFinished { get; private set; }
  49.  
  50. async void MarkConstructorFinishedAsync()
  51. {
  52. await Task.Delay(500);
  53. ConstructorHasFinished = true;
  54. }
  55. }
  56.  
  57. [TestMethod]
  58. public void TestWhenConstructorFinishes()
  59. {
  60. var subject = new HasConstructorWithAsyncCall();
  61. Assert.IsFalse(subject.ConstructorHasFinished);
  62. Thread.Sleep(600);
  63. Assert.IsTrue(subject.ConstructorHasFinished);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement