Saturday, 30 April 2016

Integrating Fluent Validation in Web API using Autofac

Create New Project

Install Necessary Packages

  Install-Package Autofac
  Install-Package Autofac.WebApi2
  Install-Package FluentValidation
  Install-Package FluentValidation.WebApi
  Install-Package Microsoft.AspNet.WebApi
  Install-Package Microsoft.AspNet.WebApi.Owin
  Install-Package Microsoft.Owin.Host.SystemWeb
  Install-Package Owin
  Install-Package Newtonsoft.Json
 

Create Partial Startup Classes

Partial class is just splitting class file into two or more files, and all parts are combined when the application is compiled.

It is used in a situation

  • When working on large projects, spreading a class over separate files enables multiple programmers to work on it at the same time.
  • When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when it creates Windows Forms, Web service wrapper code, and so on. You can create code that uses these classes without having to modify the file created by Visual Studio.
  • To split a class definition, use the partial keyword modifier

While adding this class our project structure looks like.

Startup.Autofac.cs

Startup.WebApi.cs

Startup.cs

Add Infrastructure classes

Now our Project structure looks like

AutofacValidatorFactory.cs

AutofacWebModule.cs

ValidateFilterAttribute.cs

Add Modal Validation and Controller

Now our Project structure looks like

Controller

Model and Model Validation

Finally test our code




Thanks,


Download Code

Sunday, 24 April 2016

(Part 4 of 4 ) Accessing FILESTREAM Tables

Create a FILESTREAM-enabled table

Add Rows with a simple text BLOB using CAST

Delete FILESTREAM data

(Part 3 of 4 ) Creating FILESTREAM Database

Create database with FILESTREAM filegroup/container

Add a FILESTREAM filegroup to the database

(Part 2 of 4 ) Enabling FILESTREAM for Windows and SQL Server

FILESTREAM must be enabled twice, once by the Windows administrator and then again by the SQL Server administrator. The reason for this is because FILESTREAM is somewhat of a hybrid feature. Yes it's primarily a SQL Server feature, but because of its tight integration with the NTFS file system on Windows, it requires a file system filter drive to be installed, which is something only a Windows administrator can do. Typically the Windows administrator prepares an NTFS volume and enables FILESTREAM, and then the SQL Server administrator enables FILESTREAM separately at the server instance level before creating a FILESTREAM-enabled database. This same access level must be specified in both places.

Of course, this is not an issue if you are one person that acts as both the Windows and SQL Server administrator, but otherwise, both need to get along and they need to agree on the access level that FILESTREAM should be enabled for.

Enabling FILESTREAM for Windows

Enabling FILESTREAM for SQL Server

(Part 1 of 4 ) Why to use FILESTREAM or FILETABLE in SQL Server

FILESTREAM enables SQL Server-based applications to store unstructured data, such as documents and images, on the file system. Applications can leverage the rich streaming APIs and performance of the file system and at the same time maintain transactional consistency between the unstructured data and corresponding structured data.

FILESTREAM integrates the SQL Server Database Engine with an NTFS file system by storing varbinary(max) binary large object (BLOB) data as files on the file system. Transact-SQL statements can insert, update, query, search, and back up FILESTREAM data. Win32 file system interfaces provide streaming access to the data.

FILESTREAM uses the NT system cache for caching file data. This helps reduce any effect that FILESTREAM data might have on Database Engine performance. The SQL Server buffer pool is not used; therefore, this memory is available for query processing.

FILESTREAM is not automatically enabled when you install or upgrade SQL Server. You must enable FILESTREAM by using SQL Server Configuration Manager and SQL Server Management Studio.

 EXEC sp_configure filestream_access_level, 2
 RECONFIGURE
 GO
  
To use FILESTREAM, you must create or modify a database to contain a special type of filegroup.
 CREATE DATABASE WebApiFileTable
 ON PRIMARY
 (Name = WebApiFileTable,
 FILENAME = 'E:\filestreamsql\FTDB.mdf'),
 FILEGROUP FTFG CONTAINS FILESTREAM
 (NAME = WebApiFileTableFS,
 FILENAME='E:\filestreamsql\FS')
 LOG ON
 (Name = WebApiFileTableLog,
 FILENAME = 'E:\filestreamsql\FTDBLog.ldf')
 WITH FILESTREAM (NON_TRANSACTED_ACCESS = FULL,
 DIRECTORY_NAME = N'WebApiFileTable');
 GO
  
Then, create or modify a table so that it contains a varbinary(max) column with the FILESTREAM attribute.
 USE WebApiFileTable
 GO
 CREATE TABLE WebApiUploads AS FileTable
 WITH
 (FileTable_Directory = 'WebApiUploads_Dir');
 GO
   
After you complete these tasks, you can use Transact-SQL and Win32 to manage the FILESTREAM data.
 INSERT INTO [dbo].[WebApiUploads]
 ([name],[file_stream])
 SELECT
 'NewFile.docx', * FROM OPENROWSET(BULK N'd:\kk.docx', SINGLE_BLOB) AS FileData
 GO

 select * from WebApiUploads

Friday, 22 April 2016

Easy and Smart Modal Validation in Web API 2.0



Today i have written a custom ValidateFilterAttribute that will eliminate some unnessary code in API Controller and save our developing time too. We will simply try to handle all validation from one common piece of code rather then writting every single validation in a controller.
I have used OnActionExecuting(HttpActionContext actionExecutingContext) as this allows me to remove the boilerplate if (!ModelState.IsValid) return from the methods.