Entity Framework – When to use what

In Entity Framework there are three ways of utilising the ORM, one enables you to get straight into coding (code-first), one enables you to rip a database into a new model (database-first) and the final one enables you to create a model independent of the database or your final code (model-first).

But, when should I use each of these methods?

Each method has its pro’s and con’s, and personally, I don’t really use code-first that often as it lends itself to a build where everything has been fully architected beforehand, and all you’re doing is building to spec. Something I rarely encounter, as the initial green field development is often a very agile process, especially if you’re utilising a TDD/BDD development cycle.

So what scenario would you legitimately use Code-First?

Say you have a very small micro-service to build, such as an auditing service, and you already know the database fields, and possibly the service will only know its connection at runtime. Code-First is an ideal solution, as it enables you to quickly knock out the code, leaving the spinning up and implementation of the database to the EF settings in config. The main drawback I find of Code-First is that if you’re database schema is not set in stone, a rebuild of your EF model will necessitate a destruction of the database. You can create a custom upgrade path, but this is rarely done. So, if you have a unchanging model, for a small data footprint, code-first is great.

Code-First is also great for proof-of-concept builds that may be knocked out in a day to show a particular prospect of development.

Database-First is obviously good for where your development is bound to a database schema which already exists and is quite complex. You can just use the EF designer in Visual studio to generate the model and get up and running very quickly. A database schema change will mean that the EF model will need to be recreated, but its generally no big deal as the database will be keeping its data integrity due to it being developed in its own development domain by DBAs or other colleagues.

Model-First would generally be used to map out a logical structure of the data which bridges both the system model and the database model. Say you wish to use a different paradigm of data design in the database to your model (flat-file DB with a relational ORM). It could also be the case that you are tasked with a data-design task where you need to develop a schema that satisfies the requirements of the database team and the architect, utilising a colourful database like Oracle or MySQL to fit.

I hope this helps your decide the approach to use when implementing Entity Framework in your work.

Take care

Moar Oculus hijinks

As part of the #hackcancer hackathon I ignored the urge to build another app, and instead dragged along my lanparty PC and my Oculus and set about creating an interactive game to introduce a conversation about cancer to teens and young children.

The result is NanoDocs (nanodoctors). Below are a few links to the source code and to a video of the work. Enjoy.

Github: https://github.com/neilhighley/nanodoctors
T
he Repo has a video with sound on it also. As soon a I have found a way to record the 2D output I’ll repost it.

D
evPost: http://devpost.com/software/nanodocs

Take care

 

Using Gulp on Windows machines

Doing any sort of Javascript FullStack development on windows seems to be quite niche, so you usually have to spin up a VM with Ubuntu (other distros are available) or remote into a mac.

Well, t is perfectly possible to do Javascript development on windows, even from command line.

Although tools like Webstorm take a lot of the command liney stuff away if you’re working with node you generally have to go there eventually.

In this brief overview I’m going to introduce the concept of a task runner, by using gulp. A task runner is something that runs actions on your code and in the Fullstack world, this generally means concatenating and transpiling.  In this example I’ll show you how to concatenate CSS style files into a single stylesheet file.

So lets get on getting on!

Firstly, install NodeJS.

Create a folder which to host your app, and I’ll be referring to this as root.

Open up a command prompt and then browse to your new folder (or shift right click in the folder 😉 ).

Create a build folder and a src folder within root.

>mkdir build, src

Now, initialise the node package manager

>npm init

This will create a packages file for your local node installation. Adding –save to any npm install will save the corresponding reference to the package.json. adding –save-dev will add to your development dependencies. This allows you to have a set of dependencies you may need for running tests, and another for running the app online if needs be.

Now we create the file which will store the instructions for gulp.

>type NUL > gulpfile.js

Time we installed gulp proper (use –g if you want to install globally)

>npm install gulp –save-dev

At this point, only the packages we need should be installed, so as I am just smooshing up some style files, we only need gulp-concat

>npm install gulp-concat –save-dev

