Introduction
As the date of today, we have seen various evolution of C# in many ways, In this article we are going to discuss on the new features release in C#7.0 called Named Tuples, Throughout our experience in making use of the normal tuples, we usually instantiate an instance of a tuple and then we may get the assigned values with something like 'Item1' , 'Item2'. Now, they have come up with some 'sugar' by enabling us to assign it a name, I personally think that this features really help us as a developer to understand our code more easily. This somehow portray that C# is becoming more dynamic towards its programming pattern. Note than the title 'Object On The Fly' is somehow a sugar of my own, and not related to any programming technique or any design patterns. But i am pretty sure that we might going to encounter it a lot in the future.
Consideration
The code involved in this article is based on assumptions, It was proposed for the intention to show its functions/features based on the article title. For production/release we might need to tweak it to meet the company policy, best practice and industry standards. The code snippet is free to use anywhere.
Jump Straight Right In
Sometimes, we require a return Type that is unique without its Class declaration, In the example code below, we are trying to make use Named Tuples as a return type of a single method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//(string InvoiceID , string Owner, List<Tuple<string,string>> MemberRoles , DateTime CreatedDate) | |
// As The Return Named Tuple | |
public static (string InvoiceID , string Owner, List<Tuple<string,string>> MemberRoles , DateTime CreatedDate) GetInvoiceUsers(string InvoiceID) | |
{ | |
string Owner = "Adzha"; | |
List<Tuple<string, string>> MemberRoles = new List<Tuple<string, string>>(); | |
MemberRoles.Add(new Tuple<string, string>("Jack", "AccountTeam")); | |
MemberRoles.Add(new Tuple<string, string>("Rick", "Manager")); | |
MemberRoles.Add(new Tuple<string, string>("Sandra", "AuditTeam")); | |
MemberRoles.Add(new Tuple<string, string>("Jimmy", "OperationTeam")); | |
DateTime CratedDate = DateTime.Now.AddDays(-10); | |
return (InvoiceID, Owner, MemberRoles, CratedDate); | |
} |
From the code above we may see that the return type of the method is slightly different from normal Tuple This might as well can be accomplished using something like this Tuple<string,string,List<Tuple<string, string>>,DateTime> But now we are using Named Tuple.
Making Use Of It
Assuming we wanted to check if a specific user are allowed to view a specific invoice, we may make use the method above like below
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example Of Named Tuple Usage | |
public static bool CheckUserAuthority(string UserName , string InvoiceID) | |
{ | |
// Here we are using the new Named Tuple | |
foreach(var user in GetInvoiceUsers(InvoiceID).MemberRoles) | |
{ | |
// Here we are using the Ordinary Tuple | |
if (user.Item1 == UserName) | |
return true; | |
} | |
return false; | |
} |
The Downside
Assuming we wanted to serialize the return object across our system boundaries, As the date of today, we are still unable to do so. For example returning the data as Json. We will still treat it as a normal Tuple. For Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore.Mvc; | |
using Newtonsoft.Json; | |
namespace TupleAPI.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class SampleController : ControllerBase | |
{ | |
[HttpGet] | |
public string Get() | |
{ | |
return JsonConvert.SerializeObject(SampleNamedTuple.GetInvoiceUsers(Guid.NewGuid().ToString())); | |
} | |
} | |
} |
Current Discussion
There is a lot of active discussion on this right now, Here is one of them taken from GitHub
Conclusion
From the article, we may conclude that using Named Tuple somehow will help us in code maintenance by improving code readable. To understand more, visit Microsoft official website Here. As for the date of this writing, there is still no support yet to serialize it throughout our system boundaries.
Published on : 7-Jan-2018
Ref no : DDN-WPUB-000215
About Author

Comments
Post a Comment