Dan's Green Shoes

Got my green shoes on!

Building a LOB Gamification Service Administration Website: Web API CRUD

2 Comments

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

About these ads

Author: Dan Hickman

Chief Technology Officer ProModel Corporation

2 thoughts on “Building a LOB Gamification Service Administration Website: Web API CRUD

  1. <>

    Hi Dan. I would recommend create a Delete Stored Proc and mapping that to your entity delete method in your EF Model. I think this is the most efficient way on many levels.

    Thanks so much for this article.

    Yes, let’s go further and handle transactions, next!

    -dennis

  2. Everyone loves what you guys tend to be up too. This type of clever work and reporting!
    Keep up the wonderful works guys I’ve added you guys to our blogroll.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 446 other followers