How to organize the best performance, the best business layer (1)
(Contrast below OpenSource: DotNetnuke
3.0.11
, .Text, duwamish7.0)
1 Do you only do only Open and Close operations for Connection during a Postback process?
Connecting pools, multiple OPEN and CLOSE connections can be used in a business cycle.
But when writing the data layer, it is best to use only one connection and only open it. The class of the data layer should implement the IDisposable interface.
In the business layer, a business method corresponds to a class of life cycles of a data layer. E.g:
Public BookData getcategoryItems (int CategoryID)
{
Applicationassert.checkcondition (categoryid> = 0, "invalid category id", ApplicationAsetrt.LineNumber;
Using (Books BookDataAccess = New Books ())
{
// You can do any other Oser.
Return BookDataAccess.getBooksBycategoryId (CategoryID);
}
}
Public BookData getDailyPickItems (int CategoryID)
{
Using (Books BookDataAccess = New Books ())
{
Return BookDataAccess.getDailyPickBooksBycategoryId (categoryID);
}
}
2 The maximum expansion, abstract all the operations of the business, reducing the dependence between layers and layers.
The business layer object can be seen as a collection of a series of business implementations, or some particularities can be inherited from some base classes such as MarshalByrefObject, and some particularities can be obtained. Abstract these methods is not implemented, and it is achieved by other classes. This can determine which implementation of use by configuration. The class of the business layer does not need to implement idisposable. (Except special circumstances)
Since Idisposable is not implemented, each release work is handed over to GC. Such examples:
Every time you have a business layer instance when you need to call your business method, and the implementation of your business method does not need to be cared.
------------------------------------------- ---------------------
Public Mustinherit Class DataProvider
'Provider constants - Eliminates Need for Reflection Later
Private const [providertype] as string = "data" 'Maps to
IN Web.config
Public Shared Function Instance () AS DataProvider
DIM STRCACHEKEY AS STRING = [ProviderType] & "provider"
'Use the cache because the reflection used later is expensive
Dim objconstructor as constructorinfo = ctype (datacche.getcache (strcachekey), constructorinfo
If Objconstructor is nothing the new the name of the provider
Dim objProviderConfiguration as providerConfiguration = providerConfiguration.getProviderConfiguration ([ProvIDertype])
'The assembly shop be in / bin or gac, so we simply need to get an instance of the type
Try
'Get the TypeName of the core dataprovider from web.config
DIM Strtypename as string = ctype (ObjProviderConfiguration.Providers (objProviderConfiguration.defaultProvider), provider) .type
'Use Reflection to Store THE CONSTRUctor of The Class That IMPLEments DataProvider
Dim t as type = type.gettype (startpename, true)
Objconstructor = T.GetConstructor (System.Type.emptytypes)
'INSERT the TYPE INTO The Cache
Datacche.setCache (StrcacheKey, Objconstructor)
Catch e as exception
'Could Not Load The Provider - this is Likely Due To Binary Compatibility Issues
END TRY
END IF
Return CType (Objconstructor.Invoke (Nothing), DataProvider
END FUNCTION
'Here Is The Abstract Method
Public Mustoverride Function GetProviderPath () AS STRING
END CLASS
----------------------------------------------------------------------------------------------------------------- --------------------
Public Class SqldataProvider Inherits DataProvider
Private const provodertype as string = "data"
Private _ProviderConfiguration AS ProviderConfiguration = providerConfiguration.getProviderConfiguration (providertype)
// Constructor Here
Public Sub New ()
'Read The Configuration Specific Information for this provider
Dim objProvider as provider = ctype (_ProviderConfiguration.Providers (_ProviderConfiguration.defaultProvider), provider
'Read The Attributes for this Province
End Sub
//Mplement the Abstract Method in DataProvider
Public overrides function getProviderPath () AS STRINGEND FUNCTION
END CLASS
Usually use this method, from the design mode, it is a Strategy module, and a bit similar to the modulation mode in the MVC (although this is not the observer mode). Anyway, this greatly reduces the coupling between layers and layers. Some people see the instance () method, will say, is this a Singleton mode? No, because careful analysis, I will find a new instance () every time INSTANCE () actually creates.
Then let's take a look at the blog we used now.
TEX
t.
.Text implementation method and I mentioned above, but he is a bit slight difference when dealing with the business layer.
.Text still abstains the business method, the same use of configurable selection to call that class to implement these abstract methods. But let's take a look at the following code:
Public Class DBProvider
{
Private dbProvider ()
{}
Static dbProvider ()
{
DBProviderConfiguration DPC = config.settings.blogproviders.dbProvider;
DP = (idbProvider) dpc.instance (); // creates an instance from the configuration node class read from the configuration.
DP.Connectionstring = dpc.connectionstring;
}
Private static readonly idbprovider dp = NULL;
Public Static IDBProvider Instance ()
{
Return DP;
}
}
This dbProvider should look at IDBPROVIDER. Abstract methods are in idbProvider, DBProvider actually provides a call to the entry. We analyze the DBProvider class, you can find that the process of creating an instance is placed in the static constructor of the dbProvider, and the PRIVATE of the DBProvider is off, which is actually a very typical Singleton mode. This means that there is always the end of the APPLICATION at a time it is created. Each time you have passed the instance () method in the future, it is actually a static instance in memory without repeatedly created.
The method I have implemented in front of me is to create an instance each time and then handed over to GC recycling.
Is the current two ways? I personally comment is Scott.
TEX
The use of Singleton mode is not very wise. Because Singleton is effective when processing a large number of concurrent request calls? And you should use the Singleton mode for business methods? But you can avoid creating additional overhead of destroying it. This place hopes that someone will discuss it. J