17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: <strong>FILESTREAM</strong> with ASP.NET and SilverlightLab 8: Using SqlFileStream in an N-Tier ScenarioFor many reasons, today's web applications are <strong>of</strong>ten architected to run across multiplephysical computers, referred to as tiers. Using <strong>FILESTREAM</strong> in a multi-tier designrequires attention to an important detail: in order to achieve the most benefit from<strong>FILESTREAM</strong>, it should be accessed using the Win32 streaming APIs. However, usingSqlFileStream also requires an active transaction in the <strong>SQL</strong> <strong>Server</strong> database. <strong>The</strong>consumer <strong>of</strong> the <strong>FILESTREAM</strong> data is most <strong>of</strong>ten the tier closest to the client – andfurthest away from the database. If the SqlFileStream API is used, this requires thatthis tier has at least some knowledge <strong>of</strong> the data store. In general, this violates goodarchitectural practices.In this lab, we will create a solution that makes the presentation tier as independent onthis database transaction as possible, while maintaining the performance benefits <strong>of</strong>feredby streaming access to <strong>FILESTREAM</strong> data. To fully understand the problem involved, let'sconsider the following example scenario. A web application must handle requests fromclients for which the response includes data stored in a <strong>SQL</strong> <strong>Server</strong> <strong>FILESTREAM</strong> column.<strong>The</strong> web application will be hosted on a front-end IIS web server, while the <strong>SQL</strong> <strong>Server</strong>database is hosted on a back-end server. <strong>The</strong> web application's developers aim to makethe application as efficient as possible, and want to use the SqlFileStream classand copy the stream's contents to the Response object's OutputStream (as shown inListing 5-11 as part <strong>of</strong> Lab 6). <strong>The</strong>y have four options, as shown below.• Start the SqlTransaction required for the SqlFileStream to use in thepresentation tier.This solution requires that the web application has intimate knowledge <strong>of</strong> thedatabase's configuration and details. Generally, one <strong>of</strong> the architectural goals <strong>of</strong> amulti-layered web application is to avoid this.• Start a SqlTransaction and create the SqlFileStream in the data access layerand pass the SqlFileStream back to the client application.This solution does not address the concern that the SqlTransaction must be activewhile the presentation layer uses the SqlFileStream object. <strong>The</strong> client, however, has204

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!