Week-08

=Week-08=

Discuss the benefits of separating code into layers: DAL & BLL
The advantages are the following:
 * separation of logic where it belongs to
 * no business logic in the UI
 * all the applications look the same and consequently developers knowing this architecture will immediately know where to search for the corresponding logic
 * exchangeable DAL. The interfaces define the contracts for accessing the corresponding layer.
 * Unit testing becomes easier, just focusing on the BL logic and DAL
 * theapplication could have many entry points (web interface, Winforms client, webservice). All of them can reference the same business logic (and DAL).
 * If the data access code is kept in a separate layer, it will be easy to adjust it to work with a different database. Database-specific code will be encapsulated in this layer, and so any changes here will not affect the business layer implementation.
 * If the business logic is kept in one place, it will be easy to offer its services to other applications, for example, to serve requests made via web services.
 * The maintenance efforts will be kept lower. Whenever you need to change something, you'll know where to find the responsible code, what to change and how to assure the change will not affect the rest of the code.

In general terms, describe the difference between using data sources with a database and using data sources with data access classes.
ADO.NET provides two basic strategies for how you work with this model. One model is store data in a dataset, which is an in-memory cache of records you can work with while disconnected from the data source. To use a dataset, you create an instance of it and then use a data adapter to fill it from the data source. You then work with the data in the dataset — for example, by binding controls to dataset members.

An alternative strategy is to perform operations directly against the database. In this model, you use a data command object that includes an SQL statement or a reference to a stored procedure. You can then open a connection, execute the command to perform the operation, and then close the connection. If the command returns a result set — that is, if the command performs a Select statement — you can fetch the data using a data reader, which acts like a highly efficient read-only cursor. The data reader can then act as a source for data binding.

A common model for data access in Visual Studio .NET applications is to store data in datasets and use a data adapter to read and write data in the database. (A .NET application is one that uses the .NET Framework: the common language runtime and the managed classes.) The advantages of the dataset model are:
 * Storing Data in Datasets**


 * Working with multiple tables A dataset can contain multiple tables of results, which it maintains as discrete objects. You can work with the tables individually or navigate between them as parent-child tables.
 * Manipulating data from multiple sources The tables in a dataset can represent data from many different sources (for example, from different databases, from XML files, spreadsheets, and so on, all in the same dataset). Once the data is in the dataset, you can manipulate it and relate it in a homogeneous format as if it had come from a single source.
 * Moving data between tiers in a distributed application By keeping data in a dataset, you can easily move it between the presentation tier, business tier, and data tier of your applications.
 * Data exchange with other applications A dataset provides a powerful way to exchange with other components of your application and with other applications. Datasets include extensive support for features such as serializing data as XML and reading and writing XML Schemas.
 * Data binding If you are working with forms, it is usually easier to bind controls to data in a dataset than it is to programmatically load data values into the control after executing a command.
 * Maintaining records for reuse A dataset allows you to work with the same records repeatedly without requerying the database. Using dataset facilities, you can filter and sort records, and you can use the dataset as a source of data if you are paging.
 * Ease of programming When you work with a dataset, you can generate a class file that represents its structure as objects (for example, a Customers table in the dataset can be accessed as the dataset.Customers object). This makes it easier, clearer, and less error-prone to program with, and is supported by Visual Studio tools such as IntelliSense, the Data Adapter Configuration wizard, and so on.

Alternatively, you can interact with the database directly. In this model, you use a data command object that includes an SQL statement or a reference to a stored procedure. You can then execute the command to perform the operation. If the command returns a result set — that is, if the command performs a Select statement — you can fetch the data using a data reader, which acts like a highly efficient read-only cursor.
 * Performing Database Operations Directly**

Security Note When using data commands with a CommandType property set to Text, carefully check information that is sent from a client before passing it to your database. Malicious users might try to send (inject) modified or additional SQL statements in an effort to gain unauthorized access or damage the database. Before you transfer user input to a database, you should always verify that the information is valid. A best practice is to always use parameterized queries or stored procedures when possible.Performing database operations directly has specific advantages, which include: Because of the stateless nature of Web applications and the corresponding issues associated with storing datasets, it is sometimes more practical in Web applications to work directly against the database.
 * Extra functionality As noted, there are some operations, such as executing DDL commands, that you can only accomplish by executing data commands.
 * More control over execution By using commands (and a data reader, if you are reading data), you get more direct control over how and when an SQL statement or stored procedure is executed and what becomes of the results or return values.
 * Less overhead By reading and writing directly to the database, you can bypass storing data in a dataset. Because the dataset requires memory, you can reduce some overhead in your application. This is especially true in situations where you intend to use the data only once, such as displaying search results in a Web page. In that case, creating and filling a dataset might be an unnecessary step in displaying the data.
 * Less programming in some instances In a few instances, particularly Web applications, there is some extra programming required to save the state of a dataset. For example, in Web Forms pages, the page is recreated with each round trip; unless you add programming to save and restore a dataset, it, too, is discarded and recreated with each round trip. If you use a data reader to read directly from the database, you avoid the extra steps required to manage the dataset.

More Information

Describe the two main benefits of using object data sources. Explain what reflection is and how it’s used with object data sources. Describe the way business objects can be used with object data sources.