Automatically generating Repository and Unit Of Work classes

In my projects I usually access all my data through a repository. The utilization of the repository pattern provides a number of benefits for your code:

  1. Reduces duplicate data access code.
  2. Data access code can be isolated/faked for Unit Testing.
  3. Allows you to swap other ORMs, databases or data access code without affecting other parts of the system.

The only draw back to using a repository pattern is that it can be cumbersome to implement (especially if you have a lot of models), to help with that I’ve created a NuGet package which will generate Repository and Unit of Work classes with basic Get, Get All, Delete, Update code implemented in an abstract Generic Repository (so they can be freely overridden).

The NuGet package is available here:

DotNet
https://www.nuget.org/packages/RepositoryGenerator/

 

The NuGet package will add a RepositoryGenerator.tt which will generate the following classes:

Repository.cs: An abstract class with basic implementation of Get, GetAll, Delete and Update

IRepository.cs: An interface for the abstract class

UnitOfWork: A unit of work class for committing all changes at once.

IUnitOfWork: The interface for the unit of work.

In addition to these classes, a repository and interface will be generated for each one of your entities allowing you to extend beyond the basic functionality provided by the generic repository.

With the Repository Generator NuGet package you should be up and running with a repository within a just few seconds!

Enjoy

Omar Elabd

Omar Elabd

Omar Elabd is a software developer specializing within the .NET Framework. He has experience in developing Web and Desktop based applications using ASP.NET, MVC, WebAPI and WPF.
Omar Elabd
  • Escargot

    That’s a nice work. Thank you!

  • Didier GEORGES

    hello Omar,
    Great template, indeed !

    Thanks a lot for sharing this !
    How would you please avoid creating dedicated repositories for inherited abstract classes in the model ?

    • http://www.omarelabd.net/ Omar Elabd

      Hi Didier,

      You can modify the “RepositoryGenerator.tt” file as you like. If I understand you correctly, you want to remove the interfaces generated with the repository classes? If so you can delete line 186 and it should no longer generate that file.

      • Didier GEORGES

        Hi Omar,
        Actually i allready tailored your template to my own needs :), but i cannot see how/where to detect if a class is abstract or not.
        the idea is to provide dedicated Repositories for end-of-line entities, not for mother BaseEntity.

        Moreover, do you think (with you template as a base) i could explore properties of the classes ?

        i have done that once with a EDMX based template, but i prefer to stay whith Code-First…

        anyway thaks again for sharing !

        • http://www.omarelabd.net/ Omar Elabd

          Hey Didier,

          The class that is returned is a CodeClass ( https://msdn.microsoft.com/en-us/library/envdte.codeclass.aspx )

          So you can call isAbstract to detect whether or not the class is abstract. You should be able to call all the other methods listed in the link above.

          Hope that helps

          • Didier GEORGES

            Thanks a lot Omar !