|
The Enterprise Library consists of the following seven Application Blocks. · Data Access Application Block · Exception Handling Application Block · Logging and Instrumentation Application Block · Security Application Block · Cryptography Application Block · Caching Application Block · Configuration Management Application Block
.gif) Figure 1 Enterprise Library's Application Blocks |
The following sections discusses each of these blocks briefly.
Data Access Application Block
ADO.Net Vs Data Access Block : Data Access Application Block is that application code can refer to particular databases by an ADO.NET connection string name, such as "Customer" or "Inventory." The application code can specify a named instance of a database and pass this parameter to theDatabaseFactory.CreateDatabase method. Each named database has its connection information stored in a configuration file. By changing the settings in the configuration file, developers can use their applications with different database configurations without recompiling their code.
Scenarios: Here I am explaining some key scenarios in which we can make use of DAAB. For all the scenarios , it is assumed that you already done neccessary configuration changes database creation.
1. DataReader : In this scenario, you want to retrieve multiple rows from a database and display them immediately and only once with a Web browser. You do not want to explicitly cache the data, manipulate it by using a DataSet object, or pass it to other components within your application. You simply want to use the results as quickly as possible.
These goals can be summarized as follows:
- You want to retrieve data for read-only purposes, which, in most cases, means that you want to display the data.
- You want to bind to a Web Form control for display.
- You do not need to cache the data. After it is used, it is discarded.
1: Database db = DatabaseFactory.CreateDatabase();
2:
3: using (IDataReader reader = db.ExecuteReader(CommandType.Text, "Select Name, Address, City From Customers" ))
4: { 5: customerGrid.DataSource = dataReader;
6: customerGrid.DataBind();
7: }
2. DataSet : In a multi-tier system, you may need to pass data from a data access component to a middle-tier business component. The data is retrieved from the database and sent back, through the data access layer, to the business layer. The information is contained in the DataSet object.
1: Database db = DatabaseFactory.CreateDatabase();
2:
3: string sql = "GetProductsByCategory";
4: DbCommand cmd = db.GetStoredProcCommand(sql);
5:
6: // Retrieve products from category 7.
7: int category = 7;
8: db.AddInParameter(cmd, "CategoryID", DbType.Int32, category);
9:
10: DataSet productDataSet = db.ExecuteDataSet(cmd);
3. ExecuteNonQuery : A common database task is to retrieve specific, multiple-column values. For example, in a Web-based online retail application, you may want to retrieve full product details for a certain product in response to a user request.
1: Database db = DatabaseFactory.CreateDatabase();
2:
3: string sql = "GetProductDetails";
4: DbCommand cmd = db.GetStoredProcCommand(sql);
5:
6: db.AddInParameter(cmd, "ProductID", DbType.Int32, 3);
7: db.AddOutParameter(cmd, "ProductName", DbType.String, 50);
8: db.AddOutParameter(cmd, "UnitPrice", DbType.Currency, 8);
9:
10: db.ExecuteNonQuery(cmd);
11:
12: string results = string.Format(CultureInfo.CurrentCulture, "{0}, {1}, {2:C} ", 13: db.GetParameterValue(cmd, "ProductID"),
14: db.GetParameterValue(cmd, "ProductName"),
15: db.GetParameterValue(cmd, "UnitPrice"));
3. ExecuteScalar : There are many situations in which you have to perform a single-item lookup.
1: Database db = DatabaseFactory.CreateDatabase();
2:
3: string sql = "GetProductName";
4: int productId = 7;
5: DbCommand cmd = db.GetStoredProcCommand(sql, productId);
6:
7: string productName = (string)db.ExecuteScalar(cmd);
4. ExecuteNonQuery for multiple updates : There are many situations in which you have to perform a single-item lookup.When an application executes multiple operations against a database, a common requirement is that all of the operations must succeed or the database must roll back to its original state (that is, its state before the operations began).
1: public bool Transfer(int transactionAmount, int sourceAccount, int destinationAccount)
2: { 3: bool result = false;
4:
5: // Create the database object, using the default database service. The
6: // default database service is determined through configuration.
7: Database db = DatabaseFactory.CreateDatabase();
8:
9: // Two operations: one to credit an account and one to debit another account.
10: string sql = "CreditAccount";
11: DbCommand creditCommand = db.GetStoredProcCommand(sql);
12:
13: db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
14: db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);
15:
16: sql = "DebitAccount";
17: DbCommand debitCommand = db.GetStoredProcCommand(sql);
18:
19: db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
20: db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);
21:
22: using (DbConnection conn = db.CreateConnection())
23: { 24: conn.Open();
25: DbTransaction trans = conn.BeginTransaction();
26:
27: try
28: { 29: // Credit the first account.
30: db.ExecuteNonQuery(creditCommand, trans);
31: // Debit the second account.
32: db.ExecuteNonQuery(debitCommand, trans);
33:
34: // Commit the transaction.
35: trans.Commit();
36:
37: result = true;
38: }
39: catch
40: { 41: // Roll back the transaction.
42: trans.Rollback();
43: }
44: conn.Close();
45:
46: return result;
47: }
48: }
5. DataSet to update database : The goal in this scenario is to transfer data stored in a DataSet object to the database.
1: Database db = DatabaseFactory.CreateDatabase();
2:
3: DataSet productsDataSet = new DataSet();
4:
5: string sql = "Select Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products";
6: DbCommand cmd = db.GetSqlStringCommand(sql);
7:
8: string productsTable = "Products";
9:
10: // Retrieve the initial data.
11: db.LoadDataSet(cmd, productsDataSet, productsTable);
12:
13: // Get the table that will be modified.
14: DataTable dTable = productsDataSet.Tables[productsTable];
15:
16: // Add a new product to existing DataSet.
17: DataRow addedRow = dTable.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25}); 18:
19: // Modify an existing product.
20: dTable.Rows[0]["ProductName"] = "Modified product";
21:
22: // Establish the Insert, Delete, and Update commands.
23: DbCommand insertCommand = db.GetStoredProcCommand("AddProduct"); 24: db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
25: db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
26: db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
27:
28: DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct"); 29: db.AddInParameter(deleteCommand , "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
30:
31: DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct"); 32: db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
33: db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
34: db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
35:
36: // Submit the DataSet, capturing the number of rows that were affected.
37: int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand, deleteCommand,
38: Microsoft.Practices.EnterpriseLibrary.Data.UpdateBehavior.Standard);
Exception Handling Application Block
An exception is an error that occurs at runtime. If the same is not handled by your code, it might lead to premature termination of the flow of execution of your application. Exceptions should be handled very carefully as handling them are quite expensive and too many exception blocks in your code might lead to poor application performance. The Patterns and Practices group from Microsoft has introduced the Exception Handling Application Block with the sole purpose of providing the application developers with a set of the best practices and strategies that should be followed for handling exceptions in the code. The salient features of the Exception Handling Application Block include:
· Encapsulates the recommended best practices on handling exceptions
· Support for logging exceptions
· Extensibility
· Ease of use
Logging and Instrumentation Application Block
Efficient Logging and Instrumentation feature in an enterprise application development phase facilitates application performance monitoring. Hence, it is a very important feature in enterprise application software development. The Patterns and Practices group from Microsoft has come up with this powerful block that can be instrumental in the above stated situations. The salient features of the Logging and Instrumentation Application Block include:
· Support for logging synchronously and asynchronously
· Support for formatters
· Support for logging to different destinations
Security Application Block
The Security Application Block from the Patterns and Practices group of Microsoft facilitates the implementation of security features in enterprise applications built on top of the Microsoft .NET framework. The salient features of the Security Application Block include:
· Support for authentication and authorization
· Supports both role and profile management
· Plug ability with the Data Access Application Block
· Plug ability with the Cryptography Application Block
Cryptography Application Block
The Cryptography Application Block is a collection of reusable and extensible code that simplifies the usage of cryptographic functionality in applications that are designed on the Microsoft.NET framework. The salient features of the Cryptography Application Block include:
· Plug ability
· Configurable
· Extensible
Caching Application Block
Caching is an important feature that promotes improvement of application performance by storing the frequently used pages or data in main memory. The Caching Application Block from the Patterns and Practices group of Microsoft encapsulates efficient, consistent caching strategies and, hence, simplifies implementation of caching strategies in enterprise application development. The salient features of the Caching Application Block include:
· Support for efficient caching strategies in web applications
· Configurable
· Thread Safety
Configuration Management Application Block
The Configuration Management Application Block from the Patterns and Practices group of Microsoft provides, "a seamless integration with configuration sources other than XML files. You could use the registry, a database, or many other storage mediums, all using a common interface to manipulate the configuration information." The salient features of the Configuration Management Application Block include:
· Support for reading and writing the configuration data
· Support for storing configuration data at multiple locations
· Support for caching the configuration data
· Plug ability
Thanks!
_mkrs_