question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support of Futures

See original GitHub issue

Hi,

It would be a great value for the library to have Future support. What I mean by Future is the possibility to batch multiple Sql queries in a single request to the server and parse the result one time.

You can see a practical case in Ayend’s blog, But nowadays it’s more important to have such a feature.

As many applications, we have a Saas application deployed on the Cloud, a Single Page Application :

  • When the application first loads, it fetchs a lot of data from the server, just a bunch of Selects to get the current user name, the notifications, some elements on the dashboard, and other business elements etc… The Sql server is hosted in the cloud too, sometimes not the same Network than the server so there is a lot of firewall between the two. Without Future, we would need to send each request, wait for the result, parse the result, and then send the second request, during all this time Sql Server is in Idle mode ( Because of Session pooling, we won’t face TCP Warm up problem ).
  • In our application, we follow UnitOfWork Pattern, at the beginning of the request we open a transaction, at the end of the request we flush all the Updates/Inserts/Deletes, that make a big request of many queries, a fire&forget query because we don’t read anything from this point, without Future, we would need to wait for each Update/Insert/Delete to finish to run the next one, and during this time Sql Server is in Idle mode.
  • We have a SPA, so the reactivity of our application is directly related to the performance of each small webservice, and because we precalculate everything, the webservices will have just to fetch data from sql server. Think of it like a Dashbord showing a lot of data, User experience depends directly on the speed of fetching the data from the Sql Server and serving it to the App.

If we can do something like this that would be great :

public static List<Product> Search(string searchFor, int currentPage, int pageSize, out int totalRecords)
{
  using (var db = new DbNorthwind())
  {
    var products = from p in db.Product 
                   select p;

    if (searchFor != null)
    {
      products = from p in products 
                 where p.Name.Contains(searchFor) 
                 select p;
    }

    var lTotalRecords = products.Future().Count();  // That would return a Lazy<int>

    var ret = products.Skip((currentPage - 1) * pageSize).Take(pageSize).Future().ToList();  // That would return a Lazy<List<Product>>

    totalRecords = lTotalRecords.Value; // At this moment the ret.Value will have also the value set.

    return ret.Value;      
  }
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
sdanylivcommented, Nov 25, 2019

I have API prototype which is more sophisticated and has to cover more functionality. But I don’t have time this year.

var count   = db.GetTable<Person>().DeferValue(q => q.Count());
var persons = db.GetTable<Person>().DeferValue(q => q.ToList());

db.CompleteDefer();

persons.Value - will contain list and count.Value will have Count

And how to insert graph of records:

var newId  = db.GetTable<Person>()
   .Value(p => UserName, "some")
   .DeferValue(i => i.InsertWithInt32Identity());

db.GetTable<Child>()
   .Value(c => c.Name,     () => "Child1")
   .Value(c => c.PersonId, () => newId.Value)
   .Defer(i => i.Insert())

db.GetTable<Child>()
   .Value(c => c.Name,     () => "Child2")
   .Value(c => c.PersonId, () => newId.Value)
   .Defer(i => i.Insert())

db.CompleteDefer();

db.CompleteDefer() is not required if last call is not deferred.

Library should understand that database provider support multiple record-sets and combine queries accordingly.

For Firebird it will be two separated database requests:

  1. Insert into Person
  2. Insert two items into Child table

For SQL Server it will be one round trip to database, because we can store result of first insert into SQL variable.

0reactions
frankiDotNetcommented, Nov 25, 2019

@sdanyliv : Are there any news for a future support? The ef core extension has also a nice implementation. What I missed for all implementations is a kind of group, something like:

using(var db = new DbContext()){
    string idPersonQuery = 'Query1';
    string idCardsQuery = 'Query2';

    var persons =     db.GetTable<Person>().ToFuture(){Id = idPersonQuery  };
    var pCount =     db.GetTable<Person>().ToFutureCount( ){Id = idPersonQuery  };

    var cards=     db.GetTable<Cards>().ToFuture(){Id = idPersonQuery  };
    var cCount =     db.GetTable<Cards>().ToFutureCount(){Id = idPersonQuery  };

    // only persons and count of persons is executed
    var pList = persons .ToList();

    // only cards and count of cards is executed
    var cList = cards.ToList();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Futures in Stock Market: Definition, Example, and How to ...
Futures are financial contracts obligating the buyer to purchase an asset or the seller to sell an asset at a predetermined future date...
Read more >
7 Tips Every Futures Trader Should Know
Here are seven strategies to help you improve your futures trading knowledge.
Read more >
How to trade futures
Get specialized futures trading support. Have questions or need help placing a futures trade? Call our licensed Futures Specialists today at 877-553-8887.
Read more >
Futures trading FAQ
Have a question on futures trading? We've got you covered with the most frequently asked futures questions and our expert answers.
Read more >
Futures Trading: Everything You Need to Know
Futures trading is a way to speculate on or hedge against the future value of all kinds of assets, including stocks, bonds, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found