The changes-set of entities to be saved are processed by the EntityServer which passes the change-set through an instance of the DevForce EntityServerSaveInterceptor. You can alter the course of that processing by creating a 'custom' subclass of the EntityServerSaveInterceptor and overriding its virtual properties and template methods. DevForce can discover the existence of your class automatically.
You don't have to write a custom interceptor class. The EntityServer will use the DevForce EntityServerSaveInterceptor if it doesn't find a custom subclass. Many production applications do include a custom interceptor.
The EntityServerSaveInterceptor class resides in the IdeaBlade.EntityModel.Server assembly which must be referenced by the project that contains your custom subclass.
You can write only one custom interceptor. It should be a public class and must have a public default, parameterless constructor (if it has a constructor).
Please keep your named custom interceptor stateless if possible. DevForce creates a new instance of this class for each query performed by the server so you generally don't have to worry about threading issues with instance state. If you decide to maintain static state, give great care to ensuring safe concurrent access to that state.
Avoid putting anything in the interceptor other than what is strictly necessary to achieve its purpose. The interceptor is a poor choice for a grab-bag of server-side features.
You don't have to override any of the template methods; the default base implementations all work fine. You may wish to be explicit in your custom class and override every template method; your override can simply delegate to the base implementation.
Make sure that the assembly containing your custom interceptor is deployed to the server such that it can be discovered. Assembly discovery is discussed here.
The EntityServerSaveInterceptor class contains a number of template methods that you can override to modify the base behavior of the class. These methods are executed at key points within the server-side part of the save process; you override them in your subclass to perform custom interventions. They enable you to perform operation both before and after the physical save to the database (or equivalent backing store).
Most of the template methods have no parameters because all of the relevant data are provided by properties and methods on each instance of the class. This also allows IdeaBlade to extend the base class in the future without breaking custom developer code.
Many of the template methods described below return a boolean result with a base implementation that returns "true". A return value of "true" allows the save to continue. A return value of "false" causes the save to exit with an exception. There is also a flag available that may be used to indicate a "canceled" operation.
Note that while the base implementation of the authorization method does not return ‘false’, it will throw an exception it it detects an unauthorized save. It treats an unauthorized save as an exception, not a ‘cancellation’.
In addition to the above, the following protected read only properties are also available.
Property | Property type | Used for |
---|---|---|
Context | object | Gets or sets a custom context object for this operation. |
EntityManager | EntityManager | Returns an EntityManager holding the entities to be saved. Note that this is not the original EntityManager on which the EntityManager.SaveChanges() call was made. This property can be very useful when overriding the ExecuteSave method. Additional entities that need to be saved can be added to this EntityManager or entities that should not be saved can be removed before calling base.ExecuteSave(). |
IsServerSave | boolean | Returns true if the save was issued on the server. This can occur as a result of an InvokeServerMethod call. This is useful because you typically do not need to reauthorize a save where the request for the save originates on the server. |
Principal | IPrincipal | The IPrincipal from the user session requesting this operation. |
SaveOptions | SaveOptions | Returns the SaveOptions provided in the SaveChanges call. |
VerifierEngine | VerifierEngine | Returns the VerifierEngine which will be used for server-side validation. |
Because this EntityManager is untyped, additional entities of any entity type may queried with it. Queries against this EntityManager are usually composed via a technique shown in the following example, ( the code below is assumed to be executing within the context of some EntityManagerSaveInterceptor method). Note the use of the EntityQuery<T> constructor.
C# | var newQuery = new EntityQuery<Customer>().Where(c => c.CompanyName.StartsWith("S")) var custs = newQuery.With(EntityManager).ToList(); |