Friday, July 21, 2017

The magic of LINQ

LINQ is one of my favorite features of the Microsoft .NET Framework.

 It was introduced with VS.NET 2008 / .NET 3.5, and is absolutely incredible.

I often wish we had something like it in Java, where I'm still cobbling together foreach loops to process Object Lists, SAX Parsers to process XML, and HQL/JP-QL to select data. I needed to take a list of Objects and write out XML Export in .NET.

 Here's my code:
            XDocument xmlDocument = new XDocument(new XElement("Agendas",
                from agenda in orgAgendas
                select new XElement("Agenda",
                    new XAttribute("Id", agenda.AgendaId),
                    new XElement("AgendaName", agenda.AgendaName),
                    new XElement("MeetingDateTime", agenda.MeetingDateTime),
                    new XElement("MeetingLocation", agenda.MeetingLocation),
                    new XElement("OriginalFileUrl", agenda.AgendaOrigUrl),
                    new XElement("ConvertedFileUrl", agenda.AgendaPdfUrl),
                    new XElement("Type", agenda.Type),
                    new XElement("TypeDesc", agenda.TypeDesc),
                    new XElement("CreateDate", agenda.CreateDate),
                    new XElement("Published", agenda.Publish),
                    new XElement("Sections",
                    from section in agenda.AgendaSections
                    select new XElement("Section", 
                        new XAttribute("Id", section.SectionId),
                        new XElement("Text", section.SectionText),
                        new XElement("Type", section.Type),
                        new XElement("Owner", userUtils.GetUserNameById(section.OwnerId.GetValueOrDefault())),
                        new XElement("Items",
                        from item in section.AgendaSectionItems
                            select new XElement("Item", 
                            new XAttribute("Id", item.ItemId),
                            new XElement("Text", item.ItemText),
                            new XElement("Owner", userUtils.GetUserNameById(item.OwnerId.GetValueOrDefault())),
                            new XElement("Assignee", userUtils.GetUserNameById(item.AssigneeId.GetValueOrDefault())),
                            new XElement("Completed", item.Completed),
                            new XElement("DueDate", item.DueDate),
                            new XElement("Approved", item.Approved),
                            new XElement("Attachments",
                            from attachment in item.Attachments
                                select new XElement("Attachment",
                                new XAttribute("Id", attachment.AttachmentId),
                                new XElement("Description", attachment.Description),
                                new XElement("OriginalFileUrl", attachment.OriginalUrl),
                                new XElement("ConvertedFileUrl", attachment.ConvertedUrl),
                                new XElement("Confidential", attachment.Confidential)
                                )   
                            )
                            )
                        )
                    )
                    )
                )
            ));
So elegant ... :-)

Saturday, July 15, 2017

Entity Framework Woes

I started getting a new and horrifying Entity Framework error I had never seen before. Conflicting changes detected. This may happen when trying to insert multiple entities with the same key. It looked like it was due to me saving a parent and having it cascade to save children. Soooo, I took it upon myself to save parent, save child, reconstitute relationship. This was horrifying. My code went from Exhibit A:
        /// 
        /// Helper Method.  Updates Sections and Items
        /// 
        /// Agenda ID
        /// List of AgendaSections
        /// The Current User creating Sections and Items
        private void UpdateSectionsAndItems(int agendaId, IList<AgendaSection> agendaSections, IUser currentUser)
        {
            List<AgendaSectionItem> agendaSectionItems = new List<AgendaSectionItem>();

            int sectionNumber = 0;
            foreach (AgendaSection section in agendaSections)
            {
                section.Number = sectionNumber;
                section.AgendaId = agendaId;
                section.OwnerId = currentUser.UserId;
                if (section.AgendaSectionItems != null)
                {
                    int itemNumber = 0;
                    foreach (AgendaSectionItem item in section.AgendaSectionItems)
                    {
                        item.Number = itemNumber;
                        itemNumber++;
                        item.OwnerId = currentUser.UserId;
                        agendaSectionItems.AddRange(section.AgendaSectionItems);
                    }
                }
                sectionNumber++;
            }
            db.AgendaSections.AddRange(agendaSections);
            db.AgendaSectionItems.AddRange(agendaSectionItems);
            db.SaveChanges();
        }
