Monday, March 26, 2018

How to export CSVs with ASP.NET and C#

Exporting CSV is a very common requirement. Today, let's review how to do it elegantly using ASP.NET and C#.
Photo by Mika Baumeister on Unsplash
On a previous post we discussed how to import CSVs using .NET and C#. Today we continue discussing CSVs, specifically, how to generate and export CSVs directly from the database to the users from an ASP.NET web application using C#.

Exporting CSVs from a the database is a very common requirement. Since building CSV files seems simple, it's common to see developers building their own. However, before thinking about writing your own parser, remember that there are important considerations including performance, security, and details of the CSV format itself that most likely your code will not comply with. With all that said, it's best practice to use an existing tool which, for us .NET developers is the CsvHelper Nuget Package.

Overall, this exercise can be broken in 4 steps:
  1. Add a reference to the CsvHelper nuget package in your ASP.NET project (which you saw how on the previous post);
  2. Fetch the records form the database;
  3. Build your CSV in-memory;
  4. Format and build your response setting an arbitrary file name and returning the request tot the user;

Source Code

All of the above tasks are listed on the code below. Please, read on. We will explain the details below the markup.

Reviewing the Code

Now let's review exactly the previous code. The most important bits are on:
  • Lines 12-16: on this example, we used RavenDB as our database. Lines 12-16 describe how to  load the data from the database using an index;
  • Line 15: I'm converting between my db entity (BatchRow) to my view model (CsvLine);
  • Line 26: CSVHelper is doing the magic for us. It's better when it's simple!
  • Line 31: I'm formatting the file name;
  • Line 33: Writing the the response stream;
  • Line 34: closing the response stream;

Refactoring

As I have a constant desire to make my code as clean as possible, I usually review my code before committing and pushing. If there is bloat there, I usually refactor it, making use of my tests to help me refactor with confidence.
If the code is bloated, the performance is below acceptable or if you are not satisfied with the solution, consider refactoring or changing how you architected your solution in the first place. Optimize for correctness first, then for performance.

Final Thoughts

This example covers a simple scenario of loading, building and exporting the data to the users. It should work well for serving up to 5 Mb of data. However, if your required to export a lot of data, you should consider other options such as streaming the data to the users, processing it in the backend (or in a WebJob) and emailing it back to your users. Another option could even be pre-generating the report and just serve it on the click of the download button. Use your creativity!

Also consider that depending on your use cases, this code is yes, too simplistic. But that's the objective. We should never implement what we don't need, yet. Hope it helps!

References

See Also

Monday, March 19, 2018

How to Import CSVs with .NET Core and C#

Accessing CSV is a common tasks in development. Let's review a simple yet elegant solution on how to do it using C#.
One common requirement is to deal with external files, especially CSV files. Because in theory CSV files are simple, developers tend to rush and implement their own parsers. However, it takes a lot of work (and uncountable bugs) to write a good CSV reader/writer. The effort is simply not worth it: you shouldn't repeat yourself. Luckly, Josh Close has built the excellent CSVHelper for us. Let's test it out.

Importing CSV files with C#

Let's review then a very simple use case to demo how can we import CSVs, parse them to a strongly typed model using CSVHelper. For this demo, I will be using Visual Studio 2017 and the following CSV file:

Step 1: Create a new .NET Core project

So, go and create a new project:

Step 2: Reference the CsvHelper Nupkg to your solution

Next, add a nuget reference to the CsvHelper nupkg to your project and accept the terms.

Step 3: Add the CsvLine model class

Now with my project referencing the CsvHelper package, I still need two more classes. The first one,
I called it CsvLine. This class represents each of the imported records in the csv, apart from the header.

This class name is very anemic. In a production code I wouldn't name it if I needed to use that class elsewhere but for this post, it helps simplifying the explanation.
The code for this class looks like:

Step 4: Add the CsvImportedDemo class

