BLOB support in CodeFluent Entities
I’d like to tackle today with an important matter when developing an application : the support of Binary Large Objects aka BLOBs.
A blob represents a binary data such as image, video, audio, document, etc that you need to manipulate through a client UI. Microsoft SharePoint is a good example because it handles out-of-the-box binary data that can be stored in Lists.
Many developers today and the majority of existing ORMs seems to handle BLOBs as a data type for a property. If handling with BLOBs is just declaring a property as byte then it’s ok, developers should know how to use it and yes, existing ORMs support it. But with CodeFluent Entities, the support of BLOBs is quite different. Let me explain why.
Using byte as a data type, means that the data stored in the corresponding database column will be complete read from the database to the application server memory. Imagine you have a 1GB video file, it means that this file will first be retrieved from the database and loaded into the web server memory (if we consider a user trying to display the video through his browser for instance) and sent back to the end user browser. If we are seeking for performance and scalability, this might not be the right thing to do in your development.
With CodeFluent Entities, Blob management natively relies on streaming even if the generated code also supports other BLOB management mechanism.
Note: for those who needs to understand how that works, I’ll invite you to read this document I wrote several years ago with our CTO Simon Mourier.
CodeFluent Entities provides advanced Binary Large Objects types from the designer, up to the UI layers we support. Supported binary types are: video, audio, file, picture and blob. With all these types, the product defines optional metadatas such as FileName, ContentType, Attributes, Size, LastWriteTime, LastAccessTime and CreationTime. Like any other ORMs (as a remainder, CodeFluent Entities is NOT an ORM) the product also support the simple byte that should be used for small binary data.
For all these types we support:
- Streaming: the runtime classes provided support streaming from any client type (web, smart client, WCF) to the server. Nowhere is stored the full array of bytes, to avoid hitting .NET Large Object Heap. On the data layer, the data reader uses the SequentialAccess CommandBehavior, on web layer, we use TransmitFile to save ASP.NET memory, etc.
- Chunking: we generate automatically the code for a reusable BITS server (supporting the BITS protocol 1.4 for download and upload, without dependency on IIS).
- Caching: blobs can be cached on the server (to avoid hitting database) and on the client. We support HTTP caching client and server.
- Displaying: we provide ASP.NET custom control to view, download and upload these blobs without writing code. We provide TypeConverters and WPF ValueConverters as well to ease UI databinding.
For instance, let’s say you want to store and display videos in an ASP.NET application, this is how to simply add a Presentation property of type video on a Contact entity designed in the model:
After generation, in the persistence layer the corresponding Contact table will contain extra columns:
CREATE TABLE [dbo].[Contact] ( [Contact_Id] [int] IDENTITY (1, 1) NOT NULL, (…) [Contact_Presentation] [image] NULL, [Contact_Presentation_FileName] [nvarchar] (256) NULL, [Contact_Presentation_ContentType] [nvarchar] (128) NULL, [Contact_Presentation_Attributes] [int] NULL, [Contact_Presentation_Size] [bigint] NULL, [Contact_Presentation_LastWriteTimeUtc] [datetime] NULL, [Contact_Presentation_LastAccessTimeUtc] [datetime] NULL, [Contact_Presentation_CreationTimeUtc] [datetime] NULL, (…) GO
As you can see, metadata properties are automatically added along with blob types. Stored procedures are updated to save blobs by chunk since they can be potentially large files, and the Contact class has a Presentation property of the CodeFluent.Runtime.BinaryServices.BinaryLargeObject type.
This is an example of how to use the runtime-provided ASP.NET controls to display, update, or delete blobs in your web site, without a single line of code:
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="false" DataKeyNames="Id,RowVersion" DataSourceID="DataSource"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" /> <asp:BoundField DataField="FirstName" HeaderText="FirstName" /> <asp:BoundField DataField="LastName" HeaderText="LastName" /> <cfwc:BinaryLargeObjectField HeaderText="Presentation" ContentDisposition="inline" dataField="Presentation" /> </Columns> </asp:GridView>
The above code considers that we have designed in the model a Contact entity with the following properties: Id, FirstName, LastName, Presentation.
BinaryLargeObjectField is a utility class provided in the CodeFluent Web Runtime.
As you can see, CodeFluent Entities offers a powerful mechanism to handle BLOBs based on streaming with 0 complexity and 0 lines of code.