To the following horrifying monstrosity:
        /// 
        /// Helper Method.  Updates Sections and Items
        /// 
        /// Agenda ID
        /// List of AgendaSections
        /// The current user
        private void UpdateSectionsAndItems(int agendaId, IList<AgendaSection> agendaSections, IUser currentUser)
        {            
            // This is a horrible workaround to deal with this mysterious error: 
            //   Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.
            // That I get when I simply try to save parent (AgendaSection) and let it cascade down to the child (AgendaSectionItems).
            IDictionary<int, List<AgendaSectionItem>> tempItemDictionary = new Dictionary<int, List<AgendaSectionItem>>();
            int sectionNumber = 0;
            foreach (AgendaSection section in agendaSections)
            {
                section.Number = sectionNumber;
                section.AgendaId = agendaId;
                if (section.AgendaSectionItems != null)
                {
                    int itemNumber = 0;
                    foreach (AgendaSectionItem item in section.AgendaSectionItems)
                    {
                        item.Number = itemNumber;
                        itemNumber++;
                        item.OwnerId = currentUser.UserId;
                        if (tempItemDictionary.ContainsKey(section.Number.GetValueOrDefault()))
                        {
                            List<AgendaSectionItem> agendaSectionItems = tempItemDictionary[section.Number.GetValueOrDefault()];
                            agendaSectionItems.Add(item);
                        }
                        else
                        { 
                            List<AgendaSectionItem> agendaSectionItems = new List<AgendaSectionItem>();
                            agendaSectionItems.Add(item);
                            tempItemDictionary.Add(section.Number.GetValueOrDefault(), agendaSectionItems);
                        }
                    }
                }
                // More workaround
                section.AgendaSectionItems = null;
                sectionNumber++;
            }
            db.AgendaSections.AddRange(agendaSections);
            db.SaveChanges();

            // More workaround ... FML
            foreach (int i in tempItemDictionary.Keys)
            {
                IList<AgendaSectionItem> items = tempItemDictionary[i];
                AgendaSection agendaSection = agendaSections.First(s => s.Number == i);
                if (agendaSection != null)
                {
                    foreach (AgendaSectionItem item in items)
                    {
                        item.SectionId = agendaSection.SectionId;
                        if (agendaSection.AgendaSectionItems == null)
                        {
                            agendaSection.AgendaSectionItems = new List<AgendaSectionItem>();
                        }
                        agendaSection.AgendaSectionItems.Add(item);
                        db.AgendaSectionItems.Add(item);
                        db.SaveChanges();
                    }
                }
            }            
        }
When I finally got down to it, what I had done was accidentally mapped my primary key on a table as a foreign key in a completely incorrect mapping. So, I went and rolled back the monstrosity and will sleep better tonight.

Why can't I find a 3rd party product to send emails that works?

*sigh* it should not be that difficult.

So far, I have tried:


  • SendGrid:  They seemed to work.  However, I inadvertently spun up a new project and committed my SendGrid API Key in my source code to GitHub.  Apparently they like being Big Brother and scan GitHub for their API Keys and shut down accounts when they find it.  Great.  Shut down my account, that's fine I'm moving on ...
  • MailGun: This did ... not ... work.  I walked it in the debugger, no errors, went through all their documentation.  Just did not work. I don't have the time, patience, or bandwidth to make it work.
  • SparkPost: My current attempt.  Really hoping 3rd time is a charm. 

Wednesday, July 12, 2017

Introducing: new EDMX and a personal challenge

I am taking a break to build a Recruiter Review Website.

The Data Model is much simpler:


I am a firm believer that a good Senior Developer should be able to go into a 1 hour Interview and build a fully functional website.

I am challenging myself to build this new site in 1 day.  A 1 hour Website should be functional, but will not look polished and have nice widgets and UI, but I really do believe a good Senior Developer should be able to build a functional CRUD site in an hour.

Monday, July 10, 2017

Introducing: my EDMX Diagram

I am building my own Software Project.

Here is my Data Model (Entity Framework EDMX Diagram).



You know ... that horrifying moment when you ask yourself "is my Data Model too complex?"

Seriously though, I went with the simplest Data Model possible given the scope and functionality of my site, and keeping with 3rd Normal Form, and this is what I arrived at.

Sunday, July 9, 2017

Azure WebJobs

Document Conversion Job (WebJob)


1.      Release Build from Visual Studio


2.  Navigate to DocumentConversionJob\bin\Release and zip everything into Release.zip

To Deploy open https://portal.azure.com

Azure Web App -> Web Jobs ->  Delete existing DocumentConversionJob

Add Web Job.

Screen capture of “Add Web Job” Interface:





















3  3.  For checking on the health of the WebJob, there will be a URL:


Wednesday, July 5, 2017

C# 4.0 - dynamic ... my new best friend

Okay, I can see the opportunity for all sorts of abuse if used incorrectly.

However, I found the perfect use for C# dynamic keyword ... dealing with JSON.

Often times, I need to send back a JSON Result with a subset of my object graph, or a flattened object graph.  I only want to send back the fields that the front end needs (trying to be judicious with bandwidth).


  dynamic result = new Object();
  result.AgendaOrigFileName = agenda.AgendaOrigFileName;
  result.AgendaId = agenda.AgendaId;
  return Json(result);