Now we need to open up the gulpset1 file and add the concat dependency.

We can use notepad for this (or install a cmdline editor if you don’t wish to leave the console window (Nano is great for this)).

>notepad gulpfile.js

Add the following line;

var gulp=require('gulp'), concat=require('gulp-concat');

Now we add the gulp task to concatenate the styles files..but we have no files to concat.

So, rustle up three css files called style1.css, style2.cssstyle3.css with some css in there,  and place them in the src folder.

We can create the gulp task to concat them.

Open up the gulpfile.js file in notepad again.

Add the following;

gulp.task('styles',function(){
  return gulp.src('src/*.css')
    .pipe(concat('style.css'))
    .pipe(gulp.dest('build/'))
});

This will set the source for the task to be all the css files in the src folder, pipe them all to concat into style.css then finally move them to their destination in the build folder.

And finally, to run gulp, just call gulp with the task name.

>gulp styles

If you get a not found error at this point, you may need to install gulp and gulp-concat as global (-g).

You’ll now find a concatenated styles file in the build folder called style.css.

Let’s go deeper.. And without any water wings.

Say I want to use LESS rather than css.

I alter my gulpfile to add another task, called style-less

gulp.task('style-less',function(){
  return gulp.src('src/*.less')
    .pipe(less())
    .pipe(concat('style2.css'))
    .pipe(gulp.dest('build/'))
});

But, we haven’t got the less plugin installed yet.. so it won’t work (chortle).

To get the less compiler to work we need to install the gulp less plugin, then add it to the dependencies in the gulpfile.js.

We also need some more style.less pages. So create three using a variation of the following (careful with the minus sign, ensure you have everything spaced out);

@redcolor:#C00;
@redcolor_dark:@redcolor - #222;

.textred{color:@redcolor}
.textdarkred{color:@redcolor_dark}

Run gulp with the style-less task and you should get a style2.css with the compiled and concatenated less created CSS.

The same concept follows using a javascript transpiler.  Pipe the files to the transpiler and put them in the destination.

But wait! I hear you cry! I thought Gulp was automagical!?

Ok, it is and as you insist. here is a brief overview of gulp watch.

You create a new task, call it “style-watcher”, then add a gulp watch instruction inside the task.

gulp.watch("src/*.less", ['style-less']);

Then run the style-watcher class and go and edit the stylesheet and see the magic.

I’ll let you work out how to watch the normal stylesheet files.

And that is pretty much the fundementals of Gulp. As you can see you can nest tasks, and run tasks within tasks. Gulp, alongside Grunt are the cornerstones of fullstack, node based, development. Grunt runs things a little different however,  namely in the complexity of the config, and the number of plugins due to its maturity.
However, remember all that Grunt and Gulp do is run the actual modules, so technically you don’t need either and you can do just the same with an npm build script. But thats for another day. 🙂

Take care.

 

Coming soon to your company?

 

Available from : September 2016

I’m going to update this post with my availability, so if you want to chat, give me a call. My resume and contact details are on the main page at www.neilhighley.com. Mention you saw this post and I’ll call right back!

My current role involves mainly desktop development and Industrial Hardware controllers. It’s a little different from what I normally do, but it seemed a good fit for my current interests in IOT and they’re actively using TDD and BDD for their production pipeline.

Online and Offline Software Developer available for C#, HTML, CSS, JavaScript roles from the start of November. I have almost 20 years commercial web experience and consider myself multistack, having worked across the multiple industries in a lot of positions throughout software development and operations.

Blog address: http://blog.neilhighley.com

Github repo: http://github.com/neilhighley

Main Site: http://www.neilhighley.com

Devpost: http://devpost.com/neilhighley

Daily Rate available on request.

ASP.NET 4 / 4.5 / C# / LINQ / jQuery / Javascript / REST /  XML / Visual Studio 2013 / Resharper / Git / MSSQL  / IIS8 / Windows Server / MSSQL /  CSS3 / HTML / TDD / KnockoutJS / Angular 1.0 / Azure

PHP / WordPress / MySQL