A SerializableDictionary in C#

If you create a static Dictionary in code, every time you need to change the dictionary, you have change code, recompile, and redeploy. Wouldn’t it be nice if you didn’t have to change code. What if you could create your dictionary in an Xml file and deserialize it. You can now make the change outside of code.

using System.Collections.Generic;
using System.Xml.Serialization;

namespace Rhyous.EasyXml
{
    [XmlRoot("Dictionary")]
    public class SerializableDictionary<TKey, TValue>
        : Dictionary<TKey, TValue>, IXmlSerializable
    {
        public string KeyName = "key";
        public string ValueName = "value";

        #region constructors
        public SerializableDictionary()
        {
        }

        public SerializableDictionary(IEqualityComparer<TKey> comparer)
            : base(comparer)
        {
        }
        #endregion



        #region IXmlSerializable Members
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            var keySerializer = new XmlSerializer(typeof(TKey), null, null, new XmlRootAttribute(KeyName), null);
            var valueSerializer = new XmlSerializer(typeof(TValue), null, null, new XmlRootAttribute(ValueName), null);

            var wasEmpty = reader.IsEmptyElement;
            reader.Read();

            if (wasEmpty)
                return;

            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                var key = (TKey)keySerializer.Deserialize(reader);
                var value = (TValue)valueSerializer.Deserialize(reader);
                Add(key, value);
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            var keySerializer = new XmlSerializer(typeof(TKey));
            var valueSerializer = new XmlSerializer(typeof(TValue));

            foreach (TKey key in Keys)
            {
                keySerializer.Serialize(writer, key);
                valueSerializer.Serialize(writer, this[key]);
            }
        }
        #endregion
    }
}

Code faster and with higher quality using code generation

Code generation is the idea of having a tool write code written for you. If you use a modern IDE, such Visual Studio, you likely use a type of code generation, even if you think you don’t.

Anytime code is written for you, it is code generation. You use code generation whenever you do the following:

  1. Create a new solution or project – Each project is a template with a base set of code ready for you to use.
  2. Add a new class, interface, or another item to a project – When adding a new class to a project, the class comes with a pre-created class object. If you add a new class called Person, the following class file would be created and added to the project:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LANDesk.Licensing.WebServices.File.Tests
    {
        class Person
        {
        }
    }
    

    The using statements are lines of code. The namespace and class definitions and the brackets are lines of code.  You get 11 lines of code not including the whitespace lines. This code was created for you because you didn’t have to write it. That doesn’t mean it is 100% useful. If you don’t use threading, the last using statement can be removed.

    Similarly, other items that are added have a base set of code.

  3. Use code snippets – Code Snippets are quite powerful. With a few characters and then the tab key twice, you can create a lot of code. There are many built-in code snippets. If you are using them, you should be.
  4. Other Visual Studio features – Visual Studio has some specific use cases where it provides code generation. For example, if you highlight a class name and choose Extract Interface, Visual Studio will generate and interface from the
  5. Plugins and 3rd party tools – Many plugins can generate code for you. For example, Resharper can do numerous code generation features, such as properly overriding the Equals method. ORMs, such as Entity Framework, have code generation tools. Entity Framework can generate most the Entities (class files that represent objects stored in database tables)  for you.

You can enhance the code generation tools

Most of these features are available as platforms for you to build upon. Enhancing these can be a simple as copying an existing item or as complex as developing your own product.

In Visual Studio, you can do any of the following: (listed in order of ease of use)

  1. Create your own snippets.
  2. Create your own class/item templates
  3. Download or purchase additional code generation plugins
  4. Create your own Project/Solution templates
  5. Create your own Visual Studio plugins/3rd party tools

If you are repeatedly writing the same code over an over again, you probably would benefit from creating a code generation solution.

Faster and higher Quality

When a human writes something, there is room for human error. If a person writes the same thing over and over, there is a tendency to minimize and cut corners. Also, there are many standard pieces of code that have already gone through significant use and bug fixes resulting in a stable and scalable piece of code. For example, overriding Equals in a class isn’t always straight forward. If you do it yourself, you might be left wondering if you have properly overridden Equals of if your implementation has well-known bugs? Do you leave those bugs in or research and fix them? If you research and fix them, how long will it take you each time you override Equals to make sure you accounted for all well-known bugs? However, if you use a generator with a standard, tested, and bug-free piece of code, your code will be higher quality and it will be created almost instantly. See, faster and higher quality.

I am hoping to have time to create a series of posts on this topic, starting with Snippets. But to get you started, check out my Visual Studio snippets for C# on GitHub.

https://github.com/rhyous/VisualCSharpSnippets

 


Using a method for the sole purpose of documentation

Some developers like to write one line of code for complex tasks. It’s called code golf and there is a whole subdomain on StackExchange dedicated to code golf. Also, I have seen an idea mentioned on some forums that you should never have a method that is a single line of code. I am going to challenge that statement and suggest that when a single line of code is difficult to understand, wrapping it in a method for the sole purpose of readability is a good practice to follow.

Below is an example of one line of code.

for (int i = 0; i < max; i++) { wsProducts[i].Features = dbContext.Products.Select(p=>p.Id == wsProducts.Id).Features.Select(f=>f.ToServiceObj()).ToList(); }

I am not going to argue whether one line of code is good or bad to have on one line. I like my for loops to be broken out like this.

for (int i = 0; i < max; i++) 
{ 
  wsProducts[i].Features = dbContext.Products.Select(p=>p.Id == wsProducts.Id).Features.Select(f=>f.ToServiceObj()).ToList();
}

But I am not going to dictate my personal preference onto other developers. That is not the point of this article. The point of this article is to talk about the benefit of a method for the sole purpose of documentation and making the code more readable. Besides, there are hundreds of other single lines of code that are difficult to understand. Thanks to Linq alone, C# now has plenty of examples. But this isn’t just a C# concept. This concept work in C++, Java, JavaScript, or any language. This concept is language agnostic.

So to start with, what is the above code doing? Can you tell from this line of code? I couldn’t at first glance. I had to examine it further. Who wrote this. (Hopefully, it wasn’t me two years ago. It probably was.)

Well, my ORM has Products and each product has a list of Features. My WebService also has Products and each Product has a list of Features. However, the ORM Product and Feature classes are not the same object types as the WebService Product and Feature classes. They are different objects in different namespaces. So basically, this code gets the list of features foreach product from the database and converts the features to a WebService Feature type, puts them in a list and assigns them to the WebService Product type’s feature list.

Wait, why did I have to explain that to you. Why didn’t you simply know what the code did? Because the code is not self-explanatory. Is is not easy to read or understand.

What if instead of our embedding our loop in our current code, we created and called this method instead?

GetFeaturesFromDatabase(MyDbContext dbContext, IEnumarable<MyWebService.Product> wsProducts) 
{
  for (int i = 0; i < max; i++) { wsProducts[i].Features = dbContext.Products.Select(p=>p.Id == wsProducts.Id).Features.Select(f=>f.ToServiceObj()).ToList(); }
}

Basically, we encapsulate (did I just use the term encapsulation outside of a CS 101 course) the complex code in a method and use the method instead.

GetFeaturesFromDatabase(dbContext, wsProducts);

Is that not clearer and easier to read?

But should we do this?

Let’s assume that our code already uses dependency injection and we already can mock the dbContext, and our code already has Unit Tests that are passing. So we don’t really need this method for any other reason other than documentation.

My answer is YES! Yes, using a method for the sole purpose making the code self-documenting and easier to read is worth it.

What do you think?


SQL Query to Entity Framework Cheat Sheet

The following are examples of SQL queries and how to execute them with Entity Framework

SELECT * with Entity Framework

SELECT * FROM Contacts
var mydbContext.Contacts.ToList();

SELECT Id with Entity Framework

This is really for how to select any single column.

SELECT Id FROM Contacts
List<int> idList = dbContext.Contacts.Select( c => c.Id ).ToList(); // Assuming Id is of type int

SELECT only one row with Entity Framework

This is really for how to select any single row from a table.

SELECT * FROM Contacts where Id = 10
var id = 10;
var contact = dbContext.Contacts.SingleOrDefault( c => c.Id = id );

SELECT only one result with Entity Framework

This is really for how to select any single value from a single row in a table.

SELECT UserId FROM Contacts where Id = 10
var id = 10;
int userId = dbContext.Contacts.Where( c => c.Id = id ).Select( c => c.UserId ).SingleOrDefault();

SELECT first result with Entity Framework

This is really for how to select the first value from a list of returned rows from a table.

SELECT TOP 1 * FROM Contacts
var id = 10;
int userId = dbContext.Contacts.FirstOrDefault();

INSERT INTO with Entity Framework

INSERT INTO dbo.LD_User (Name, Type, Active, CreateDate, CreatedBy)
VALUES ('user1', 1, 1, GetDate(), 101)
var user = new User { Name = "user1", Type = UserType.Contact, true, DateTime.Now, User.System };
dbContext.Users.Add(user);
dbContext.SaveChanges();

Note: UserType.Contact and User.System are enums in the above example.

INSERT INTO … SELECT with Entity Framework

Imagine you have these three tables. Product, ProductGroup, ProductGroupMembership. You want to make Products a member of a group by inserting into the ProductGroupMembership table.

INSERT INTO ProductGroupMembership (ProductId, GroupId, CreateDate, CreatedBy)
SELECT Id, @GroupId, GetDate(), @CreatedBy FROM Product
WHERE Id IN (1, 2, 3, 4, 5) -- there could be hundreds or thousands of numbers in the IN statement
EPIC FAIL!!! Can't be done without raw sql and opening up to sql injection attacks.

However, you can create a stored procedure that takes and user-defined table called ArrayOfInt. Then add EntityFrameworkExtras.EF6, which is available as a NuGet package, to call the storedprocedure and pass it an array.

WHERE with many AND/ORs with Entity Framework

Imagine you have these three tables. Product, ProductGroup, ProductGroupMembership. You want to make Products a member of a group by inserting into the ProductGroupMembership table.

You have a list of software Products provided to you. However, the input only includes Product.Name and Product.Version. You now need to check if the products exist and get the id.

SELECT Id FROM Product
WHERE (Name = 'Product 1' AND Version = '10.0')
WHERE (Name = 'Product 2' AND Version = '10.0')
WHERE (Name = 'Product 3' AND Version = '10.1')
WHERE (Name = 'Product 4' AND Version = '10.0')
WHERE (Name = 'Product 5' AND Version = '1.0')
EPIC FAIL!!! Can't be done without raw sql

However, you can add LinqKit’s PredicateBuilder to do this. PredicateBuilder works on top of Entity Framework and is available as a NuGet package. See how I used it here: Entity Framework and many WHERE clauses


How to create a WordPress content filter plugin?

It is pretty easy to roll out a WordPress plugin that adds a content filter.

Creating a WordPress content filter plugin

Here are the basic steps to replace the content with a filter. This example is very rudimentary and replaces all of the content, which you would probably never really do.

  1. Create a new file called MyPlugin.php
  2. Add this code:
    <?php
    /*
    Plugin Name: <Your Plugin Name>
    Version: 1.0
    Plugin URI: tba
    Description:
    Author: <your name>
    Author URI: <your web site>
    */
    
      function handleContentFilter( $content = null ) {
        return "Hello, World!";
      }
    
      $test = add_filter( "the_content", "handleContentFilter" );
    
    ?>
    
  3. Upload (or copy) MyPlugin.php to the /wp-content/plugins/ directory in your WordPress install.

Replace content based on a search string

This is more likely what you are going to do. Sames steps as above, but change the file as follows:

  function handleContentFilter( $content = null ) {
    return str_replace("FindMe","Hello, World!", $content);
  }

Using a WordPress shortcode plugin

  1. Start a new Post
  2. type in the following:

    FindMe

  3. Click Preview.

Your post should have replaced FindMe with “Hello, Word!”.

A better WordPress content filter plugin template

While the above is all you need, a more scalable solution might involve using classes. Here is a template that uses classes.

<?php
/*
Plugin Name: <Your Plugin Name>
Version: 1.0
Plugin URI: tba
Description:
Author: <your name>
Author URI: <your web site>
*/

// A class to manage your plugin
class MyPlugin {
 
  public function MyPlugin( $shortCodeHandler ) {
    $result = add_filter( 'the_content', array( $shortCodeHandler, 'handleContentFilter' ) );
  }
 
}
 
// A class to handle your shortcode
class ContentFilterHandler {
 
  public function handleContentFilter( $content = null ) {
    return str_replace("FindMe","Hello, World", $content);
  }
 
}
 
$contentFilterHandler  = new ContentFilterHandler();
$plugin = new MyPlugin( $contentFilterHandler  );

?>

How to create a WordPress shortcode plugin?

It is pretty easy to roll out a WordPress plugin that adds a shortcode.

Creating a WordPress shortcode plugin

Here are the basic steps:

  1. Create a new file called MyPlugin.php
  2. Add this code:
    <?php
    /*
    Plugin Name: <Your Plugin Name>
    Version: 1.0
    Plugin URI: tba
    Description:
    Author: <your name>
    Author URI: <your web site>
    */
    
      function handleShortcode( $atts, $content ) {
        return "Hello, World!";
      }
    
      $test = add_shortcode( 'my-shortcode', 'handleShortcode' );
    
    ?>
    
  3. Upload (or copy) MyPlugin.php to the /wp-content/plugins/ directory in your WordPress install.

Using a WordPress shortcode plugin

  1. Start a new Post
  2. type in the following:

    [my-shortcode]

  3. Click Preview.

Your post should have replaced your shortcode with “Hello, Word!”.

A better WordPress shortcode plugin template

While the above is all you need, a more scalable solution might involve using classes. Here is a template that uses classes.

<?php
/*
Plugin Name: <Your Plugin Name>
Version: 1.0
Plugin URI: tba
Description:
Author: <your name>
Author URI: <your web site>
*/

// A class to manage your plugin
class MyPlugin {
 
  public function MyPlugin( $shortCodeHandler ) {
    $result = add_shortcode( 'my-shortcode', array( $shortCodeHandler, 'handleShortcode' ) );
  }
 
}
 
// A class to handle your shortcode
class ShortCodeHandler {
 
  public function handleShortcode( $atts, $content ) {
    return "Hello, World";
  }
 
}
 
$shortCodeHandler = new ShortCodeHandler();
$plugin = new MyPlugin( $shortCodeHandler );

?>

SQL Addendum Table

I have an application I am building that needs to be malleable. It is a data-driven application. It will have users, contacts, organizations, and many other objects represented as a database table. One goal of this project is to allow for extension. Some customers are going to want to add a field to an object that our tables don’t include. We want to handle this end to end. It seems the perfect use of a property value table.

It would be pretty easy to create an Addendum table for each object.

dbo.Organization
dbo.OrganizationAddendum
dbo.User
dbo.UserAddendum

