Thursday, September 26, 2019

Conditionally added XML element

On a project I'm working on we have to generate some XML documents with a pretty complex structure. In previous version we used XmlDocument, but the code is pretty verbose, so I checked XDocument alternative from Linq to XML.

This new solution works better (looks like is even faster), but I was looking for a simple solution to skip empty nodes. We didn't want <node></node> or <node />.

So in the end I tried something like this:
using System;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            var b3 = string.Empty; // "3"
            XDocument x = new XDocument(
                new XDeclaration("1.0", "utf-8", null),
                new XElement("a",
                    new XElement("b1", "1"),
                    ElemString("b2", "2"),
                    ElemString("b3", b3),
                    new XElement("b4", string.Empty),
                    new XElement("b5", null),
                    ElemChildren("c", ElemString("c1", "11"), ElemString("c2", string.Empty)),
                    ElemChildren("d", ElemString("d1", string.Empty), ElemString("d2", string.Empty))
        static XElement ElemChildren(string name, params object[] children) {
            return children.Where( obj => obj != null).Any() ? new XElement(name, children) : null;
        static XElement ElemString(string name, string val) {
            return string.IsNullOrEmpty(val) ? null : new XElement(name, val);
which produces:
<?xml version="1.0" encoding="utf-8"?>
  <b5 />
ElemChildren() was used to exclude a node having all children Null, while ElemString() is a simple ternary function (here's only for strings, most important part).
Notice the difference between <b4></b4> and <b5 />. Also notice that node <d> is missing completely because of all Null children nodes.

Monday, September 23, 2019

.NET Core 3.0 and .NET Core 3.1 LTS

New .NET Core 3.0 was just launched today at .NET Conf but I also see that new LTS version is announced:

.NET Core 3.0 is a ‘current’ release and will be superseded by .NET Core 3.1, targeted for November 2019. .NET Core 3.1 will be a long-term supported (LTS) release (supported for at least 3 years). We recommend that you adopt .NET Core 3.0 and then adopt 3.1. It’ll be very easy to upgrade.
Nice. Moving very fast, hard to catch up with all the new announced features.

Also don't forget that .NET 2.2 will be retired at the end of this year: ".NET Core 2.2 will go EOL on 12/23"

Wednesday, September 11, 2019

eShopOnWeb PR to allow basket items removal

I did a PR request on forked Microsoft's demo project eShopOnWeb to allow basket items removal by setting the quantity to zero. See the commit 70e009b.

Also, added 2 more tests (a unit and another integration test) for new functionality. Plus another small fix in some unit testing async functions to return Task instead of void.

This sample project is a good example of architectural principles, very well described in the eBook that comes with the project. I'm thinking to do some other contributions to this project, as coding exercises.

Wednesday, September 4, 2019

.NET Core 3.0

Microsoft launched today .NET Core 3.0 preview 9, probably the last preview version before the official launch at .NET Conf, on Sept. 23rd.

They are also running local events for this launch, I already registered for one in Toronto, on Oct. 7th. See you there?

Wednesday, June 26, 2019

Software Architecture for Developers by Simon Brown

A book mainly addressed to developers, especially to those who want to switch roles to software architecture, in an agile environment.

Read more on my LinkedIn article.

Tuesday, June 18, 2019

SQL SELECT / UPDATE formatting trick

Many times I'm using this very simple SQL formatting for both SELECT & UPDATE (usually with MS SQL Server using SQL Server Management Studio or some other tool, interactively):

SELECT Count(*) -- TOP 20 A.*
-- UPDATE A SET A.Field1=B.Field1
WHERE <condition>

By default, all query is just a SELECT Count(*) which doesn't change anything but give you an idea of the number of records selected or updated later.

If I comment out the first part to something like this:

SELECT /*Count(*) --*/ TOP 20 A.*

it gives me some records that will be updated.

Then just undo the change and select the query from the UPDATE and execute the selected text. Also, the number of records updated should match the number displayed with Count(*).

It's like 3-in-1, which can easily go in comments of the ticketing system or emails.