ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
DYNAMIC STATE STORAGE: UNDERSTANDING THE ASP.NET PROVIDER MODEL Miguel A. Castro [email_address]
Advanced Web Form Practices - Miguel A. Castro
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
What is a Provider? Based on the Strategy Pattern (GoF) Allows the separation of definition and implementation + ability to swap implementations Provider Definition (interface or abstract class) Concrete class 1 Concrete class 2 Config decides which to use.
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
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>
The Membership Provider 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
The Membership Provider Membership Object Model Membership MembershipProvider SqlMembershipProvider Static Factory Class Abstraction Provider Class
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>
Swappable State Storage Requirements Ability to swap out method of state storage on-the-fly. Session Application Cache Cookies Ability to define more than one state-storage provider.
Swappable State Storage Design 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.
Swappable State Storage Design 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
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
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
Configuration Section <configSections> <sectionGroup name=&quot;system.web&quot;> <section name=&quot;stateStorage&quot;   type=&quot; WebFormLibrary.   StateStorageSection,   WebFormLibrary &quot;/> </sectionGroup> </configSections>
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>
Custom Config-Section Class Inherits from  ConfigurationSection Properties: DefaultProvider: string Providers: ProviderSettingsCollection Decorated with  ConfigurationProperty Rest of configuration definition handled by  ProviderSettingsCollection  type
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
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
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
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
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
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
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
Programming Microsoft ASP.NET Advanced Topics Dino Esposito ¨C Microsoft Press Professional ASP.NET 4.0 with C#  or VB} Mathew MacDonald - APress

More Related Content

Advanced Web Form Practices - Miguel A. Castro

  • 1. DYNAMIC STATE STORAGE: UNDERSTANDING THE ASP.NET PROVIDER MODEL Miguel A. Castro [email_address]
  • 3. 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
  • 4. What is a Provider? Based on the Strategy Pattern (GoF) Allows the separation of definition and implementation + ability to swap implementations Provider Definition (interface or abstract class) Concrete class 1 Concrete class 2 Config decides which to use.
  • 5. 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
  • 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. The Membership Provider 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
  • 8. The Membership Provider Membership Object Model Membership MembershipProvider SqlMembershipProvider Static Factory Class Abstraction Provider Class
  • 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. Swappable State Storage Requirements Ability to swap out method of state storage on-the-fly. Session Application Cache Cookies Ability to define more than one state-storage provider.
  • 11. Swappable State Storage Design 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.
  • 12. Swappable State Storage Design 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
  • 13. 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
  • 14. 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
  • 15. Configuration Section <configSections> <sectionGroup name=&quot;system.web&quot;> <section name=&quot;stateStorage&quot; type=&quot; WebFormLibrary. StateStorageSection, WebFormLibrary &quot;/> </sectionGroup> </configSections>
  • 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. Custom Config-Section Class Inherits from ConfigurationSection Properties: DefaultProvider: string Providers: ProviderSettingsCollection Decorated with ConfigurationProperty Rest of configuration definition handled by ProviderSettingsCollection type
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. Programming Microsoft ASP.NET Advanced Topics Dino Esposito ¨C Microsoft Press Professional ASP.NET 4.0 with C# or VB} Mathew MacDonald - APress

Editor's Notes

  1. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  2. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  3. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  4. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  5. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  6. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  7. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  8. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  9. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  10. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  11. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  12. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  13. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  14. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  15. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  16. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  17. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  18. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  19. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  20. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  21. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  22. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  23. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  24. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.
  25. MGB 2003 ? 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.