Provider Model is Win
The provider model’s goal is to allow for the abstraction of implementation to the nth degree by allowing for the choice between separate implementations of the same functionality. This design pattern is incredibly common in Microsoft’s ASP.NET, especially in the realm of security. Developers can choose between different providers for membership, roles and authentication for a single site. The point is that the core functionality of the site does not change, only the implementation. Below is a quick snapshot of how such a model is used in Vodka.
The driving force behind the provider model is the abstraction of the implementation. This is done via an interface.
1 2 3 | public interface IAuthenticationProvider{ bool Authenticate(string username, string password); } |
What gives the provider model its meat and bones is the different implementations of the above interface. The two implementations below represent two authentication backends: an ActiveDirectory installation and an Sql database. Although the meat has been stripped from these two classes, it is clear that their implementations would differ enough to have the two separate classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class ActiveDirectoryAuthenticationProvider : IAuthenticationProvider{ public bool Authenticate(string username, string password){ // Attempt to log user into AD. return authTest; } } public class SqlAuthenticationProvider : IAuthenticationProvider{ public bool Authenticate(string username, string password){ // Validate credentials against the Sql DB. return authTest; } } |
Now that the implementation is abstracted from the functionality requirements, the provider model requires only a driver to use differing implementations. Below is a sample driver that simply uses each provider to authenticate a user. It is important to note that there is no code to be changed based on what backend (provider) is being used due to the use of the common interface.
1 2 3 4 5 6 7 8 9 10 11 | public class Driver { public Driver(IAuthenticationProvider provider){ if(provider.Authenticate(“jsedlak”, “hello”)) Console.WriteLine(“You are authenticated!”); } public static void Main(){ Driver d1 = new Driver(new SqlAuthenticationProvider()); Driver d2 = new Driver(new ActiveDirectoryAuthenticationProvider()); } } |
The above example is a simplified version of the provider models used in Vodka where Membership, Authentication and Roles have all been squeezed into one provider. The reason for this is to remove an unnecessary code complexity requirement of three different security providers. More often than not, the three are commonly found together in the backend anyways.
No Comments
No comments yet.
RSS feed for comments on this post.
Leave a comment
You must be logged in to post a comment.