While that is OK, it requires additional work every time a table is created. What if a Partner writes a plugin and adds an object in the database? Well, unless the Partner creates an addendum table, this won’t really work.

Is there a way to solve this so any object in the database can have Addendum data?

I came up with this table.

CREATE TABLE [dbo].[LD_Addendum](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Table] [nvarchar](100) NOT NULL,
	[TableId] [int] NOT NULL,
	[Property] [nvarchar](255) NOT NULL,
	[Value] [nvarchar](255) NOT NULL,
	[CreateDate] [datetime2](7) NOT NULL,
	[LastUpdated] [datetime2](7) NULL,
	[CreatedBy] [int] NOT NULL,
	[LastUpdatedBy] [int] NULL,
 CONSTRAINT [PK_Addendum_Id] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY])

Then in the Web interface, I could have single template control that works for any object. Whatever object I am editing, be it user, contact, organization or other, the control would exist. If a partner adds a plugin with a new object, the control would exist. Seems easy enough, right?

The problem comes in with some of the features that we would like to be automatically handled on the database side:

  1. Table should have Id column
  2. Table should have four main fields
    • Table <– Database table to add addendum data for
    • TableId <– The row of in the table that addendum is for
    • Property <– The property of the addendum
    • Value <– the value of the addendum data
  3. Table should have the four auditing fields in IAuditTable
    • CreateDate
    • CreatedBy
    • LastUpdated
    • LastUpdatedBy
  4. Only one Property of the same name should exist per table and id. Easily done with a Unique constraint.
  5. Table should have a constraint that enforces that table must exist.
    I found a way to do this: I created User-defined Function (UDF) and check constraint that uses the UDF.

    CREATE FUNCTION [dbo].[TableExists](@TableName NVARCHAR(255))
    RETURNS bit
    AS
    BEGIN
    RETURN ((SELECT COUNT(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @TableName))
    END
    
  6. Table should have a constraint that enforces that TableId must exist in the Table
    1. Not supported – CLR UDFin C#? Or handle this in code?
  7. The row should delete when Table is deleted. Similar to ON DELETE CASCADE.
    • Not supported – CLR UDFin C#? Or handle this in code?
  8. The row should delete when a row from a table that matches Table and Id is deleted. Similar to ON DELETE CASCADE.
    • Not supported – CLR UDFin C#? Or handle this in code?

Perhaps we ignore the missing features from database side and handle them with code?

Or perhaps another database system other than Microsoft SQL Server (such as Postgresql) could do this?

Scaling

Assuming I got this to work, I see one main problem: Table size. However, I am not sure this is an issue. Tables can quite large, millions of rows. If this table got too big, then we could investigate why, analyze the table, and perhaps move a property value from the Addendum table to an actual column in a real table. This should replace the ability to create a plugin with an additional table, but it should make it so few plugins are needed as there is more extensibility out of the box.

Also, we found that default values often alleviate addendum tables. For example, imagine adding an addendum item to an organization, ContactIntervalInDays. Say a company is supposed to contact their customers every 90 days. However, some customers might require more or less contact. However, the default is 90. Instead of adding 90 to all customers, you set a default. If ContactIntervalInDays is not in the Addendum table, then use 90, otherwise use the value.

Anyway, it seems like an Addendum table is something that most projects and solutions, such as CRMs, Shopping Carts, ERPs, etc. should implement. It won’t solve the most complex issues with extending a product, but it would perhaps solve many of them. The more complex extension can continue to be added via a well-designed plugin architecture.

Unfortunately, this simplistic solution is not supported. The recommendation is to have 1 addendum table for every regular table. Ugh! That doesn’t scale and is not maintainable long term.

Still, I went ahead and requested this feature from the SQL team.


Entity Framework and many WHERE clauses

So today, I needed to get Entity Framework to return me a list of Products from the database based on a list of Product.Name and Product.Version values (not Ids). If it were Product.Id, it would have been simple as I could have used an IN statement, but it wasn’t.

The query might get many (maybe hundreds at a time) products based on the list. Here is the query I imagined.

So when doing a query like this, since there could be hundreds, I have a couple of options.

  1. Query the database once for each product.
    SELECT * FROM dbo.Product
    WHERE (Name = 'Product 1' AND Version = '10.0')
    

    Repeat this same query once for each Product.

  2. Query the database one time with an or clause for each Product.Name and Product.Version.
    SELECT * FROM dbo.Product
    WHERE (Name = 'Product 1' AND Version = '10.0')
       OR (Name = 'Product 2' AND Version = '10.0')
       OR (Name = 'Product 3' AND Version = '10.0')
       OR (Name = 'Product 4' AND Version = '1.0')
       -- There could be hundreds
    
  3. Query the database once and get all products and use code to find the ones I wanted.
    SELECT * FROM dbo.Product
    

Option 1
I didn’t like this option because I could end up doing hundreds of single queries. That doesn’t sound like a good idea. What would the performance impact would be when doing hundreds of single queries? The overhead of traversing over the network to the database would prevent this option from scaling.

Option 2
This is the option I imagined in my head. My gut said to use this option.

Option 3
This would work. We only have about two thousand products today and querying them all would, right now, not be bad at all. However, we just bought a company and will be adding more products. We plan to buy more companies. Also, we have two companies that we have already bought and have yet to add those products in. When would the number of Product rows in the database make the SELECT * and invalid option? Doing this would work now, but it leave a time bomb for some future developer encounter and have to fix.

Winner: Option 2

Problem
Entity Framework doesn’t really have an easy way to create the Option 2 query.

So, how do I create this query with many where statements?

Here are the two options I’ve found:

Inline SQL Query with Entity Framework

        private static List<Product> GetProductsByNameAndVersion(ActivationDbContext dbContext, IEnumerable<ProductRequest> products)
        {
            if (!products.Any())
            {
                return new List<Product>();
            }
            var query = "Select * FROM Product WHERE ";
            var or = "";
            var template = "(Name = '{0}' AND Version = '{1}')";
            foreach (var prod in products)
            {
                query += or;
                query += string.Format(template, prod.Name, prod.Version);
                or = " OR ";
            }
            var dbProducts = dbContext.Products.SqlQuery(query).ToList();
            return dbProducts.ToList();
        }

This option means I have to create magic strings and make sure that I handle the strings correctly. It has bugs already. Such as what if a product only has a name and not a version (version could be null or empty, who knows) or vice-versa? How would this affect my query string?

PredicateBuilder

Predicate Builder from the LinqKit library which is available as a NuGet package.

        private static List<Product> GetProductsByNameAndVersionPredicate(ActivationDbContext dbContext, IEnumerable<ProductRequest> products)
        {
            if (!products.Any())
            {
                return new List<Product>();
            }

            var predicate = PredicateBuilder.False<Product>();

            foreach (var prod in products)
            {
                var inner = PredicateBuilder.True<Product>();
                inner = inner.And(p => p.Name== prod.Name);
                inner = inner.And(p => p.Version == prod.Version);
                predicate = predicate.Or(inner);
            }
            var dbProducts = dbContext.Products.AsExpandable().Where(predicate).ToList();
            return dbProducts;            
        }

PredicateBuilder isn’t very intuitive. For starters, what is the different between these methods:

  • PredicateBuilder.True() – from what I understand this would be more appropriate and understandable as PredicateBuilder.And()
  • PredicateBuilder.False() – from what I understand this would be more appropriate and understandable as PredicateBuilder.Or()

Also, you have to remember to call AsExpandable() on the first call to a table in order to use it.

Conclusion

I am going to go with PredicateBuilder for now. It feels cleaner than rolling my own string query. But both solutions ultimately worked. That means that Entity Framework ultimately provided me a solution without an extra library. However, LinqKit saved me from magic strings. My only question is this: Why isn’t a predicate builder built into Entity Framework?


10+ Reasons why Java needs C#’s property syntax

Written in response, to all the Java developers who claim that Java doesn’t need C#’s property syntax.

The truth is most C# developers can’t quantify all the benefits to C# properties because there are so many. Most arguments I have seen from C# developers have been weak at best. These arguments fail to provide the Java development team a good enough reason to implement C#-like properties.

A proper analogy would be the Pyramids. You can’t point to one brick and say, “That brick makes the pyramid,” because every brick is small (relative the size of the who pyramid), but together, each brick is important to making the pyramid and altogether, they are one of the seven wonders of the world. Similarly, C# Properties provide many little features that together improve the language far more significantly than most can’t quantify.

Properties are quite awesome and provide some great features that Java cannot do now.

1. Easy refactoring when moving from a simple member variable to a property.

Properties using the same syntax as a member variable enable more than one feature, but we are going to talk about only this feature here to start.

public class MyObj
{
   public int Id;
   public String Name;
}

The program starts off with this because for years simplicity is all that is needed.

Note: A getter and a setter provide little to no benefit for a model object. If you aren’t encapsulating code on get and set, using a getter and setter gets in the way. Also, a using a getter and a setter is only a convention anyway, and not forced by the language. As such, getters and setters can’t be relied upon.

A year later, you find that you need to add some feature on name set. You change your class. In Java, you have to create getters and setters.

public class MyObj
{
   public int id;
   private String _Name;
   public String getName(){return _Name;}
   public void setName(string value) {
      _Name = value; 
      // More work here
   }
}

Now every single line of code that calls MyObj.Name is broken. However, with properties, this wouldn’t the case.

public class MyObj
{
   public int id;
   private String Name {
      get { return _Name; } 
      set {
         _Name = value;
         // more work here
      }
   }
}

None of my existing code broke using the property example. This sounds trivial but what if you are an API that hundreds of other tools consume and use. Now this a breaking change that has a massive impact on a business. This feature is huge.

Can Java change a class today from a member variable to a method without breaking an API? No, it cannot. Does this happen often? No, but when it does, it is a breaking change without properties, while with properties, it works without a second thought.

Important: This feature is also why so many hacks that suggest that Java adds a language construct that creates getProperty and setProperty methods are still broken. I’ve seen recommendations such as this where the suggestion is for the compiler and IntelliSense to just convert these to getProperty and setProperty:

public class MyObj
{
   @get @set String Name;
   public MyObj()
   {
      setName("Rhyous");// Default value
   }
}

That syntax doesn’t solve the problem. In order to solve this problem, the syntax still has to be this:

public class MyObj
{
   @get @set String Name;
   public MyObj()
   {
      Name = "Rhyous";// Default value
   }
}

However, even with this, the suggested syntax is not the right suggestion. While the above syntax works for auto properties, how would code be added to the get or set method? The above syntactical sugar, while better than what java has now, is lacking compared to C#’s implementation and would result in fewer features.

2. Replacing an object that uses member variables with an interface

This is similar to #1. You have an object and need to create a layer of abstraction and use an interface for that object. But the object uses only member variables? How can you create an interface for the following syntax in Java?

MyObj.Name

You can’t. So to add a layer of abstraction, you now have to first refactor code. Again, as mentioned in #1, moving from MyObj.Name to MyObj.getName() and MyObj.setName() is a breaking change, especially in a API. It can have a massive impact.

Now, before you argue that you would never need to add a layer of abstraction for a simple object, let me remind you that all objects are not simple. I agree, on a simple model object, abstraction wouldn’t be necessary. However, the problem is certainly with a method on an object that also has public members. But you need the whole object, not just the method. And interface with only methods won’t be enough.

MyObj.Name
MyObj.CallServer();

Well, we can’t use the CallServer() method as is in a Unit Test. We need to use and interface and dependency injection and good language architecture. We have to refactor some. But we with properties we don’t need to make a breaking change. Without properties, we do. We have to change from MyObj.Name to MyObj.getName().

3. Properties are different than member variables and methods and reflection reflects that.

C# has reflection. You can loop through all members, properties, and methods separately. In Java, you have something similar. But no way to get properties separate from other methods.

When you only want to loop through getters and setters in Java, how do you do that? Unless every getter and setter is perfectly named getProperty setProperty, you can’t.

Can you loop through only properties today in Java? Maybe. Java cannot guarantee this ability.

4. Sharing objects between Java and C#.

This feature goes beyond properties, but the lack of a Property syntax is the biggest barrier.

In order to do this today, the object has to be written in Java and C#. C# developers have to live with the more verbose getProperty() setPropert() syntax. For example: MyObj.Id++ is nice and clean but the equivalent in java is MyObj.setId(MyOjb.getId() + 1);

Some tools, such as Hibernate/NHibernate, would benefit greatly from this feature.

5. Properties let you write with cleaner and shorter syntax.

You save programming time. You save lines of code. Look below, as three lines become one.

private String _Name;
public String getName() { return _Name; };
private void setName(String name) { _Name = name; }

vs

public string Name { get; set; }

Java is not just a little more typing, it is a lot more typing. While you can argue that snippets or IDE code generator tools take care of this, I’d argue that C# has snippets and code generators, too. I can type prop tab in Visual Studio and have property pretty quickly so at best, snippets help Java almost catch up in development speed.

Also, let’s not forget that this is one single feature. I could write a whole article about all the ways that C#’s property syntax is cleaner and shorter than Java’s syntax.

How about adding one to an int?

MyObj.Id++;

vs in Java

MyObj.setId(MyObj.getId() + 1);

Again, a shorter and simpler syntax probably is a few dozen features as it will be used in so many different ways for so many different language benefits, not just one.

6. Most the property data is in one place.

This provides multiple features:

  • Easier to rename
  • Easier to copy and paste
  • Easier to identify as a property
public string Name { get; set; }


And in C#, a new syntax was added to make it so you no longer need to break this out to a backing field to set a default value.


public string Name { get; set; } = "Rhyous";

In java, there is no guarantee that properties are all together. I have seen code like this where the properties are not together in java.

public class MyObj 
{
  // Members
  private Integer _Id;
  private String _Name;

  // Getters
  public Integer getId() { return _Id; };
  public String getName() { return _Name; };

  // Setters
  public void setId(int id) { _Id = id; };
  public void setName(String name) { _Name = name; }
}

While this is fine, it makes renaming have to occur in 7 places in three lines that are separated and could be anywhere in the class file. (Count all the times Name or Id is used and it is 7 times.) In C#, it is once with an auto property and only four times with a property with a backing field. Of course, C# has the same problem once you use a backing field. The backing field isn’t always together with the property, that is only two items that can be separate, not three. In Java, keeping it together would have to be by convention. I use this convention in C# to keep backing fields together.

public string Name 
{ 
  get {return _Name; } 
  set {_Name = value;}
} private string _Name;

7. In Aspect-oriented programming (AOP), you can inject code into only Properties.

Using tools like AspectJ, can you do a pointcut only on properties in java? No, you cannot. You can do it on methods and filter it on methods that start with “get” or start with “set”, but we all know that get and set are only conventions and so any method that doesn’t follow convention won’t get the injected code (point cut in AspectJ).

8. Names of properties in a class match when doing Xml or Json serialization

Xml and Json serialization are very common. In C#, for newly developed code, there is no need to decorate the code with any C# Attributes as the names will serialize to JSON and Xml as is.

Imagine we want to get this following Xml from code.

<Person>
  <FirstName>Jared</FirstName>
  <LastName>Barneck</LastName>
</Person>

Or the following JSON.

{  
   "FirstName":"Jared",
   "LastName":"Barneck"
}

In C#, we create this poco class. Note: Poco = plain old class object.

public class Person
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

In Java, the class is not so simple. There is not an easy or a guaranteed to work way to do this without a lot more code and without annotations. Here is the same object in Java and what you would have to do to have both getters and setters, private members and serialize using Simple. See my article on Java’s Simple Xml Serialization library.

public class Person
{
   @FirstName
   private String _FirstName {get;set;}
   public String getFirstName() { return _FirstName; }
   public void setFirstName(string value) { _FirstName = value; }

   @LastName
   private String _LastName {get;set;}
   public String getLastName() { return _FirstName; }
   public void setLastName(string value) { _LastName = value; }
}

So in Java, names don’t match, requiring the developer to add a fourth line, an annotation, just to make this work. And this is new code, not legacy code.

So when Java developers tell me that they don’t need properties, they are saying that they prefer four lines of code per property over one line per property when doing serialization. There is no argument. Of the two serializable class syntaxes above, C# is the winner by a long way.

Now some serializers other than Simple are made by using magic strings, where they hope that the get and set convention is used and if it is used, everything after the get or set is serialized. So getPerson() will serialize to Person. Still, this leaves the names not matching, and it requires magic strings of “get” and “set” and a hope (or requirement) that convention was followed.

C# also allows for taking of property of one name and serializing it to another name.

public class Person
{
   [XmlElement("fName")]
   public string FirstName { get; set; }
   [XmlElement("lName")]
   public string LastName { get; set; }
}

Yes, both C# and Java have this feature. The difference is, this is only needed to accommodate existing or legacy systems and not needed for brand new code and systems.

9. Names of properties in a class match when using a database ORM (or CSV or Excel headers)

Ok, this is almost an identical reason as #8, but serialization for JSON and Xml is vastly different than using a database ORM. But the same issue applies. And really Json and Xml are separate reasons and used often, so I could have broken those out into two features instead of one. But let’s not forget that this also benefits Excel output and CSV output as well as database table output. So, yes, this is a big, huge separate feature than serialization.

If you have a table:

CREATE TABLE [dbo].[User]
(
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Username] [nvarchar](255) NULL,
	[CreateDate] [datetime] NOT NULL,
	[LastUpdated] [datetime] NULL,
	[CreatedBy] [int] NOT NULL,
	[LastUpdatedBy] [int] NULL
)

