Introducing generator-efrepo for yeoman

Mar 17, 2015

In this blog post I will show you how to use the new generator-efrepo that I wrote couple of days ago. The idea behind this Yeoman generator is to automate some of the boaring tasks while starting on a brand new project such as,

  • Creating Model classes
  • Adding EntitiFramework NuGet package
  • Creating EntitiFramework Context class
  • Creating Repositories and UnitOfWork on top of your context
  • During public demos you have to create multiple class and integrate EF for data access

This blog post assumes that you have basic idea about the Yeoman generator itself along with EntityFramework CodeFirst and Repository/UnitOfWork design patterns. If not,please do some basic reading on here before you start using this generator. Alright, let's get started.

Installing Yeoman(If you do not have it already)

Then make a directory where you want the Yeoman generator to create the EntityFramework project. Navigate to that folder in your command line tool.Make sure you have nodejs and npm installed on your system by runing following commands.

Check nodejs $ node -v

Check npm $ npm -v

Install Yeoman

$ npm install -g yo

Install generator-efrepo

Once you have Yeoman generator installed, now you can bring in other generators created by the community. In this case let's install the efrepo generator package from npm.

To install generator-efrepo from npm, run:

$ npm install -g generator-efrepo

You should see the respective modules in node_modules folder.

Alright, at this point we have npm moudles installed and ready. Next, let's start creating the Project using generator-efrepo project. Few things to note how the generator operates and names your project.

  • Generator will create two projects
  • .Model project which will have all the classes
  • .Data project which will have the Factory, Repositories and UnitOfWork
  • You do not need to provide the .Model and .Data to generator.If you want the project to be created as MyForum.Data , just pass MyForum as the project name for first question.

Ok, let's fire up the generator.

$ yo efrepo

The generator will ask you 3 questions. - Project Name - Model classes seperated by comma (Customer,Order,Vendor) - EntityFramework Context Class name

Alright, that's all. You should see the two project folders .Model,.Data and a solution file(.sln). Open the solution the file using Visual Studio.

Things to remember

Restore Packages

The actual packages are not part of the template generation process. So once you ope the solution, restore the packages.

Readd reference of th .Model project to Data project

Model classes are just Empty classes

Generated classes are just empty classes with no properties defined, use CodeFirst to start creating properties for your classes.

Using the Repository and UnitOfWork

  • The project is inspired by SocialGoal , so how to further use this repositories can be understood form that repository

I will explain that workflow here in brief steps but you can use the above project as your reference.

Update Category and Forum classes with basic properties.

Update Category and Forum classes with basic properties.

Add empty MVC application. Again I am demoing how to use the generated projects so I will try to keep things simple.

Start using the Repositories

Following code demonstrates how you can make use of the generated data access repositories and services. By no means this is the recommended way. Again use the reference project(SocialGoal) to see how to leverage Dependency Injection for this use case.

Adding EntitiFramework in Web and Project

The last step is to add EntitiFramework NuGet package in the new web project along with the ConnectionString. Note how ConnectionString name matches the project name you gave to your yeoman generator.

Run the application and verify all works

And that's all.

Again, the generator is hosted at on GitHub and can be found here.