Next, add a class to your project (Project -> Add -> New Item) [or just Alt-Shift-C when selecting your project in Visual Studio's Solution explorer] called CsvImporterDemo. This class will act as the facade of my project containing the logic to orchestrate the execution flow.

CsvImporterDemo will contain a Import method that basically will:
  • open the file;
  • feed csv reader;
  • load all recs on the file with the csv reader;
  • print some of the records
The code for this class looks like:

Debugging the .NET Core console app

Because I plan to specify the imported file from the command line, I need to provide args to my project before running. This is done on project properties window -> Debug -> add "Application arguments". Ex:
So that when I run my code in debug mode, I get the params that the user is supposed to pass in the command line:
All set! Now, before we run it and see the results, let's add a nice tip.

Tip: Override ToString and use string interpolation

A nice caveat to add for this extremely simple project is to override the ToString method on the CsvLine class. This allows us to format its return when calling the ToString method. A bonus here is to use string interpolation to simplify even more our logic:

Running on Windows

Now, let's run it. From within Visual Studio, just pressing F5 runs in debug mode for me. Here's the output in windows:

We also can run it directly from the terminal (but don't forget to pass the filename) with:
dotnet CsvImporter.dll --file export.csv

Conclusion

We showed some interesting things on this demo:
  • how to import CSV files the simple way. For more complex scenarios, check this link;
  • publishing .NET Core apps;
  • some programming techniques such as string interpolation, method overrides, programming concepts and some design patterns were introduced;
  • how to run .NET Core apps from the console;
  • run to run run a .NET Core app in Linux.

Source Code

As always, the source for this project on my GitHub.

See Also

Monday, March 12, 2018

Package Management in .NET Core

When developing .NET applications, developers use Nuget, Visual Studio and PowerShell. But what about .NET Core?
Photo by Christian Wiediger on Unsplash
In development, it's common to import and reuse routines from external libraries. That code is often code is bundled into packages/libraries that could contain compiled code (as DLLs) along with other content needed in the projects that consume these packages (CSS, images, JavaScript, etc). If you use .NET you have already dealt with NuGet one way or another. But how does that work in .NET Core?

NuGet and .NET Core

NuGet remains strong in .Net Core as it's the default tool to download packages in the .NET world. Nuget.org is where most of these packages (free or not) are hosted and can be downloaded. If you have Visual Studio installed in your machine, probably already have nuget.exe installed since  VS only wraps NuGet when managing the packages of your projects. With .NET Core however, package maintenance is done using the new .NET Core CLI.

Managing NuGet Packages

In order to build, push, modify and manage nuget packages, we need to use the nuget.exe tool. If you're interested in managing your own packages, please take a look at this documentation.

Installing nuget.exe

As nuget.exe itself is not included with any version of Visual Studio, if you want to use it, you can then go and download it on nuget.org/downloads. Please download the latest.
Don't forget to set it on your %path% so it's accessible from the command prompt everywhere.

Nuget Package Exlorer

I also recommend installing NuGet Package Explorer for a friendly nuget.exe user interface. It will help you understand your package, its dependencies and metadata. NuGet Package Explorer looks like this:

The Global Packages Folder

After NuGet 3, there is now a packages folder that is shared for all projects that you work with. Packages are downloaded and stored in the %userprofile%\.nuget\packages folder so you will only have one copy of that package on your machine. The same package can be reused for new projects saving time and band, especially on build servers.

The following command line lists you all packages installed in your system, for your user:
nuget locals global-packages -list

Adding nuget packages to your .Net Core project

Adding packages a your solution can be done two ways: by using Visual studio or from the command line.

Adding packages using the command line:

I'm a strong supporter of using the CLI. So let's start with it first. In your project folder, run the following command:
dotnet add package <package-name>
As an example, this is how I added jQuery to an old JS project:

Adding packages using Visual Studio:

Adding with VS is simply right-clicking the web project name -> Manage NuGet packages:

Restoring packages

To finish, you can also to restore packages using the command line. In your solution folder, run:
dotnet restore

Conclusion

On this post we reviewed how to manage NuGet packages using both Visual Studio and the new .NET Core CLI. Since most developers are used to Visual Studio and the experience won't change much between .NET Framework and .NET Core projects, on this post I empathized how to use the CLI. Using the command line it a great way to learn your tools, write scripts, automate and manage your packages more quickly.

References

See Also

    Monday, March 5, 2018

    Why use .NET Core

    Developers have the .NET Framework and .NET Core to choose from. What are the differences and why should you use .NET Core?
    Photo by Anne NygÃ¥rd on Unsplash
    Developers working with .NET since the beginning know what a big and important change .NET was. For those who worked with Classic ASP, Perl, Com+ and CGIs, that was a real change! But as you can expect, .NET back then wasn't what it is today. Those who worked with NET 1.1 for example may remember that back then, the framework didn't have Nullable types so developers had to implement our own INullable<T> class.

    Using a new framework only makes sense if the framework accelerates, simplify the software development process and introduces access to new features. Using the latest tools and features may be awesome but can also be a big risk!

    .NET Framework

    But .NET 1.1 wasn't ready yet Everything changed when .NET 2.0 was released. With it, the community got access to Nullable types (yay!), Partial classes, iterators and anonymous methods and a flourishing ecosystem making it a serious and competent framework for enterprise software. As the .NET framework grew in popularity, Microsoft constantly improved it release after release. For example, my favourite features were:
    As you can see, there was a significant decrease in new features after .NET 4.5. Well, if you look at the release dates, you'll see that both .NET 4.6 and .NET Core were announced in 2014. Why? Let's see next.
    If you want to read more about the .Net Framework's history, please click here.

    .NET Core

    Microsoft introduced .NET Core to the world in the end of 2014. According to them:
    .NET Core is a cross-platform development framework that can be used in servers, workstations, mobile devices, cloud and embedded/IoT devices.
    As you might expect .NET Core 1.0 wasn't ready yet for production. However, it indeed showed a huge potential since for the first time, .NET developers were able to build, run and deploy their apps on Linux and Mac. Plus, with the Docker revolution, they'd be able to work on containers, Kubernetes and microservices as the rest of the world did.

    But the .NET Core only grew up on version 2.1. Released 2018, .NET Core 2.1 is not only an LTS release but it brought massive API compatibility with .NET Framework:
    • Double the number of APIs from 13k in .NET Standard 1.6 to 32k in .NET Standard 2.0
    • Added support for .NET Standard 2.0.
    • Added support for referencing .NET Framework libraries and NuGet packages.
    • Made available the Linux .NET Core SDK and Runtime builds for most Linux distributions.
    .Net Standard 2.0 - Source

    Why use .NET Core

    So, with that short introduction, let's understand why we should use .NET Core.

    Cross-platform

    Runs on Windows, Mac and Linux;

    Container-friendly

    That's a huge win for .NET Cover over .NET Framework. Because .NET Core runs natively on Linux, it

    Powerful CLI

    Differently from the .NET framework, NET Core can be exercised using command line tools like we're used to doing in Linux for so long. The .NET Core CLI is a powerful and very welcome addition to the .NET family. Finally!

    Open Standard

    .NET Core is compatible with the open-source .NET Standard, the standard for .NET-compatible platforms and created to establish greater uniformity in the .NET ecosystem. .NET Standard includes a formal specification of .NET APIs that are intended to be available on all .NET implementations including Xamarin and Mono.

    Microsoft still provides the .NET implementation compatibility table:
    Source: .NET Implementation Support

    Open source

    Being open-source is also a huge advantage. Now we can read, learn, fix and contribute to the code. For example, if you go to dotnet Core on GitHub, you'll be able to:
    • report bugs
    • participate in discussions
    • follow the project
    • follow the releases
    • read the release notes 
    • and much more!

    Side-by-side versions

    .NET can be installed side-by-side user- or machine-wide. That's a significant and major advancement of .NET Framework that was built before Windows 10 and Docker. Side-by-side versions allow developers (those that aren't deploying their apps on containers) to target specific versions of the framework without breaking their apps.

    Improved Platform Support

    Windows, Mac, Linux and Docker and soon with .NET 5,  will also run on XBox, Mobile, IoT, Cloud. In summary, everywhere.

    SignalR Core

    As I mentioned above, yes, the new SignalR Core is finally available with this release. That's awesome news for those who are using this library and were waiting for that upgrade!

    Entity Framework Core - A new Entity Framework

    Entity Framework also gained some modern features and also became open-source on its new incarnation names Entity Framework Core. EF Core includes faster queries, a serious list of database providers and support ranging from SQLite cloud databases. Plus, EF can also be installed and run from the command line via it's EF.NET CLI.
    Installation is as simple as:
    dotnet tool install --global dotnet-ef

    Razor Class Libraries

    We can now embed Razor in class libraries so Razor is used in your backend to build views and pages into reusable class libraries. According to Microsoft:
    Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence.

    .NET Core Tools

    This is a very important feature. Existing already in NPM, it basically allows us to install global applications from nuget packages. .NET Core tools run on all .NET Core supported operating system and chip architecture by default, with one set of binaries. To install a global .NET tool, run:
    dotnet tool install --global dotnetsay

    Build Improvements

    Builds are faster on .Net Core 2.1. Here's a comparison from Microsoft:

    .NET Core 2.1 Incremental Build-time performance improvements
    Source: https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-net-core-2-1/

    Docker Support 

    Docker support is probably the biggest benefit of .NET Core to date. Docker, containers and microservices are booming and became the standard way to build, package and ship our apps. Today Microsoft offers multiple .NET Core images on  Docker Hub and creating your container is some keywords away.

    ASP.NET Core

    ASP.NET gained tons of features and on .NET Core is more lightweight and faster than its older sibbling including:
    • The framework is completely distributed as NuGet packages
    • Cloud-optimized runtime
    • A unified story for building web UI and web APIs
    • A cloud-ready environment-based a rewritten configuration system
    • A lightweight and modular HTTP request pipeline
    • Build and run cross-platform ASP.NET Core apps on Windows, Mac, and Linux
    • Open-source
    • Side-by-side app versioning when targeting .NET Core.

    The dotnet tool also provides lots of templates so you can quickly scaffold the project you need from the command line:

    Blazor

    Blazor lets you build interactive web UIs using C# instead of JavaScript. Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS. Both client and server code is written in C#, allowing you to share code and libraries. Blazor is a feature of ASP.NET, the popular web development framework that extends the .NET developer platform with tools and libraries for building web apps.

    Significant Perform Enhancements

    .NET Core also brings significant performance features over .NET Framework. Many are the reasons, notably an extensive use of async patterns, ligther codebase and decoupling from heavier Windows-only features such as WPF and IIS. For more information check:

    Modern Features

    .NET Core brings new features including gRPC, Razor Pages, Blazor, and many other updates, including a more up-to-date development framework providing simple integration with Node.js, Vue.js and React.

    Kestrel

    Kestrel is the default web server specified by the ASP.NET Core project templates. Kestrel can be used by itself as an edge server processing requests directly from a network, including the Internet. Kestrel can be run directly or indirectly with the Internet without a reverse proxy server, with a reverse proxy server, such as IIS, Nginx, or Apache or indirectly with the Internet through a reverse proxy server, such as IIS, Nginx, or Apache.

    Coming soon: .NET 5.0

    And, it all changes again! Microsoft recently announced that they'll combine .NET Framework and .NET Core in .NET 5.0. If you want to read more, I wrote in detail why that's another significant and important for .NET developers:

    Conclusion

    On this post we reviewed a little about the histories of the .NET Framework and .NET Core. We also discussed the main advantages of .NET Core and why you should use it going forward. .NET Core is Microsoft's bold attempt to modernize their omnipresent framework. Some articles to read next are:

    About the Author

    Bruno Hildenbrand      
    Principal Architect, HildenCo Solutions.