With properties, the naming scheme matches exactly.

public class User
{
   public int Id { get; set; }
   public string Username { get; set; }
   public DateTime CreateDate { get; set; }
   public DateTime LastUpdated { get; set; }
   public int CreatedBy { get; set; }
   public int LastUpdatedBy { get; set; }
}

That is simple and pretty with the six members making up six lines. What do we do in Java?

public class Person
{   
   @Column(name = "Id")
   private String _Id {get;set;}
   public String getFirstName() { return _Id ; }
   public void setFirstName() { _Id = value; }

   @Column(name = "FirstName")
   private String _FirstName {get;set;}
   public String getFirstName() { return _FirstName; }
   public void setFirstName(string value) { _FirstName = value; }
   
   // Sorry, it is too tedious to even type out the rest of the java example, 
   // I would have to create 16 more lines of code for remaining four columns. Ugh!!!
}

10. Making convention a language construct and not just a convention

Multiple times, we have mentioned that trying to require the get and set convention and having tools key of magic strings (starts with “get” or starts with “set”) is not really a good practice. The convention cannot be enforced. I will say that Java has done a tremendous job of trying to enforce this convention. But in the end, it is still a questionable practice that keys off of magic strings that are guaranteed to be there.

However, by implementing simpler properties, the convention goes away. Everyone who wants property features gets them. Tools no longer have to key off of magic strings.

