Skip to content

Introduction to Commands

Jon Smith edited this page Jan 20, 2015 · 16 revisions

The primary job that GenericServices does is standardise the interface between the User Interface (UI) and the database using Entity Framework (EF). GenericServices provides commands for List, Detail, Create, Update and Delete of data. All the commands apart from Delete can work either directly with the database (EF) classes or via Data Transfer Objects - DTOs.

Direct or indirect data access via a DTO

Each command, apart from Delete, can work with either a database (EF) classes or a DTO. See the section called Why DTOs? to hear why DTOs are so useful.

The commands work out whether the type/class you provided is one or the other of these and takes appropriate action.

  • If the given type inherits from EfGenericDTO/EfGenericDTOAsync then it will do certain transformations on the data. See PostsController for example of using DTO with commands.
  • Otherwise it assumes the given type is a database (EF) class and then directly accesses that data. See TagsController for example of direct access to database class.

Data and command validation

Entity Framework validates all the data before it writes or updates the database. If this validation fails it throws an exception. Other validation is done by SQL Server, again throwing an exception. Other issues, such as trying to read a entry that is no longer in the database is another issue that can throw an exception.

GenericServices tries to provide superior error checking and reporting back to the user. It does this by intelligently capturing all these exceptions and errors and returning a status with human readable messages from each of its commands. Please see the description of the ISuccessOrErrors interface for more information.

Sync/Async support

All the commands support both synchronous (sync) and asynchronous (async) access to the data. Async has become fairly easy since .NET 4.5 and provides better scalability for web applications. Examples are:

If you don't know anything about async/await in MVC then I recommend