Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Building a LOB Gamification Service Administration Website: Web API CRUD

, 23 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

a. Add the interface to the specialized repository.

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

2. In the “Data” project:

a. Add the concrete implementation of the specialized repository interface.

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

c. 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 and 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)

Share

About the Author

DapperDanH
Chief Technology Officer PROMODEL Corporation
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 23 Dec 2012
Article Copyright 2012 by DapperDanH
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid