C# Dapper Repository

New Direction

I had created a framework for a new Microsoft C# MVC web application at work using XML Serialization for the database to POCO conversion and straight ADO for data transmission. We had an architecture review and were asked to try Dapper instead. The word was that other units were using it.

We looked around but didn't find anyone using Dapper Micro ORM with a repository pattern, especially for hierarchical data, so last weekend I set out to create one that I'm sharing on my Dropbox:

https://www.dropbox.com/s/n5v1o2zdyhyvjyf/DapperDemo.zip?dl=0 

This link is to a .zip file. Hopefully have all the goodies you need to view the code and run it. 


The Zip Folder Contents


In the “Database” folder I have a document with some description about the solution. I also have 2 scripts to create and populate a simple database with 3 tables. 

There is a Main table, a Child table with a many to one relationship to the Main table, and an Auxiliary table with a many to one relationship to the Child table. That’s 3 levels of hierarchy!

In the Visual Studio solution, the functionality is exercised through a test project; there is no actual interface.

I’ve tried out the Dropbox solution on 3 PCs and it works but there can be some fiddling getting the database installed but I’m sure everyone here can figure it out. Once you have it installed, you may also need to change the connection string in the test project Apps.config file.


On the Visual Studio side, after you open the solution and it does it’s thing, you should go to the Microsoft Visual Studio > Tools > NuGet Package Manager > Manage NuGet Packages for Solution… and after it opens and refreshes, if you see a Restore button in the upper right of the window, click it to download and install the 4 NuGet packages used (Dapper, DapperExtensions, MSTest.TestAdapter, and MSTest.TestFramework).


Additional Notes


  1. I use the Dapper and Dapper-Extensions NuGet packages.
  2. I have a MainRepository and a ChildRepository that work on the 2 corresponding tables (and their children).
  3. For this example, I really only needed the MainRepository but I had extra hours to kill so created the ChildRepository for fun.
    1. I’ve also created and implemented a ConnectionFactory in the ChildRepository just for fun.
  4. The test project will likely fail if you do a “Run All”. If you do that, try restoring and then running the “Ordered Tests > MainRepositoryTests” or “Ordered Tests > ChildRepositoryTests”. If you haven’t seen them before, these are Ordered Tests.



Comments

Popular posts from this blog

A better mouse trap - issues with a Logitech Wireless Marathon Mouse M705

Implementing IDisposable Properly

Driver Training - cleaning up unused USB drivers