Monday, October 3, 2011

Hansen8 Developers Group Meeting IX

LINQ

Swathi discussed LINQ and the many advantages it brings. She provided three examples:

var feeType = from p in db.FEETYPEs
  where p.FEEDESC.Contains("Permit")
  select p;

XDocument xmlSource = XDocument.Load("XMLFile1.xml");

var books = from book in xmlSource.Descendants("Table1")
  select new
  {
     Name = book.Element("bookName").Value,
     Author = book.Element("author").Value
  };

//using Query Expression
string[] names = { "Tom", "Rick", "Harry", "Mary", "Jay" };

IEnumerable query = from n in names
    where n.Contains("a") // Filter elements
    orderby n.Length // Sort elements
    select n; // Translate each element (project)
        
//using Lambda Expression
IEnumerable lambdaQuery = names.Where(n => n.Contains("a")).OrderBy(n => n.Length);
Finally, Swathi showed how we can now use LINQ in H8:

var requests = NewComponent();
requests.Load(x => (x.RequestType.RequestType == "Graffiti" && x.IsAssigned == true) || x.RequestType.RequestType == "Dead Animal")

Hansen8 Developers Group Meeting IIX

Windows Workflow Foundation

Anthony lightly touched on WWF and the concept of dehydration and rehydration to persist workflow to a durable medium. He showed examples of the primary events:
  • Completed
  • Terminated
  • Loaded
  • Unloaded
  • Persisted
  • Idled
I've delayed too long in writing this so I cannot elaborate further! He provided this link.

Hansen8 Developers Group Meeting VII

Extension Methods

Tim presented extension methods and used as an example several date extension methods, One of them was DaysUntil:
        /// 
        /// Gets the number of days until the specified date.
        /// 
        /// 
        /// 
        public static double DaysUntil(this DateTime dt)
        {
            DateTime nextDay;

            if (dt > DateTime.Now)
                nextDay = new DateTime(dt.Year, dt.Month, dt.Day);
            else
                nextDay = new DateTime(dt.Year + 1, dt.Month, dt.Day);

            return nextDay.Subtract (DateTime.Now).TotalDays;
        }
Usage:

  var dt = new DateTime(DateTime.Now.Year, cmbMonth.SelectedIndex + 1, cmbDay.SelectedIndex + 1);
  var du = dt.DaysUntil();
He indicated some disadvantages, chief among them being the inability to reflect upon them. There is no way of knowing via reflection that DaysUntil is an extension method. Interestingly if you write an extension method which shares the same functionality of an existing method the extension method will not get called. This means you could write extension methods for future functionality so that when recompiled for a later framework those methods would run natively without the need to delete them. Of course you could also wrap them in compiler directives or simply delete them so that also got filed under a disadvantage. He hinted at some practical extension methods for Enum such as Has, Add, Remove but did not share the source code. I've always been frustrated by the inability to easily detect if an Enum has a certain value (assuming it is decorated with a FlagAttribute. With this extension you could test via myEnum.Has(MyEnum.SomeValue).