One might argue that in C#, you can still write the Hava way, by writing out getProperty() setProperty(Property value) methods, and that using Properties instead of methods is also only a convention. But that is not true and is shortsighted. Properties are properties. Whereas java only has two object members: member variables and methods; C# has three: member variables, properties, and methods. Properties are not just methods. As noted earlier, properties are separate in reflection, in a different list in the object type information. There is a clear and distinct difference between properties and methods and member variables and how they behave in C#. Because of this, magic strings are not needed to execute code.

Don’t just copy, improve too

Salesforce added C# like properties to Apex, their originally java-based language. Only they enhanced the properties by not requiring a separate backing field. In C#, if you call a property from the get, you create an infinite loop. In Apex, any calls to the variable inside the get don’t create an infinite loop. They copy and improved. Java should do the same.

From the Apex documentation on properties:

Apex properties are based on their counterparts in C#, with the following differences:

  • Properties provide storage for values directly. You do not need to create supporting members for storing values.
  • It is possible to create automatic properties in Apex. For more information, see Using Automatic Properties.

This is outdated information as C# has had the second item, auto properties, for many years now. However, C# doesn’t have the first improvement yet. Good job Salesforce and Apex. They didn’t just see a good thing and copy it, they saw it, copied it, and improved on it.

Properties Provide More Features

The benefits are many and far more valuable than Java developers or even most C# developer realize. The Pyramid analogy is really accurate. There are so many little features of C# properties that the sum of the feature set is, like sum of bricks in a pyramid, huge.

I surely missed many such features that properties provide.

What features do properties give you that would benefit the Java language?
What shorter syntaxes do properties give you over Java’s getter setter syntax?


Microsoft announces it is acquiring FreeBSD for $300 Million

FreeBSD-BoxToday Microsoft® has announced that it has acquired FreeBSD®. FreeBSD is an open source operating system known for its very enterprise friendly license. Microsoft has recently embraced open source, moving .NET Core to GitHub, as well as announcing that a bash port that will run in Windows 10. However, this move was quite unexpected.

Microsoft is paying the FreeBSD Foundation approximately 300 Million for the FreeBSD brand, the open source operating system’s source repository, all forks, sub-brands (OpenBSD and NetBSD), websites, and communities.

Microsoft is in the process of negotiation full-time salaries for many of the developer volunteers.

Rumor has it that iXSystems may also be acquired either as part of this deal or as a separate deal. Interestingly enough, Microsoft is not paying for the source itself because that is already free for everyone.

In an interview with the Microsoft CEO Satya Nadella, he made the following comment:

“With Apple using so much of FreeBSD’s source in their OS X operating system, we felt owning part of the OS X operating system’s source code could really help our Office development team to write a better Office port of OS X.”

We further questioned Nadella on how this affected their recent relationship with Canonical, who ported bash to Windows 10 for Microsoft.

“Canonical is behind Ubuntu, who is moving away from the Linux Kernel. Canonical has recently embraced the idea of UbuntuBSD. With this aquisition, Cononcial and Microsoft are going work close together over the next few years.

There used to rumors that older Windows Operating Systems used some FreeBSD code, we commented to Nadella. He responded with this quip:

“Only older ones? Where do you think we get all our great ideas for our networking stack. I would expect a lot of integration between Windows and FreeBSD, especially on the networking stack.”

Is there anything that FreeBSD has that you want to pull in as soon as you can.

