Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Building a LOB Gamification Service Administration Website: Web API CRUD

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Dec 2012CPOL3 min read 6.5K   3  
In this post, I would like to discuss adding, deleting, and updating data through the Web API.

At the end of the last post, I had shown a high level overview of some data design patterns such as Repositories and Unit of Work and ended with the browser fetching data from the database for the Application entity of the domain model. In this post, I would like to discuss adding, deleting, and updating data through the Web API.

Here is the fully coded ApplicationsController. Notice much of the code is simply delegating to the UoW which in turn simply delegates its work to the generic EFRepository.

clip_image001[1]

Deleting

I use Fiddler’s Composer tab to execute HTTP delete, put and post verbs. Looking at the ApplicationsController Delete method, I can execute the following in Fiddler.

clip_image002[1]

The result is a status code of 500 with the error message “The DELETE statement conflicted with the REFERENCE constraint FK_dbo.ProficiencyAction_dbo.Action_ActionId”. I cannot simply delete a single dbo.Application row in the database because there are foreign key constraints. To fix this, I am going to have to create a specialized version of Application repository and overwrite the Delete method. (After writing this, I found out the above is not 100% true. I did not HAVE to write the custom delete. Instead, I needed to revisit my domain model and make sure I have properly mapped out all the one to many relationships.)

Specialized Repository

Creating a specialized repository takes the following steps:

  1. In the “Data.Contracts” project:
    1. Add the interface to the specialized repository
    2. Update the UoW interface to use the specialize repository
  2. In the “Data” project:
    1. Add the concrete implementation of the specialized repository interface
    2. Update the concrete UoW class to reference the specialized repository interface
    3. Update Helpers/RepositoryFactories.cs

Below, you will find the supporting screenshots of these steps:

clip_image003[1]

1a: Add the interface to the specialized repository.

clip_image004[1]

1b: Update the UoW interface to use the specialize repository.

clip_image006[1]

2a: Add the concrete implementation of the specialized repository interface. Notice I have overridden the base Delete method.

The Application object is at the top of the gamification service object graph. Deleting an application basically requires deleting all the child objects which is almost every table. If you are reading this and know of a more efficient method to execute the Delete code above, please reply.”

clip_image008[1]

2b: Update the concrete UoW class to reference the specialized repository interface.

clip_image010[1]

2c: Update Helpers/RepositoryFactories.cs

With the following code changes, retrying in Fiddler produced no errors. I tried to “Get” that same Application using the Chrome browser, and received the following:

clip_image011[1]

The requested resource cannot be found. Not 100% proof it worked, but, I also checked the database Smile.

Updating

The “Put” method of the ApplicationsController expects an Application object. To make this easy, I first do a “Get” verb requesting a single Application and then copy and paste the JSON from the response.

clip_image013[1]

Loading this object up from the browser, you can see the name has been updated to AST3.

clip_image014[1]

Adding

The “Post” method of the ApplicationsController expects an Application object. Here is an example of adding a new application through the Web API using Fiddler’s Composer.

clip_image015[1]

Querying for this new Application using the browser confirms the new Application was created.

clip_image016[1]

The rest of the controllers require similar code but I think you get the point.

At this point, I have a database, domain model and a Web API that a client such as a browser can use to get data to display HTML using JavaScript.

Possible Future Discussion Points

The following list of topics is things I skimmed over, ignored altogether or think would be a good discussion. If you are interested in one of these, let me know.

  • Application specific Request/Response objects and best practices
  • Updating UoW to support Transactions

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer PROMODEL Corporation
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --