Introduction

In this step-by-step article we will discuss all about Create, Read, Update, Delete (CRUD) operations with the Web API using the Repository pattern and using the most popular ORM, FluentNHibernate. In this article we will not go into depth about how to consume WEB APIs.

I recommend reading following article to learn more if you want to use the Web API from a third-party as a client:

Why Web API

While preparing this whitepaper I asked myself the question "Why Web API?". In simple and shorter words there are numerous things why we are doing with the Web API and not with WebServices, WCF or REST WCF.

I categorized these as follows:

Although we will not dig deely into the preceding, that is beyond our main topic, but I would like to recommend reading the following to understand these terms better:

Pre-requisites

To work with the source code and taste the flavor of ASP.NET WEB API, you should have:

Step 1: Create ASP.NET WEB API Project

To start we are using the following simple ASP.Net project:

Step 2: Visiting Folder Structure

After the preceding step, we will see that our project template is ready and the following is the default folder structure provided by the template:

Step 3: Installing FluentNHibernate Support

To install FluentNHibernate support to the project:

Wait until FluentNHibernate is installed.

Step 4: Working on Models, Mappings and Repository Pattern

In this step, we will create the Model and its mapping and will add a repository.

Adding Models and their Mappings

Adding Model Mappings

Adding Fluent NHibernate support to the project

In this class we need to configure NHibernate and build all sessionfactory, so our application will interact with the database:

  1. private static void CreateSessionFactory()
  2. {
  3. _sessionFactory = Fluently.Configure()
  4. .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString).ShowSql)
  5. .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ServerData>())
  6. .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
  7. .BuildSessionFactory();
  8. }

We are not going to discuss all that stuff in details since they are beyond the scope of this article.

We arw now ready to start playing with our Repository.

P.S.: refer to the msdn for more details of the Repository Pattern.

Step 5: Add WEB API Controller

In this step, we will add an API controller and will add all the necessary resources.

P.S.: Please note that in this article/demo we are not going to implement any DI pattern or Inversion of Control (IOC) framework.

We are all set to go now:

  1. public IEnumerable<ServerData> GetServerData()
  2. {
  3. return serverDataRepository.GetAll();
  4. }

In the preceding defined method, why we addd the "Get" suffix. It is a good practice to add "Get" with your method name, by convention it maps to a GET request.

This method has no parameter, so you can say this maps to a URI without an "id" parameter.

Will the preceding method work? Of course, it works but it's not an ideal or say it's not quite complete, why?

In the preceding method, we missed the following from the HTTP response:

From the preceding, we can understand that the WEB API matches this method to a PUT request. The preceding has two parameter ids and serverdata. So, it is taken from the URI path and the serverdata is deserialized from the request body.

P.S.: By default the Web API framework takes simple parameter types from the route and complex types from the request body.

Step 6: Set default result output type

We need the result in JSON by default, let's add the following line either in the Global.asx.cs file or in the file where you are registering routes.

  1. //return JSON response by default
  2. config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

Step 7: Testing WEB API

Here I used the "Poster for Firefox" plugin to test the output. You can directly download this plugin from the Plugin directory of Firefox:



Just enter the URI and press GET or whatever you want to play, you will get the output accordingly.



Conclusion

I hope you enjoyed this article. I tried to make this as simple as I can. If you like this article, please rate it and share it to share the knowledge.