“Well, we are jealous that they have ZFS and Windows does not. Unfortunately, this aquisition doesn’t help bring ZFS to windows. Oracle has the copyright on ZFS. I guess we’ll have to acquire Oracle next.”

That last statement, Nadella laughed.

We were also able to contact the President of the FreeBSD foundation, Justin T. Gibbs and discuss with him the acquisition.

Has Microsoft made any exciting promises to the FreeBSD Foundation in light of this acquisition? Gibbs quipped:

“You mean besides promising to not lay us all off? No, in seriousness, Microsoft has committed to the FreeBSD copyright. They are looking for improvements in IPv6 that we have already implemented. We are looking to make .NET a first class citizen and make C# the primary development language for Web Services, Cloud Services, and Desktop apps written for FreeBSD.”

Does Mono or Xamarin have a big play in that? Gibbs responded:

“Yes, it does. In fact, expect to see FreeBSD added to the list of projects creates when you start a new Xamarin Forms project in Visual Studio. Soon, when you write an App, it will run universally on Windows devices, as well as Android, iOS, OS X, and FreeBSD.”

What does the future look like for FreeBSD under Microsoft’s reign?Microsoft announces it is acquiring FreeBSD

“The future looks promising for FreeBSD under Microsoft.”


Xamarin Free 4 All

We’ve been talking about the fact that Microsoft could make a huge move in enhancing the Windows App ecosystem simply by doing two things:

  1. Buy Xamarin
  2. Make it free

Recently, Microsoft bought Xamarin, checking off one of the two things they needed to do. I speculated on whether Microsoft would make Xamarin free for everyone. I believed that Microsoft would include Xamarin in different tiers for different levels of Visual Studio. But I noted that only by making it free for everyone, including the Visual Studio Community Edition Users, would Microsoft get the full community benefit.

They did it: Xamarin 4 all


Why you should avoid multiline string literals in C# with Git

Recently I started using Continuous Integration (CI) for my open source C# projects on GitHub. I found a http://www.AppVeyor.com would provide me this for free for my open source projects. I setup a few of my projects on the AppVeyor’s CI.

Unfortunately, one of my projects, Rhyous.EasyXml, failed four out of ten unit tests on the CI server. This made no sense. I had the code checked out on a work desktop and a laptop and all ten tests passed in both places.

I had a string that my EasyXml code generates. I had the expected Xml in the following multiline string literal.

        public string PrettyUtf8Xml =
@"<?xml version=""1.0"" encoding=""UTF-8""?>
<Person>
  <FirstName>John</FirstName>
  <MiddleName>Al Leon</MiddleName>
  <LastName>Doe</LastName>
</Person>";

The test results were not helpful because the string results in the test output were identical.

Starting test execution, please wait... 
Passed   TestMethodLinearize 
Failed   TestMethodPretty 
Error Message: 
   Assert.AreEqual failed. Expected:<<?xml version="1.0" encoding="UTF-8"?>
<Person>
  <FirstName>John</FirstName>
  <MiddleName>Al Leon</MiddleName>
  <LastName>Doe</LastName>
</Person>>. Actual:<<?xml version="1.0" encoding="UTF-8"?>
<Person>
  <FirstName>John</FirstName>
  <MiddleName>Al Leon</MiddleName>
  <LastName>Doe</LastName>
</Person>>.  
Stack Trace: 
   at Rhyous.EasyXml.Tests.XmlTests.TestMethodPretty() in C:\projects\easyxml\src\Unit Tests\Rhyous.EasyXml.Tests\XmlTests.cs:line 102

My first guess was that somehow my UTF-8 vs UTF-16 code wasn’t working and I set up to figure out how to compare the strings in a way that shows me the difference. I quickly found a wonderful string extension method ShouldEqualWithDiff for Unit Tests by Phil Haack. Phil Haack’s extension method is extremely helpful because it provides a verticle character by character output of the string if the comparison fails.

This provided the following output and pointed the finger of the problem directly at Git. See the highlighted lines 59 and 60 below that show that characters 38 and 39 fail to match up.

Failed   TestMethodPretty 
Error Message: 
   Assert.AreEqual failed. Expected:<<?xml version="1.0" encoding="UTF-8"?>
<Person>
  <FirstName>John</FirstName>
  <MiddleName>Al Leon</MiddleName>
  <LastName>Doe</LastName>
</Person>>. Actual:<<?xml version="1.0" encoding="UTF-8"?>
<Person>
  <FirstName>John</FirstName>
  <MiddleName>Al Leon</MiddleName>
  <LastName>Doe</LastName>
</Person>>.  
Stack Trace: 
   at Rhyous.EasyXml.Tests.StringExtensions.ShouldEqualWithDiff(String actualValue, String expectedValue, DiffStyle diffStyle, TextWriter output) in C:\projects\easyxml\src\Unit Tests\Rhyous.EasyXml.Tests\StringExtensions.cs:line 50
   at Rhyous.EasyXml.Tests.StringExtensions.ShouldEqualWithDiff(String actualValue, String expectedValue) in C:\projects\easyxml\src\Unit Tests\Rhyous.EasyXml.Tests\StringExtensions.cs:line 12
   at Rhyous.EasyXml.Tests.XmlTests.TestMethodPretty() in C:\projects\easyxml\src\Unit Tests\Rhyous.EasyXml.Tests\XmlTests.cs:line 102
Standard Output Messages: 
     Idx Actual    Expected
   -------------------------
     0   60   <    60   <  
     1   63   ?    63   ?  
     2   120  x    120  x  
     3   109  m    109  m  
     4   108  l    108  l  
     5   32   \u20;  32   \u20;
     6   118  v    118  v  
     7   101  e    101  e  
     8   114  r    114  r  
     9   115  s    115  s  
     10  105  i    105  i  
     11  111  o    111  o  
     12  110  n    110  n  
     13  61   =    61   =  
     14  34   "    34   "  
     15  49   1    49   1  
     16  46   .    46   .  
     17  48   0    48   0  
     18  34   "    34   "  
     19  32   \u20;  32   \u20;
     20  101  e    101  e  
     21  110  n    110  n  
     22  99   c    99   c  
     23  111  o    111  o  
     24  100  d    100  d  
     25  105  i    105  i  
     26  110  n    110  n  
     27  103  g    103  g  
     28  61   =    61   =  
     29  34   "    34   "  
     30  85   U    85   U  
     31  84   T    84   T  
     32  70   F    70   F  
     33  45   -    45   -  
     34  56   8    56   8  
     35  34   "    34   "  
     36  63   ?    63   ?  
     37  62   >    62   >  
   * 38  13   \r   10   \n 
   * 39  10   \n   60   <  
   * 40  60   <    80   P  
   * 41  80   P    101  e  
   * 42  101  e    114  r  
   * 43  114  r    115  s  
   * 44  115  s    111  o  
   * 45  111  o    110  n  
   * 46  110  n    62   >  
   * 47  62   >    10   \n 
   * 48  13   \r   32   \u20;
   * 49  10   \n   32   \u20;
   * 50  32   \u20;  60   <  
   * 51  32   \u20;  70   F  
   * 52  60   <    105  i  
   * 53  70   F    114  r  
   * 54  105  i    115  s  
   * 55  114  r    116  t  
   * 56  115  s    78   N  
   * 57  116  t    97   a  
   * 58  78   N    109  m  
   * 59  97   a    101  e  
   * 60  109  m    62   >  
   * 61  101  e    74   J  
   * 62  62   >    111  o  
   
   . . .

