29 July 2017

Eager loading vs Explicit loading in Entity Framework (using Northwind database)

Microsoft has an excellent article on this https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx
     public string GetCategoryEagerLoading()  
     {  
       StringBuilder sb = new StringBuilder();  
       NORTHWNDEntities ctx = new NORTHWNDEntities();  
       ctx.Configuration.LazyLoadingEnabled = false;   
       var ca = ctx.Categories.Include(p => p.Products).First(c => c.CategoryID == 1);  
       foreach (Product p in ca.Products)  
       {  
         sb.Append(p.ProductName + ",");  
       }  
       return sb.ToString();  
     }  
     public string GetCategoryExplicitLoading()  
     {  
       StringBuilder sb = new StringBuilder();  
       NORTHWNDEntities ctx = new NORTHWNDEntities();  
       ctx.Configuration.LazyLoadingEnabled = false;  
       var ca = ctx.Categories.First(c => c.CategoryID == 1);  
       ctx.Entry(ca).Collection(c => c.Products).Load();  
       foreach (Product p in ca.Products)  
       {  
         sb.Append(p.ProductName + ",");  
       }  
       return sb.ToString();  
     }  

27 July 2017

Executing stored procedure / SQL through entity framework (based on the Northwind database)

     

   static void Main(string[] args)  
   {  
       Class1 c = new Class1();  
       string s= c.GetShipper();  
       Console.Write(s);  
       Console.ReadLine();  
   }      
   class Class1  
   {  
     public string GetShipper()  
     {  
       StringBuilder sb = new StringBuilder();  
       using (var ctx = new NORTHWNDEntities())  
       {  
         var idParam = new SqlParameter  
         {  
           ParameterName = "ShipperID",  
           Value = 1  
         };  
         var ShipperList = ctx.Database.SqlQuery<Shipper>("exec GetShippers @ShipperID ", idParam).ToList<Shipper>();  
         foreach (Shipper s in ShipperList)  
         {  
           sb.Append(s.CompanyName + ",");  
         }  
         return sb.ToString();  
       }  
     }  
   }  
   public partial class Shipper  
   {  
     public Shipper()  
     {  
       this.Orders = new HashSet<Order>();  
     }  
     public int ShipperID { get; set; }  
     public string CompanyName { get; set; }  
     public string Phone { get; set; }  
     public virtual ICollection<Order> Orders { get; set; }  
   }  
 public partial class NORTHWNDEntities : DbContext  
 {  
   public NORTHWNDEntities()  
     : base("name=NORTHWNDEntities")  
   {  
   }  
   protected override void OnModelCreating(DbModelBuilder modelBuilder)  
   {  
     throw new UnintentionalCodeFirstException();  
   }  
   public virtual DbSet<Category> Categories { get; set; }  
   public virtual DbSet<CustomerDemographic> CustomerDemographics { get; set; }  
   public virtual DbSet<Customer> Customers { get; set; }  
   public virtual DbSet<Employee> Employees { get; set; }  
   public virtual DbSet<Order_Detail> Order_Details { get; set; }  
   public virtual DbSet<Order> Orders { get; set; }  
   public virtual DbSet<Product> Products { get; set; }  
   public virtual DbSet<Region> Regions { get; set; }  
   public virtual DbSet<Shipper> Shippers { get; set; }  
   public virtual DbSet<Supplier> Suppliers { get; set; }  
   public virtual DbSet<sysdiagram> sysdiagrams { get; set; }  
   public virtual DbSet<Territory> Territories { get; set; }  
 }  

08 July 2017

SOAP VS REST

I got blind sighted recently when I was asked to explain SOAP vs REST.  Even though I have been programming them for ages, nothing was coming out of my mouth.  So here I am reproducing what I think is a really good answer.

REST(REpresentational State Transfer)
REST is an architectural style. It doesn’t define so many standards like SOAP. REST is for are exposing a public API over the internet to handle CRUD operations on data. REST is focused on accessing named resources through a single consistent interface.
SOAP(Simple Object Access Protocol)
SOAP brings it’s own protocol and focuses on exposing pieces of application logic (not data) as services. SOAP exposes operations. SOAP is focused on accessing named operations, each implement some business logic through different interfaces. Though SOAP is commonly referred to as web services this is a misnomer. SOAP has very little if anything to do with the Web. REST provides true Web services based on URIs and HTTP.
Why Rest?
  • Since REST uses standard HTTP it is much simpler in just about ever way.
  • REST permits many different data formats where as SOAP only permits XML.
  • REST allows better support for browser clients due to it’s support for JSON.
  • REST has better performance and scalability. REST reads can be cached, SOAP based reads cannot be cached.
  • If security is not a major concern and we have limited resources. Or we want to create an API that will be easily used by other developers publicly then we should go with REST web services.
Why SOAP?
  • WS-Security: While SOAP supports SSL (just like REST) it also supports WS-Security which adds some enterprise security features.
  • WS-AtomicTransaction: Need ACID Transactions over a service, you’re going to need SOAP.
  • WS-ReliableMessaging: Rest doesn’t have a standard messaging system and expects clients to deal with communication failures by retrying.
  • SOAP is highly secure as it defines its own security.
  • If the security is a major concern and the resources are not limited then we should use SOAP web services. Like if we are creating a web service for banking related work then we should go with SOAP as here high security is needed.

This is how you do Agile software development :)