I recently learned efcachingprovider. This extension package can be used not only for the extension of entityframework, but also for all applications related to database connections.ProgramYou can use a similar solution for expansion. Make a small summary today to facilitate future review.
General description
For more information about what efcachingprovider is and how to use it, see this article in garden.Article: Entity Framework cache processing and log monitoring. I will mainly talk about the internalCodeImplementation principle.
The figure in the garden article shows the extended position of efcachingprovider:
That is to say, entityconnection does not directly use sqlconnection In The Middle Of sqlclient, but converts the command through efcachingprovider and then uses sqlclient for execution.
Solution Structure
Key Project explanation:
Efproviderwrappertoolkit: defines the sqlclient extension framework, including dbconnectionwrapper and dbcommandwrapper.
Efcachingprovider: Implements sqlclient extensions in the form of caching, including dbcachingconnection and dbcachingcommand.
Eftracingprovider: added the log monitoring function based on sqlclient.
How to expand
Although efcachingprovider is named, It is a cache extension package of entityframework. But it is actually directly extended on system. Data. Common. That is to say, this extension package can be used for any application dealing with databases. (A few modifications may be required .)
It uses the decoration mode and abstract factory and directly inherits the following core ADO. Net objects: dbconnection, dbcommand, dbcommanddefinition, dbproviderfactorybase, dbproviderservicesbase, and is named *** wrapper. The following are extensions of dbconnection:
The specific decoration code is in the static method dbconnectionwrapper. wrapconnection:
/// <Summary> /// wraps the connection. /// </Summary> /// <Param name = "connection"> the connection. </param> // <Param name = "wrapperproviderinvariantnames"> the wrapper provider invariant names. </param> // <returns> wrapped connection. </returns> internal static dbconnection wrapconnection (dbconnection connection, Params string [] wrapperproviderinvariantnames) {foreach (string invariantname in wrapperproviderinvariantnames) {dbproviderfactory factory = dbproviderfactories. getfactory (invariantname); var connectionwrapper = factory. createconnection (); dbconnectionwrapper wrapper = (dbconnectionwrapper) connectionwrapper; wrapper. wrappedconnection = connection; connection = connectionwrapper;} return connection ;}
Usage:
Dbconnectionwrapper. wrapconnection (storeconnection, "eftracingprovider", "efcachingprovider ")
Summary
Efcachingprovider is a decoration mode extension package in system. Data. Common. Therefore, it can also be applied to database application systems other than entityframework. At the same time, it supports other extensions based on the framework.
If your database access system requires functions such as caching and tracing, try using this extension package. :)
Related links:
Http://code.msdn.microsoft.com/EFProviderWrappers
Http://kb.cnblogs.com/page/72713/