The cause is carriage returns. Why would AppVeyor’s tests have only \n while running the tests on any of my machines has \r\n? Yes, Git is the reason. Git normalizes carriage returns when you check in and check out your code. On a Windows box, \r\n is converted to \n on check-in. On checkout \n is converted to \r\n. When AppVeyor checks out my code, the conversion from \n to \r\n doesn’t occur.

So my options to fix this are these:

  1. Change Git to:

    1. use \r\n and not change line endings at all
    2. Change my code to be a single line string

    I chose the second option. I did not want to mess with the Git settings. Different people could have difference Git settings and if anyone else forked my code, and ran the tests, I wanted them to work. So I changed my code. Now the string literal is on one line and the new lines are indicated with \r\n.

            public string PrettyUtf8Xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Person>\r\n  <FirstName>John</FirstName>\r\n  <MiddleName>Al Leon</MiddleName>\r\n  <LastName>Doe</LastName>\r\n</Person>";
    

    And now my Continuous Integration on AppVeyor is building and passing tests.


A simple C# factory class

I have a project that is pretty small. Despite the small size, it is well-designed, using multiple layers, and interfaces, dependency injection, and unit tests. I need to create a production object at runtime and a mocked object at test time. I could easily use an IOC container. The problem with that is that most IOC containers (Autoface, Castle Windsor, Unity, etc.) are larger than my entire project. While I am a proponent of using IOC containers in large projects, I’m not a big proponent of using them in very small projects.

To make my code more unit testable, I am using a project called SystemWrapper that wraps standard system calls in an Interface and a Wrapper. Again, because my project is small, I didn’t bring in the SystemInterfaces and SystemWrapper dlls. This wrapper includes an ISmtpClient and an SmtpClientWrap object and I only brought in those two class files. The business logic uses the interface, ISmtpWrapper. This allows for me to unit test it by injecting a mock ISmtpClient.

I needed a simple factory that creates a new SmptClient in production runtime but allows for my unit test to create and use a mock ISmptClient during unit test time.

Here is what my factory should do:

Production

  1. Create a new SmptClientWrap object (which wraps an System.Net.SmptClient oject).
  2. Use setting from the app.config or web.config for the mail server, domain, user, and password.

Unit Test

  1. Create an mock of ISmptClient (using Moq).

Here is the simple factory class that I wrote:

using System.Configuration;
using System.Net;
using SystemInterface.Net.Mail;
using SystemWrapper.Net.Mail;

namespace Rhyous.System.Factory
{
    public class SmtpClientFactory
    {
        public ISmtpClient GetNewSmtpClient()
        {
            return CreateCredentialsMethod();
        }

        public delegate ISmtpClient CreateCredentialsDelegate();

        public CreateCredentialsDelegate CreateCredentialsMethod = () => new SmtpClientWrap(ConfigurationManager.AppSettings["SmtpServer"])
        {
            Credentials = new NetworkCredential
            {
                Domain = ConfigurationManager.AppSettings["SmtpDomain"],
                UserName = ConfigurationManager.AppSettings["SmtpUser"],
                Password = ConfigurationManager.AppSettings["SmtpPassword"]
            }
        };
    }
}

In the above class, the GetNewSmptClient returns an ISmtpClient. I use a delegate to create a concrete ISmtpClient called SmtpClienWrap. The default delegate implementation gets the data from the app.config or web.config.

Now I can inject a concrete ISmtpClient into my code:

    using (var smtpClient = SmtpClientFactory.GetNewSmtpClient())
    {
        var mailer = new Mailer(smtpClient);
    }

Note: I could make SmptClientFactory static or make it a singleton. I’m thinking about both.

Now in a test, I am able to create a mock ISmtpClient. Here is an example.

        [TestMethod]
        public void ReplacingTheCreateCredentialsDelegateWorks()
        {
            var factory = new SmtpClientFactory();
            bool _wasCalled = false;
            factory.CreateCredentialsMethod = () =>
            {
                _wasCalled = true;
                return new Mock<ISmtpClient>().Object;
            };
            var client = factory.GetNewSmtpClient();
            Assert.IsTrue(_wasCalled);
        }

The one problem with my factory is that it is pretty specific to one class. It might be interesting to make it more generic.

using System;

namespace Rhyous.Factory
{
    public class ObjectFactory<TInterface, TObject>
        where TInterface : class
        where TObject : TInterface, new()
    {
        public TInterface GetNewObject()
        {
            if (!typeof(TInterface).IsInterface)
            {
                throw new Exception("The first generic, TInterface, must be an interface.");
            }
            return CreateObjectMethod();
        }

        public delegate TInterface CreateObjectDelegate();

        public virtual CreateObjectDelegate CreateObjectMethod
        {
            get
            {
                return _CreateObjectMethod ?? (_CreateObjectMethod = () => Activator.CreateInstance<TObject>());
            }
            set { _CreateObjectMethod = value; }
        }
        public CreateObjectDelegate _CreateObjectMethod;
    }
}

Now I move my delegate (which is creation method of the factory) upstream to where I instantiate the factory.

    var smtpClientFactory = smtpClientFactory = new ObjectFactory<ISmtpClient, SmtpClientWrap>
                    {
                        CreateObjectMethod = () => new SmtpClientWrap(ConfigurationManager.AppSettings["SmtpServer"])
                        {
                            Credentials = new NetworkCredential
                            {
                                Domain = ConfigurationManager.AppSettings["SmtpDomain"],
                                UserName = ConfigurationManager.AppSettings["SmtpUser"],
                                Password = ConfigurationManager.AppSettings["SmtpPassword"]
                            }
                        }
                    };

    // Then later use the factory...

    using (var smtpClient = smtpClientFactory.GetNewSmtpClient())
    {
        var mailer = new Mailer(smtpClient);
    }

Anyway, have fun with this mini-factory. It might be useful on small projects where you don’t want an entire IOC container.


The oft forgotten Middle Trim

Two Most Popular Ways to Trim

It has become ubiquitous to trim whitespace from data. Data should almost never have whitespace at the front or at the end. This fact is nearly ubiquitous throughout the industry.

  • Front Trim (also called left trim) = Remove leading whitespace, whitespace (space, tab, new line, carriage return) at the front of text.
  • Back Trim (also called left trim) = Remove trailing whitespace, whitespace (space, tab, new line, carriage return) from the back of data. Trailing whitespace.

What does this mean? Look at the following data example:

"  White space at front"      <-- space
"	White space at front" <-- tab
"
White space at front"         <-- new line or carriage return
"White space at back   "      <-- space
"White space at back	"     <-- tab
"White space at back
"                             <-- new line or carriage return

When extra white space is added to the front or back of data, it should almost always be trimmed.

The Third Way to Trim – Middle Trim

There is a third type of trimming that should be done for many fields. It is not as popular and many developers forget about it. (Marked in green below.)

  • Front Trim (also called left trim) = Remove whitespace (space, tab, new line, carriage return) from the front of data.
  • Back Trim (also called right trim) = Remove whitespace (space, tab, new line, carriage return) from the back of data.
  • Middle Trim (also called center trim) = Remove extra whitespace (space, tab, new line, carriage return) from between words of data.

Note: Extra whitespace could mean different things depending on the field. In this post, it means more than one space. However, if we were dealing with names of objects in code that should not have any middle spaces at all, then even one middle space could be considered an extra space.

Perhaps “Middle Trim” is not something you have heard of before. Front and back trim involves only removing characters if they exist. Middle Trim involves either removing or replacing characters if they exist. Because of this, some might argue that Middle Trim is an incorrect phrase. From a certain point of view, I would agree. However, to properly link the task to front trim and back trim, the phrase Middle Trim makes a lot of sense.

