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.
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.
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. )
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.
1a: Add the interface to the specialized repository.
1b: Update the UoW interface to use the specialize repository.
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.”
2b: Update the concrete UoW class to reference the specialized repository interface.
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:
The requested resource cannot be found. Not 100% proof it worked, but, I also checked the database .
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.
Loading this object up from the browser and you can see the name has been updated to AST3.
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.
Querying for this new Application using the browser confirms the new Application was created.
The rest of the controllers require similar code but I think you get the point.
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