Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
DYNAMIC STATE STORAGE: UNDERSTANDING THE ASP.NET PROVIDER MODEL Miguel A. Castro [email_address]
Agenda <ul><li>What is a Provider? </li></ul><ul><li>The [ASP.NET] Provider Model </li></ul><ul><li>The Config File </li><...
What is a Provider? <ul><li>Based on the Strategy Pattern (GoF) </li></ul><ul><li>Allows the separation of definition and ...
The ASP.NET Provider Model <ul><li>Designed by the ASP.NET team </li></ul><ul><li>Part of  System.Web.dll </li></ul><ul><l...
Config-file Provider Sections <membership    defaultProvider=&quot;{defaultProvider}&quot;> <providers> <add name=&quot;{p...
The Membership Provider Membership.CreateUser Factory Class Command <ul><li>Static Constructor: </li></ul><ul><li>Read Web...
The Membership Provider Membership Object Model Membership MembershipProvider SqlMembershipProvider Static Factory Class A...
The Membership Provider <membership  defaultProvider=“AspMembershipProvider&quot;> <providers> <clear /> <add name=&quot;A...
Swappable State Storage Requirements <ul><li>Ability to swap out method of state storage on-the-fly. </li></ul><ul><ul><li...
Swappable State Storage Design <ul><li>Configuration section handler. </li></ul><ul><ul><li>Config model follows standard ...
Swappable State Storage Design <ul><li>Data: </li></ul><ul><ul><li>Ability to isolate storage by provider name </li></ul><...
Swappable State Storage <ul><li>Some data is static for ALL usage </li></ul><ul><ul><li>Information described in previous ...
Swappable State Storage  Static Attributes <ul><li>isolateProvider </li></ul><ul><ul><li>Uses name of the provider in the ...
Configuration Section <configSections> <sectionGroup name=&quot;system.web&quot;> <section name=&quot;stateStorage&quot;  ...
Configuration Section <stateStorage defaultProvider=&quot;SessionStorageProvider&quot;> <providers> <clear/> <add name=&qu...
Custom Config-Section Class <ul><li>Inherits from  ConfigurationSection </li></ul><ul><li>Properties: </li></ul><ul><ul><l...
The Provider’s Definition <ul><li>Abstract class </li></ul><ul><li>Inherits from  ProviderBase </li></ul><ul><li>Defines f...
Concrete Classes (Session) <ul><li>Inherits from provider definition class </li></ul><ul><li>Overrides  Initialize   (Prov...
The Factory Class <ul><li>Point of entry </li></ul><ul><li>Static constructor reads all defined providers and makes provid...
Writing Providers <ul><li>Can be defined in separate assemblies </li></ul><ul><li>Similar to the Session-State provider </...
Writing Providers <ul><li>Application State Provider </li></ul><ul><ul><li>Like  Session  but uses  Application  variable ...
Using The Providers <ul><li>Define one or more in the  config </li></ul><ul><li>Define one as the default provider </li></...
Summary <ul><li>Provider model can accommodate a terrific plug-in architecture for many features of your application </li>...
<ul><li>Programming Microsoft ASP.NET Advanced Topics </li></ul><ul><ul><li>Dino Esposito – Microsoft Press </li></ul></ul...
Upcoming SlideShare
Loading in …5
×

Advanced Web Form Practices - Miguel A. Castro

827 views

Published on

  • Be the first to comment

Advanced Web Form Practices - Miguel A. Castro

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

×