A protection proxy controls resource acquisition. It is used to fetch resources and authenticate users. This C# design pattern can coalesce code and make it easier to maintain.
An implementation must access the database only when required to, and only when the user is authenticated. We use an interface
to work with the protection proxy.
We want a class
for database-related code. The logic sends back DataTable
or DataSet
objects to the rest of the program. In this database layer file, we need an interface
.
interface
is a contract that will be used to "connect" the actual database-using code to the proxy class
.interface
containing five methods, but it does not include the actual SQL code in its methods.DatabaseLayer.cs - Interface IJournalData
interface
This interface
is shared between the database layer and proxy. This interface
will be shared by the proxy and database classes. An interface
does not have implementations.
public interface IJournalData { void InsertNewRow(DateTime dateTime); DataTable GetSearchResults(string queryString); }
The proxy sets between the database code itself, and the callers in the user interface. The Authenticate()
method is not part of the interface
, but a special method on the proxy.
public class JournalProxy : IJournalData { JournalLayer _journalLayer; string _password; public bool Authenticate(string suppliedPassword) { // User sends in the password, and we make sure it is correct. if (string.IsNullOrEmpty(suppliedPassword) || suppliedPassword != _password) { return false; } // Password is correct. // Allocate the database layer, // ... and setup the connection to the database code. _journalLayer = new JournalLayer(); return true; } public void InsertNewRow(DateTime dateTime) { // In every method that the proxy implements from the interface, // ... make sure that the user was authenticated. if (_journalLayer == null) { return; } // Forward the call to the actual database code. _journalLayer.InsertNewRow(dateTime); } public DataTable GetSearchResults(string queryString) { if (_journalLayer == null) { return null; } return _journalLayer.GetSearchResults(queryString); } }
At this point, we use the protection proxy in the user interface code. We are using Windows Forms and this code is inside a class
that implements a Form.
JournalProxy _journal = new JournalProxy(); private void PasswordEntered() { if (_journal.Authenticate(passwordTextBox.Text)) { // Show something from the database. } else { textBox1.Text = "Password is incorrect"; // User must now enter a new password. passwordTextBox.Clear(); } }
Here is that we don't need to worry about passwords because the protection proxy handles that logic. It uses an abstraction to make this easier.
class JournalLayer : IJournalData { public void InsertNewRow(DateTime dateTime) { return; } public DataTable GetSearchResults(string queryString) { return new DataTable(); } }
We consolidate the password authentication code. You can create code that accesses the database without worrying about the password logic.
We used the protection proxy design pattern to manage accesses to important resources. Protection proxy makes C# applications easier to develop when requiring a password for a database.