Skip to content

Configuration Options

Jon P Smith edited this page Oct 3, 2016 · 4 revisions

Configuration of GenericService

GenericServices can be configured via the static class GenericServicesConfig. The following static properties or methods are available via GenericServicesConfig, e.g. GenericServicesConfig.SqlErrorDict(). Note that the default GenericServicesConfig should be suitable for most applications.

List of properties/commands

Dictionary for catching SQL errors

When writing to the database SQL validates the data in various ways defined by the database. Normally any error at this level would cause an Exception. However in GenericServices we provide a way to catch specific SQL errors that we are able to report in a useful way. Please see the section called DbUpdateException during SaveChanges in my Simple-Talk article Catching Bad Data in Entity Framework.

This is implemented by having the SaveChangesWithChecking() method, which is used inside all the GenericService methods for committing changes to the database, catch any SqlException errors and checking them against the codes in two dictionaries in the following order:

  1. SqlHandlerDict. If an entry for the sql error number is present it calls the method associated with that error number. The method has the signature HandleSqlException. If the method can handle the error it returns a ValidationResult, otherwise null.
  2. SqlErrorDict. If an entry for the sql error number is present it returns the error message. If not then it lets the exception ripple up as normal.

Methods to get at these two dictionaries

  1. IReadOnlyDictionary<int, string> SqlErrorDict and IReadOnlyDictionary<int, HandleSqlException> SqlHandlerDict to read the two dictionaries.
  2. void ClearSqlErrorDict() and void ClearSqlHandlerDict() to clear the respective dictionary of all entries.
  3. Adding an entry. There are slightly different versions for the two dictionaries:
  • void AddToSqlErrorDict(int sqlErrorNumber, string errorText) This sets the error string for the entry sqlErrorNumber in the dictionary. If an entry for that key exist it updates that entry. Note: By default the SqlErrorDict contains two sql error codes, 547 (foreign key constraint) and 2601 (index constraint)
  • AddToSqlHandlerDict(int sqlErrorNumber, HandleSqlException errorHandler, bool checkNotAlreadySet = true) which sets the error handler for the entry sqlErrorNumber in the dictionary. If it already has an entry and you haven't set the checkNotAlreadySet to false then it will throw and exception

Catching exceptions via RealiseSingleWithChecking

RealiseSingleExceptionMethod is called if an exception happens when '.RealiseSingleWithChecking' is called inside GetDetails or GetOriginal. This can intercept exceptions and turn them into errors.This property is used by GenericSecurity to catch permission errors.

  • RealiseSingleException RealiseSingleExceptionMethod Set this to your method. If not given then exceptions are not caught.

Control use of DelegateDecompiler

GenericServices supports the use of DelegateDecompiler to allow calculated properties to be added to EF data classes.

UseDelegateDecompilerWhereNeeded (Default true) If set to true then it will look at the TEntity class and any associated TEntity classes to see if any of its properties have the [Computed] attribute and will enable to use of .DecompileAsync() in all data reads.

If set to false then GenericServices will not check for [Computed] attribute in TEntity classes. However you can manually force the use of .DecompileAsync() by overriding the ForceNeedDecompile in the DTO and setting it to true.

Clone this wiki locally