Friday, November 11, 2016

Creating Git repository on MS Team Fondation Server

Another note to self.

I was new to Git, so looked for an easy-to-follow instruction such as this one: http://rogerdudler.github.io/git-guide/.

Though the above does not talk about it, first you need to create the repository itself, in case you do not have one. I did not have one, so created one on TFS, and uploaded (“pushed”) the set of files that I want to version-control.

This tells you how to create a team project: https://blogs.msdn.microsoft.com/visualstudioalm/2013/01/30/getting-started-with-git-in-visual-studio-and-team-foundation-service/. You do so on Visual Studio. Though it does not say (I did not find it saying), I believe a team project configured to use Git is a remote master repository of Git.

First, I have cloned the empty repository onto my PC, and added the files that I want to version-control from now on.

Then issued these commands.

- git add *

- git commit -m "Commit message"

They resister and commit my files to the cloned local repository. Now my files are ready to be copied (“pushed”) to TFS, with the following command.

- git push origin master

This pushes the contents of my master branch to the remote repo, the origin (I think this is because the local repo has been cloned from it).

Now, my files are under Git version control. I can checkout, that is, clone them and start modifying in other IDEs which support Git, suh as AndroidStudio and Xcode.

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