Skip to content

Why GenericServices?

Jon Smith edited this page Jan 22, 2015 · 12 revisions

Generic Services is a .NET class library to help build a Service Layer, sometimes called the Application layer. This is a assembly in your web application (see Architecture Overview) that acts as a facard/adapter between your data service layers and your User Interface or HTTP service. Its aim is to make the creation of the service layer simple while providing robust implementations of standard database access commands.

What are the key benefits of GenericServices?

Below I have listed the key features that GenericServices provides. They are:

  • It provides a standard interface to the database. GenericServices has standard commands to list, show detail, create, update and delete data in the database. These commands have a fixed interface with just the class/data changing. This means the code in the presentation layer is very standard and simple.
  • It handles the complexities of mapping from the database to DTOs. In many cases you just have to write a class that inherits from the standard EfGenericDto/EfGenericDtoAsync class and add you properties and set one flag. GenericServices will then handle the copying/shaping of the data between the database and the presentation layer.
  • It handles data validation for you. GenericServices handles all the standard error checking and data validation for you. All the commands return a status with human-readable success or error massages to show to the user. This provides good feedback to the user, including confirmation that the action was successful (optional).
  • It provides efficient calculated parameters. Sometimes we might like a calculated parameter, like the sum total of all line items in an order. GenericServices, with the help of other open-source libraries, provides a simple mechanism for doing this while still retaining the efficiency of the final SQL command.
  • It is designed to produce efficient Entity Framework calls. GenericServices works with the data queries at the IQueryable<> level. This means they are turned into direct SQL calls to the database, thus stopping unnecessary in-memory conversion of data.
  • It supports both sync and async access. Since .NET 4.5 the use of async access to the database is really simple and has advantages for scalability of web sites or RESTful services. See my article The .NET 4.5 async/await feature in Promise and Practice for more on that.
  • It has a whole range of extension points. With any library there comes a point where it doesn't quite do what you need. GenericServices assumes it can't handle every case so in DTO mode it provides virtual properties and methods that can be overridden to achieve the exact features you need.
  • It is well test and documented. As well as over 250 Unit Tests I have developed two sample implementations of a MVC application - one fairly simple and one on a more complex database. Both have a) a live site to explore and b) the source code of the application.

Where would you use GenericServices?

GenericServices is aimed at Web/Mobile applications where you have what is called a 'disconnected state' between each call, i.e. each HTTP/REST call does not retain a state. This lack of state makes for a few issues not found in windows applications which GenericServices needs to deal with.

All my examples are using ASP.NET MVC, which is the recommended way of developing Web UI/RESTful interfaces. I have not tried it with ASP.NET web forms or WCF - it should work but it might not be so useful in something like web forms.