"Extra     white space in middle"      <-- space
"Extra 	white space in middle"          <-- tab
"Extra
white space in middle"         <-- new line or carriage return

This one actually takes some thought. Because it doesn’t apply to every field as often as front trim and back trim do. However, for many fields, middle trim is just as valid.

  • Address Lines (When there is one field per line)
  • City
  • Country
  • Name (Pretty much any type of name)
    • Account
    • Business
    • Contact
    • Company
    • Course
    • Customer
    • First
    • Last
    • Middle
    • Part
    • Partner
    • Product
    • School
    • Spouse
    • Street
    • User
  • Order Identifiers
  • State
  • etc…

Names should not have extra whitespace at the front, end, or middle. State or Country names should never have extra whitespace at the front, middle, or end. Many types of input should be cleaned of extra whitespace in the front, middle, or end.

"Awesome     Company LLC"  <-- space
"Washtington	D.C."      <-- tab
"United States of
America"                   <-- new line or carriage return

All of the above are wrong. I could quote First Normal Form to you, but really common sense should be enough. These spaces make the data wrong.

Now, each field may be different. You may not want middle trim if your field is a blob of text, that has paragraphs. In that case, you certainly want to leave carriage returns.

Implementing Middle Trim in C#

Middle trim isn’t exactly easy to implement. Some languages have features, such as Regex, which make it easy. Others do not.

Why isn’t Middle Trim extremely common and more easily implemented? Perhaps middle trim is forgotten because there isn’t a clear method for it like there is with String.Trim() and so it is often left out?

Many languages, like C#, make front and back trimming easy. In C#, you can simply call String.Trim() and it will trim whitespace from the front and back. However, it doesn’t clean up extra whitespace in the middle.

Doing all three trims in C# is most easily done with Regex and an extension method.

public static class StringExtensions
{
    public static string TrimAll(this string value)
    {
        var newstring = value;
        newstring = myString.Trim(); // This removes extra whitespace from the front and the back.
        newstring = Regex.Replace(LastName, @"\s+", " "); // Replaces all whitespace with a single space
    }
}

If you want to avoid regex, you could roll your own like this:

public static class StringExtensions
{
    public static string TrimAll(this string value)
    {
        var trimmedValue = new StringBuilder();
        char previousChar = (char)0;
        foreach (char c in value)
        {
            if (char.IsWhiteSpace(c))
            {
                previousChar = c;
                continue;
            }
            if (char.IsWhiteSpace(previousChar) && trimmedValue.Length > 0)
            {
                trimmedValue.Append(' ');
            }
            trimmedValue.Append(c);
            previousChar = c;
        }
        return trimmedValue.ToString();
    }
}

You would use either method the same way.

  var newstring = " This string     has extra whitespace in the      front, middle and the end.   "
  newstring = nestring.TrimAll();

Implementing Middle Trim in MSSQL

MSSQL also has LTRIM (left trim) and RTRIM (right trim), but middle trim doesn’t exist. Middle Trim is even harder to write in MSSQL because there is no Regex. So you have to replace whitespaces characters with spaces, then remove multiple spaces.

Here is what it looks like to add a name to a person and to do all three trims: front, back, middle. Wow! It is ugly.

INSERT INTO PERSON  (NAME) VALUES (
	REPLACE(
		REPLACE (
			REPLACE(
				REPLACE(
					REPLACE(
						REPLACE(
							LTRIM(RTRIM(@str))
							, char(9), ' '
						),  char(10), ' '
					),  char(13), ' '
				),'  ',' '+CHAR(7)
			), CHAR(7)+' ',''
		), CHAR(7),''
	)
)

This does right trim, left trim. Then it replaces tabs, new line, and carriage returns with spaces. Then it uses the bell character (because bell is basically never used) to replace any double spaces, char(32)+Char(32), with space bell, char(32)+char(7). Then it replaces any instance of char(7)+char(32) with ”, an empty string. Then that might leave a few space bell sequences, so we only need one more replace of bell, char(7), with ”, an empty string.

How to know which type of trimming you need?

This is very simple. Just ask questions:

  • Front trim – Will extra whitespace at the front ever be valid?
  • Back trim – Will extra whitespace at the back ever be valid?
  • Middle trim – Will extra whitespace in the middle ever be valid? Are middle spaces allowed? If so, should they always be a single space?

If the answer to any of those questions is “no,” then you need to do that type of trim. However, it is clear that Middle Trim has more questions as it is more complex.


Victoria’s Secret Swim Special not available to Ad Blockers

If you are like many of internet users, you probably use an Ad Blocker. If so you have unwitting joined what I call the Ad Blocking Arms Race. This arms race is extremely interesting. Unlike real war, this arms race doesn’t include a body count, making it extremely interesting and fun to watch. I keep an eye out to see how this arms race progresses.

Ad Blocking has caused a significant hit to the bottom line of many Ad-sponsored websites. It is well-known that Forbes.com has taken a stand against Ad Blockers. Their actions have made a clear statement:

“Our content is not free, it is paid for and sponsored by ads. Block those ad, and you will be blocked from our content.”

I previously discussed 6 Reasons Why Ad Blocking Is A Short Term Fad, even though I not against ad blocking. In this article, I discuss how soon, almost no site will work unless you disable your Ad Blocker. If that happens, Ad Blockers will be rendered quite ineffective.

Today, CBS joined Forbes.com in the Ad Blocking Arms Race, taking a stand and fighting back against Ad Blockers. If you try to watch free CBS content, then Ads are required. Those who go to CBS.com to catch up on shows that they might want to catch up on, such as Victoria’s Secret Swim Special, will receive the following denial:

“This Video is unavailable because we were unable to load a message from our sponsors. If you are using ad blocking software please disable it and reload the page.”

CBS is giving a similar message as Forbes.com. Here is what it looks like when you go to their web site and try to watch the Victoria’s Secret Swim Special with an Ad Blocker enabled. This is not subtle and makes it clear that the Ad Blocker is not acceptable on their site. Instead of feeling like an ad blocker is effective, this makes it feel like the Ad Blocker simple got in the way.

CBS Blocks Victoria's Secret Swim Special to Ad Blockers

 

Turning off Ad Blocking is easy enough, but that is not the interesting part of this. The interesting part of this is the effect this has on the Ad Blocker Arms Race. As a techie myself, I probably won’t stop using an Ad Blocker, but many average users will soon be annoyed enough by these constant denial of content to Ad Blockers, that they may stop using Ad Blockers altogether. It may take time as they may not uninstall an existing Ad Blocker, but next time a user switches to a new device, might they simply forgo installing an Ad Blocker. If that happens, Advertisers will soon win this arms race.

However, in an Arms Race, there are two sides who continually escalate. Content providers, who are sponsored by ads, are just starting to fight back.  These are their first punches.

Besides the ubiquitous feature to disable the Ad Blocker for a certain site, what is going to be the Ad Blocker’s response? Are they going to try to disguise themselves somehow? Technically, that seems difficult.

With so many sites being funded primarily by ads, are Ad Blockers really good for the Internet? There are multiple points of view. On one side, Ad Blockers are having a small but positive effect on the quality and legitimacy of ads. However, already some sites are shutting down, or if not shutting down, going inactive, do to Ad Blockers. Others are simply blocking users who have Ad Blocking software enabled.

All I can say is, this: The battle is on, and this arms race is fun to watch.