Generate Layered ASP.NET MVC Web Applications

By | July 15, 2013

I created a generator which generates Business and Data Access functionalities based on your domain model. I’ve build this generator + template because I think it makes sense even in smaller web applications to structure the code well. Also when the web application is very simple we never know which requirements we encounter in the future.
The advantages of my generator and template are:

  • Code generation based on domain model. (Saves you time!)
  • Flexibility. The generator creates partial classes. You can easily extend them.
  • Seperation of concerns.
  • Testability. No direct instantiation of objects. Abstraction with interfaces.
  • Concentrate on the core (Business, UI) of your application.
  • Running application in few minutes!

The solution template contains following projects:

WebUI ASP.NET MVC project.
Business Contains the domain model and the business logic.
DataAccess Data access with ADO.NET EF Code First approach.
Bootstrapper Boostrapping. (Dependency Injection with StructureMap)

T4 Generator Architecture

How does it works?

Download the solution from GitHub.
(You need only the “Templify” folder.”)
Open the SetupProject.cmd and set the “DestinationFolder”, “TemplateFolder” and “SolutionName” variables.
AppGen SetupProject.cmd
Now run the SetupProject.cmd file.

AppGen Domain Model
Open the solution and define your domain models (in this tutorial “Person.cs”) in the Business project under the folder “Domain”. Then execute the “” file. This file generates in this example following files:
Dmicic.WebShop.DataAccess (under .generated folder)

PersonConfiguration.generated.cs ADO.NET EF entity type configuration for Person domain model.
ObjectContext.generated.cs ADO.NET EF DbContext class.
PersonRepository.generated.cs Generic repository class for Person domain model.

Dmicic.WebShop.Business (under ApplicationService\.generated folder)

IPersonRepository.generated.cs Person repository interface. (Implementation is in Dmicic.WebShop.DataAccess project)
IPersonApplicationService.generated.cs Person application service interface contract.
PersonApplicationService.generated.cs Person application service implementation.

The base classes for the repositories and application services contains CRUD functionalities. If you need additional functions you can create a partial class for the appropriate domain model repository or application service.
Here is an example: I’ve created a partial interface “IPersonApplicationService” and a class “PersonApplicationService” in the folder Dmicic.WebShop.Business\ApplicationService.

public partial class PersonApplicationService
    public IEnumerable<Person> FindByFirstname(string firstname)
        return this.Repository.GetByCondition((person) => person.Firstname == firstname);

public partial interface IPersonApplicationService
    IEnumerable<Person> FindByFirstname(string firstname);

Now build your application. I recommend to inject the application services. In this example the services are passed as constructor parameters automatically. Here is an example of a simple ASP.NET MVC Controller.

public class SearchController : Controller
    private IPersonApplicationService Service { get; set; }
    private IUnitOfWork UnitOfWork { get; set; }

    public SearchController(IUnitOfWork unitOfWork, IPersonApplicationService service)
        this.UnitOfWork = unitOfWork;
        this.Service = service;

    public ActionResult Index()
        var foundPeople = this.Service.FindByFirstname("Darko");
        return this.View(foundPeople);

    public ActionResult SaveSomething()
        // Start transaction. (Optional)
        using (var tx = this.UnitOfWork.StartTransaction())
            var person1 = this.Service.Create();
            person1.Id = Guid.NewGuid();
            person1.Firstname = "Firstname 1";
            person1.Lastname = "Lastname 1";


        return this.View();

When you run the web application, the ObjectContext installs the Database and querying etc will work from the first compile! 😉

Have fun with the generator/template.
Feedback is appreciated!

  • Alejandro Segura

    hi darko, when try build solution , nuget script line in all solution folder throw error: return -1. one more thing, “Multiple directives were found in the template” <– which is the location of this file?. saludos

  • DaMi

    With VS.NET version are you using?
    I work with Visual Studio 2012 and it works without problems.

  • Alejandro Segura

    yes, i using vs 2012 , i thing there missing another extension/pugin in the tutorial

  • Alejandro Segura

    hi, solve this problem, the file” is not included in the Zip file. where do you implement UOW in this project? saludos

  • DaMi

    Did you installed T4 before?
    You can inject repositories in the controller but they don’t have a save function. and then you can inject a IUnitOfwork object which has a save function. If you look in the DI/bootstrapper you will see, that for each http request only one UOW context can exist.

    EDIT: You see the usage of the UOW in my blog post (step 5).

  • Alejandro Segura

    excellent! tks

  • Alejandro Segura

    excellent example. now, why transaction management should be in the frontend? and why not in the business layer or data access layer.

    Thank you.

  • dmicic

    You’re right. The transaction management should be there where the process flow is. In my demo, the flow is in the controller. But best practise is: Having the transaction management on top of the business layer. If you application service handles the whole flow for a specific use case then put the transaction management there.

  • Brahim Boulkriat

    Hi Darko , i’am a Tunisian software ingennering student and i’am builiding a social network with amazing and exiting features and ideas for the next Microsoft imagine cup .I will implement it using mvc 4 , Web API , SignalR, KnockoutJs and RavenDB.Can you help me in the technologie and architecture decisions and in the development ?

  • Ram Kumar Reddy

    could any one mail me a project which uses mvc as UI and having an 3 tier architecture ……plzzz