Saturday, May 7, 2016

Yesterday I realized that most events, summoned from one locale in code, may just be written as closures.

Consider these two methods from here:

  1. public ActionResult Export()
    {
       var gridViewSettings = new GridViewSettings();
       gridViewSettings.Name = "whatever";
       gridViewSettings.KeyFieldName = "CustomerID";
       gridViewSettings.Columns.Add("ContactName");
       gridViewSettings.Columns.Add("CompanyName");
       gridViewSettings.Columns.Add("ContactTitle");
       gridViewSettings.Columns.Add("City");
       gridViewSettings.Columns.Add("Phone");
       var printable = GridViewExtension.CreatePrintableObject(gridViewSettings,
             NorthwindDataProvider.GetCustomers());
       
       PrintingSystem ps = new PrintingSystem();
       
       using (this.headerImage = Image.FromFile(Server.MapPath("~\\Content\\pic.png")))
       {
          Link header = new Link();
          header.CreateDetailArea += new
                CreateAreaEventHandler(header_CreateDetailArea);
          
          PrintableComponentLink link1 = new PrintableComponentLink(ps);
          link1.Component = printable;
          
          CompositeLink compositeLink = new CompositeLink(ps);
          compositeLink.Links.AddRange(new object[] { header, link1 });
          
          compositeLink.CreateDocument();
          using (MemoryStream stream = new MemoryStream())
          {
             compositeLink.PrintingSystem.ExportToXls(stream);
             WriteToResponse("filename", true, "xls", stream);
          }
          ps.Dispose();
       }
       return PartialView("View");
    }
  2. void header_CreateDetailArea(object sender, CreateAreaEventArgs e)
    {
       e.Graph.BorderWidth = 0;

       Rectangle r = new Rectangle(0, 0, headerImage.Width, headerImage.Height);
       e.Graph.DrawImage(headerImage, r);

       r = new Rectangle(0, headerImage.Height, 400, 50);
       e.Graph.DrawString("Additional Header information here....", r);
    }

 
 

They may be hammered into one method like so:

public ActionResult Export()
{
   var gridViewSettings = new GridViewSettings();
   gridViewSettings.Name = "whatever";
   gridViewSettings.KeyFieldName = "CustomerID";
   gridViewSettings.Columns.Add("ContactName");
   gridViewSettings.Columns.Add("CompanyName");
   gridViewSettings.Columns.Add("ContactTitle");
   gridViewSettings.Columns.Add("City");
   gridViewSettings.Columns.Add("Phone");
   var printable = GridViewExtension.CreatePrintableObject(gridViewSettings,
         NorthwindDataProvider.GetCustomers());
   
   PrintingSystem ps = new PrintingSystem();
   
   using (this.headerImage = Image.FromFile(Server.MapPath("~\\Content\\pic.png")))
   {
      Link header = new Link();
      header.CreateDetailArea += new
            CreateAreaEventHandler(
(object sender, CreateAreaEventArgs e) => {
               e.Graph.BorderWidth = 0;
            
               Rectangle r = new Rectangle(0, 0, headerImage.Width, headerImage.Height);
               e.Graph.DrawImage(headerImage, r);
            
               r = new Rectangle(0, headerImage.Height, 400, 50);
               e.Graph.DrawString("Additional Header information here....", r);
            }
);
      
      PrintableComponentLink link1 = new PrintableComponentLink(ps);
      link1.Component = printable;
      
      CompositeLink compositeLink = new CompositeLink(ps);
      compositeLink.Links.AddRange(new object[] { header, link1 });
      
      compositeLink.CreateDocument();
      using (MemoryStream stream = new MemoryStream())
      {
         compositeLink.PrintingSystem.ExportToXls(stream);
         WriteToResponse("filename", true, "xls", stream);
      }
      ps.Dispose();
   }
   return PartialView("View");
}

 
 

A followup refactoring after this might be to get rid of headerImage as a class wide variable and just have it isolated to the Export method. That seems cleaner to me.

No comments:

Post a Comment