You have many decisions to make when building an ASP.NET MVC app. In this post, we explore options for working with data by covering topics like Entity Framework, LINQ and Object Mapping.
So you’re considering ASP.NET MVC for your next project? It’s fantastic that you’ve picked your framework of choice. Still, you have many more choices before you when planning your app—from tooling choices during development all the way through deployment and performance strategies. In this post, we’ll share some of developer advocate Ed Charbeneau’s recommendations for working with data (Entity Framework, LINQ and Object Mapping). If you’d like to read more on the topic, we recommend Ed’s recent whitepaper, Planning an ASP.NET MVC Application.
Let’s get started.
When it comes to database access with ASP.NET, Microsoft’s Entity Framework (EF) is the go-to option. EF is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data access code that developers usually need to write. If you work with EF to query data using LINQ, you don’t need to switch context between C# code and SQL.
While EF provides a robust infrastructure for working with databases in .NET, there are special scenarios where additional tooling can be helpful. The tools listed below will assist in speeding up the development process:
TOOL |
PURPOSE |
Useful design-time utilities for EF6
|
|
EntityFramework Reverse POCO Generator
|
Reverse-engineers an existing database and generates EntityFramework Code First POCO classes, configuration mappings and DbContext |
When LINQ was added to C# in ASP.NET 3.0, it changed the way that we work with data collections. When working with server-side data in ASP.NET applications, LINQ is an invaluable time saver. It makes short work of iterative functions by means of a simple syntax that is easy to read and understand. In addition, it enables developers to build queries in C# code, which are translated into SQL by Entity Framework. This means LINQ is not only a handy tool for operating on collections and arrays, but also for interacting with databases from .NET.
CATEGORY |
METHODS |
Quantify |
All, Any, Contains |
Filter |
Where, OfType |
Project/Transform |
Select, SelectMany, Zip |
Criteria/Set |
Distinct, Except, Intersect, Union |
Sorting |
OrderBy, OrderByDecending, ThenBy, ThenByDecending, Reverse |
When building web applications in ASP.NET MVC, typically there’s a rich domain model that’s bound to the database with Entity Framework. With this exists a general problem where backend objects are generally very rich and behavioral and the user interface is not. Mixing those two things together poses a challenge for developers. To solve this problem an object mapper can be used to copy that information from the rich backend objects to frontend data transfer objects (DTO). In doing so, a separation is clearly defined between database behaviors and frontend view models. Additionally, this approach will provide added security by solving mass-assignment vulnerabilities that can exist in MVC applications.
Automapper is one effective tool that can be used to handle object mapping in ASP.NET. Automapper, as the name suggests, can copy values between two like objects so mappings don’t need to be created by hand.
For a more detailed view of key considerations—software management tools, development process and methodology, tooling and development, backend methodologies, testing methodologies and more—make sure you download Ed’s “Planning an ASP.NET MVC Application” whitepaper.
Download Ed's Whitepaper
While you’re at it, if you haven’t played with Telerik UI for ASP.NET MVC, consider downloading a free trial. Our UI library enables you to quickly and easily deliver a sleek UI and delightful UX, increasing your productivity and your app’s performance.
Start Your Free Trial
Sara Faatz leads the Digital Experience Technology Community Relations team at Progress. She has spent the majority of her career building community, producing events, creating marketing programs and more. When she's not working, she likes diving with sharks, running and watching hockey.