Monday, 13 October 2014

Asynchronous Entity Framework operations

Sample code:

using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace TestAsyncEntityFramework6
{
   
    public class CustomHelper
    {

        public static List<Customer> SelectAll()
        {
            using (var context = new NorthWindEntities())
            {
                var query = from c in context.Customers
                    orderby c.CustomerID ascending
                    select c;
                return query.ToList(); 
            }
        }

        public static async Task<List<Customer>> SelectAllAsync()
        {
            using (var context = new NorthWindEntities())
            {
                var query = from c in context.Customers
                    orderby c.CustomerID ascending
                    select c;
                return await query.ToListAsync();
            }
        }

        public static async Task<Customer> SelectByIdAsync(string id)
        {
            using (var context = new NorthWindEntities())
            {
                var query = from c in context.Customers
                    where c.CustomerID == id
                    select c;
                Customer obj = await query.SingleOrDefaultAsync();
                return obj;
            }
        }

        public static async Task<string> InsertAsync(Customer obj)
        {
            using (var context = new NorthWindEntities())
            {
                //try
                //{
                    context.Customers.Add(obj);
                    await context.SaveChangesAsync();
                    return "Customer added successfully!";
                //}
                //catch (Exception err)
                //{
                //    return "Crash"; 
                //}
            }
        }

        public static async Task<string> UpdateAsync(Customer obj)
        {
            using (var context = new NorthWindEntities())
            {
                Customer existing = await context.Customers.FindAsync(obj.CustomerID);
                existing.CompanyName = obj.CompanyName;
                existing.Country = obj.Country;
                await context.SaveChangesAsync();
                return "Customer updated successfully"; 
            }
        }

        public static async Task<string> DeleteAsync(string id)
        {
            using (var context = new NorthWindEntities())
            {
                Customer existing = await context.Customers.FindAsync(id);
                context.Customers.Remove(existing);
                await context.SaveChangesAsync();
                return "Customer deleted successfully!"; 
            }
        }

        public static async Task<Customer> GetCustomerByIdAsync(string id)
        {
            using (var context = new NorthWindEntities())
            {
                Customer existing = await context.Customers.FindAsync(id);
                return existing; 
            }
        }

    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestAsyncEntityFramework6
{
    class Program
    {

        static void Main(string[] args)
        {
            Stopwatch sw = Stopwatch.StartNew();
            var task = CustomHelper.SelectAllAsync();
            task.Wait();
            Console.WriteLine("Got data!");
            List<Customer> data = task.Result;
            Console.WriteLine(data.Count);
            Console.WriteLine("Async op took: " + sw.ElapsedMilliseconds);
            sw.Stop();
            sw.Start();
            //data = 
            //var data =  CustomHelper.SelectAll();
            //Console.WriteLine("Got data!");
            //Console.WriteLine(data.Count);
            //Console.WriteLine("Sync operation took: " + sw.ElapsedMilliseconds);

            var c = new Customer {CustomerID = "TEIT", Country = "Burkina Faso", CompanyName = "Tore Aurstad IT"};
            try
            {
                var task2 = CustomHelper.InsertAsync(c);
                task2.Wait();
                Console.WriteLine(task2.Result);
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.Message);
            }

            var c3 = CustomHelper.GetCustomerByIdAsync("TEIT");
            c3.Wait();
            Console.WriteLine(c3.Result.Country);

            c3.Result.Country = "Norway"; 

            var c4 = CustomHelper.UpdateAsync(c3.Result);
            c4.Wait();
            Console.WriteLine(c4.Result);

            var c5 = CustomHelper.DeleteAsync(c3.Result.CustomerID);
            c5.Wait();
            Console.WriteLine(c5.Result); 

            Console.WriteLine("Press any key to continue ...");
            Console.ReadKey(); 
        }

    }
}


No comments:

Post a Comment