advanced web form practices - miguel a. castro
TRANSCRIPT
![Page 2: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/2.jpg)
ineta
.NET Architect, Developer, & Trainer Microsoft MVP ASP Insider Member of the INETA Speakers Bureau Conference Speaker Creator of CodeBreeze In IT business since 1986
Your Speaker
2
![Page 3: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/3.jpg)
Agenda
• What is a Provider?
• The [ASP.NET] Provider Model
• The Config File
• Identifying the Need for Swappable State Storage
• Designing & Developing the Swappable State Storage Provider
• Summary
![Page 4: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/4.jpg)
What is a Provider?• Based on the Strategy Pattern (GoF)
• Allows the separation of definition and implementation + ability to swap implementations
4
Provider Definition(interface or abstract
class)
Concrete class 1 Concrete class 2
Config decides
which to use.
![Page 5: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/5.jpg)
The ASP.NET Provider Model
• Designed by the ASP.NET team
• Part of System.Web.dll• Used by many features of ASP.NET
• Membership, Roles, Profile, WebPart, Personalization, Session, Logging, etc.
• Not just for ASP.NET
5
![Page 6: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/6.jpg)
Config-file Provider Sections
6
<membership defaultProvider="{defaultProvider}"> <providers> <add name="{provider name}“ type="{.Net type}" .../> <add name="{provider name}“ type="{.Net type}" .../> </providers> </membership>
![Page 7: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/7.jpg)
The Membership Provider
7
Membership.CreateUser
Factory Class Command
Static Constructor:•Read Web.Config file•Determine what Provider to use•Instantiate Provider and cast to Abstraction•Call CreateUser in provider
![Page 8: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/8.jpg)
The Membership Provider
8
Membership Object Model
Membership
MembershipProvider
SqlMembershipProvider
Static Factory Class
Abstraction
Provider Class
![Page 9: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/9.jpg)
The Membership Provider
9
<membership defaultProvider=“AspMembershipProvider"> <providers>
<clear /><add name="AspMembershipProvider“
type=“System.Web.Security. SqlMembershipProvider“ connectionStringName="connStr“ requiresUniqueEmail="true”
… other attributes … </providers></membership>
![Page 10: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/10.jpg)
Swappable State StorageRequirements
• Ability to swap out method of state storage on-the-fly.– Session– Application– Cache– Cookies
• Ability to define more than one state-storage provider.
![Page 11: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/11.jpg)
Swappable State StorageDesign
• Configuration section handler.– Config model follows standard provider
config model
• Static factory class for entry point.
• Provider Base class defines public interface.
• One provider implementation class for each type of storage.
![Page 12: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/12.jpg)
Swappable State StorageDesign
• Data:– Ability to isolate storage by provider name
– Ability to isolate storage by user• (using the Session ID)
– Key and Value used for storage/retrieval
• Behavior:– Store an item
– Retrieve a stored item
![Page 13: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/13.jpg)
Swappable State Storage
• Some data is static for ALL usage– Information described in previous slides
• Some data is different for EACH usage– Key used for storage– Value being stored
• Static data can be fed using configuration
• Dynamic data fed into method arguments
13
![Page 14: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/14.jpg)
Swappable State Storage Static Attributes
• isolateProvider– Uses name of the provider in the storage key– Allows two instances of same provider type to
store using same key
• isolateUser– Uses session ID in the storage key– Allows two users to store using same key– Important in Application and Cache
14
![Page 15: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/15.jpg)
Configuration Section
15
<configSections> <sectionGroup name="system.web"> <section name="stateStorage" type="WebFormLibrary. StateStorageSection, WebFormLibrary"/> </sectionGroup> </configSections>
![Page 16: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/16.jpg)
Configuration Section
16
<stateStorage defaultProvider="SessionStorageProvider"> <providers> <clear/> <add name=" SessionStorageProvider " type="WebFormLibrary. SessionStateStorageProvider, WebFormLibrary" isolateProvider="true" /> <add name="ApplicationStorageProvider" type="WebFormLibrary. ApplicationStateStorageProvider, WebFormLibrary" isolateUser="true" isolateProvider="true" /> </providers> </stateStorage>
![Page 17: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/17.jpg)
Custom Config-Section Class
• Inherits from ConfigurationSection• Properties:
• DefaultProvider:string• Providers:ProviderSettingsCollection
• Decorated with ConfigurationProperty• Rest of configuration definition handled by ProviderSettingsCollection type
17
![Page 18: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/18.jpg)
The Provider’s Definition
• Abstract class
• Inherits from ProviderBase• Defines fields that will come in from config
• Defines properties exposed by all implementations
• Defines abstract methods
18
![Page 19: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/19.jpg)
Concrete Classes(Session)
• Inherits from provider definition class• Overrides Initialize (ProviderBase)
• Config-section (<provider>) attributes received as NameValueCollection object.
• Provider-specific _Fields (defined here) and those defined in base class are filled.
• If expected attribute is not found, throw ProviderException
• Allowed attributes are removed from collection and collection is checked for anything left over.
• Abstract methods implemented
19
![Page 20: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/20.jpg)
The Factory Class
• Point of entry
• Static constructor reads all defined providers and makes provider determination from config information
• Duplicates provider’s methods for entry
• Exposes a collection of all providers addressable by name
• Exposes default provider
20
![Page 21: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/21.jpg)
Writing Providers
• Can be defined in separate assemblies
• Similar to the Session-State provider
• May use same, different, more, or less attributes than other providers– Has its own Initialize method implementation
• Implement abstract methods– Same, similar, or different than other providers
• Assembly needs to be accessible by host
21
![Page 22: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/22.jpg)
Writing Providers
• Application State Provider– Like Session but uses Application variable
• Cache State Provider– Like Session but uses Cache variable
• Cookie State Provider– Like the others but must serialize and
deserialize values before storage and retrieval– Cookies only store string values
22
![Page 23: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/23.jpg)
Using The Providers
• Define one or more in the config• Define one as the default provider• Use the default provider
• Use the StateStorage factory class to store and/or retrieve values
• Use a specific provider• Define a variable of the base class-type• Set to an element of StateStorage.Providers
using provider name as key
23
![Page 24: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/24.jpg)
Summary
• Provider model can accommodate a terrific plug-in architecture for many features of your application
• Familiar & proven pattern
• Can be used outside of ASP.NET• Need to reference System.Web.dll
• The ability to swap state storage can come in very handy
![Page 25: Advanced Web Form Practices - Miguel A. Castro](https://reader035.vdocument.in/reader035/viewer/2022062708/558a1304d8b42ab1588b45bb/html5/thumbnails/25.jpg)
References
www.dotnetdude.comwww.steelbluesolutions.com25
• Programming Microsoft ASP.NET Advanced Topics• Dino Esposito – Microsoft Press
• Professional ASP.NET 4.0 with C# or VB}• Mathew MacDonald - APress