Friday, October 28, 2016

Another my own memo about MVC Scaffolding

First of all, I was not familiar with the word “scaffold”. An online dictionary defines it as “a temporary structure for holding workers and materials during the erection, repair, or decoration of a building.” My understanding of it at this moment is a tool to generate other constructs of a MVC project, such as views and controller, from a model class.

So let us start some tries and errors.

First I create my model class.

image

Then, give a try to the built-in menu to create the controller.

image

image

image

NOTE: I choose to create the new data context class.

Give it a try to see if the site works for the model. It fucntions nicely to me. Then what is this New Scaffolded Item?

image

Let us give it a try.

image

This appears does exactly the same as the Add > Controller...

The next and finally, there is a NuGet package called MvcScaffolding.

It can be used in the Package Manager Console as follows.

PM> Scaffold Controller Team -DbContext TeamContext

NOTE: If omitted, and none exists, a DbContext class is created with the project name.

The result effectively is the same. It creates the controller and views. Though, the code generated are different (taste of the author perhaps)

TIP: This tool does not like my model class as it is shown above, and produces an error: Get-PrimaryKey : Cannot find primary key property for type 'WebApplication9.Models.Event'. Multiple properties appear to be primary keys: Id, EventId

It appears that it does not recognize the [Key] annotation, and takes as the primary key either the column ID or {classname}ID. Read more: https://social.msdn.microsoft.com/Forums/en-US/2cef7375-e832-4f98-9084-4bf0558e57f1/getprimarykey-cannot-find-primary-key-property-for-type?forum=adodotnetentityframework

In the actual project that I work in, for this Event model, I need ths EventID, which uniquely identifies an Event, and is defined and assigned outside of my peoject. Apart from it, there need to be another ID which would be populated automatically when it is created into the corresponding database table. I named the first ID as EventID because It has been calld as such in the existing system, but then stepped on the land mine.

There seems no workaround. I renamed it as _EventID.

Anyway, one advantage of this package over the built-in tool is that it could creates the Repository class.

What is a Repository class?

http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/ reads: TeamController reads and writes the data in SoccerSiteContext directly. That’s fine in many simple scenarios, but if you want to decouple your controller logic from persistence logic a little (e.g., so that you can write clean unit tests for the controller), you may prefer to reach your data through an interface.

And this seems is the standard architecture for the next version of the Framework; ASP.NET Core MVC. I understand that the framework that is developped as ASP.NET 5 is now renamed as ASP.NET Core 1.0.

The MvcScaffolding creates the repository class, and then the contoller as follow.

PM> Scaffold Controller Team -DbContext TeamContext -Repository