r/shittyprogramming • u/form_d_k • Feb 26 '18
C#: (An Introduction To) The Power of ExpandoObject
In our previous post, we conclusively demonstrated the advantages of using dynamic over var. In this post, we'll expand on this usage with ExpandoObject.
A Problem Case
Consider the following code. Can you spot the error?
/tpublic class UserLogin
/t{
/t/t// The default company user name.
/t/tinternal string UserName { get; set; } = "admin";
/t/t// The default company password.
/t/tinternal string Password { get; set; } = @"123!@#abcABC";
/t/t// #TODO Implementation.
/t/t//internal SecuritySettings Security { get; set; } = SecuritySettings.InternalAccess();
/t/t// ... More code.
/t}
/t// Elsewhere:
/t/t// #TODO: Add security ifs
/t/tpublic static AccountDataBase GetDataBase(UserLogin login)
/t/t{
/t/t/treturn Databases.GetFromAccountInfo(login.UserName, login.Password, login.Security);
/t/t}
Do you see it? The Security property is commented out, yet it is being passed into the GetFromAccountInfo method.
At our development studio, we ran into this very problem. This simple mistake brought prod down, grinding development to a halt. With a milestone due in mere days and a state contract on the line, this proved disastrous.
During lengthy post-mortems, we decided mandatory code reviews would avoid such mistakes from ever occurring again in the future. This solution came too late for the engineer at fault, who was reassigned to frontend development with reduced pay. But it led to some interesting discussion: why doesn't the above code work? Shouldn't C# know that a non-declared property being assigned to should exist? After all, we are assigning to it.
Where ExpandoObject Plays a Role
Thankfully, C# provides an elegant solution: ExpandoObject. ExpandoObject, named so because it expands object, was introduced in the .NET 4.0 SDK. It works with the the CLR's successor, the DLR, and allows you to assign to properties that don't exist until assigned to.
Let's convert our problem code to use ExpandoObject:
public AccountDataBase GetDataBase(WebClient webClient)
{
dynamic login = new ExpandoObject(); // Var begone!
login.UserName = webClient.UserNameField;
login.Password = webClient.PasswordField;
// #TODO Implementation.
if ((WebClient.InternalIps.Contains(webClient.Connection.IpAddress) == true) && (login.Password == Databases.DefaultPassword))
{
//login.Security = SecuritySettings.InternalAccess();
}
else
{
//login.Security = SecuritySettings.PublicAccess();
}
return Databases.GetFromAccountInfo(login);
}
Notice how login is now dynamic and created at the point of use. Even if security features aren't yet implemented, we would still able to build. Had our subcontractor relied on the above code, we would have avoided the headache of invoicing our client for additional work.
The Benefits of ExpandoObject
You likely have years of dev experience, so you'll recognize the powerful nature of ExpandoObject from the get go. But what about less experienced developers? How can they convince their lead to switch over to this exotic construct?
Here's a taste:
ExpandoObjectworks hand-in-hand withdynamic(point team to previous Reddit post).Understandabler code.
- Fewer source files.
- All method parameters can be
dynamic. - No more bloat from explicit typecasting.
- Fewer keywords required.
Smaller memory footprint: only the properties needed will be put on the heap stack.
Probably speed.
What additional benefits can YOU think of?
In our next post, C#: The Paradigm Shift, we'll discuss using ExpandoObject in a radical way: to replace the usage of almost all types with a single one: ExpandoObject.
p.s. Thanks for taking the time to enjoy this post. Please consider supporting our small development studio via Patron, or PM for more direct ways to invest.
5
u/Celdron Feb 27 '18
Oh I'm looking forward to the next article. Finally I can stop doing this:
2
u/nemec Feb 27 '18
Coming from VB I prefer my objects to be case insensitive.
9
u/[deleted] Feb 27 '18
Holy shit I thought I was in /r/csharp for a minute
2
u/[deleted] Feb 28 '18
I thought you made 'ExpandoObject' up...
2
u/Tyler11223344 Mar 27 '18
I need moar
0
u/[deleted] Feb 27 '18
[deleted] — view deleted comment