Saturday, May 5, 2018

A minimal web application structure with technologies like node.js, typescript, express

Recently, I have been working the lot with Node.js and TypeScript. And I absolutely love TypeScript due to its features. I was actually looking for a boilerplate code to start which should be very easy to understand. But there was no code there. So I decided to create a minimum web application structure. I have used it in some project and I was quite happy with it.

So I thought why should I make it open source so that people can also get benefited by this. So here I am presenting A minimal Web Application Structure with technologies like Node.js, TypeScript, and Express.

You can find the complete source code on Github at following location - https://github.com/dotnetjalps/minimum-nodejs-typescript-express

How to Run this Project:

To run this project you need to first install all the NPM packages via the following command.
npm install 
Then you can run this project with the following command.
npm start

Directory Structure Of Project:

  • App.ts – Typescript file for creating express application class and where we have initialized the application.
  • routes.ts - Typescript files for creating all the routes under Init() Method.
  • package.json - Contains all the packages and dev dependencies required for this application. You can add more as your requirement.
  • tsconfig.json - Where all the typescript configuration is there and we converting typescript into ES5.
  • Controller Folder - Contains all the classes for the controller of the express application
  • tsconfig.josn  - Contains all the rules for TypeScript linting.
I’m looking for constructive feedback on this and I’m hoping that I will get it.
Share:
Friday, April 27, 2018

Flexible Object Mapping in Entity Framework Core 2.0

Entity Framework 2.0 is out for some time. If you don’t know Entity Framework 2.0 is a lightweight, extensible and Cross-Platform Object-relational mapper from Microsoft. It is a new version of Entity Framework which is now cross-platform and can now run now operating system like Linux, Mac etc.With Entity Framework Core 2.0, There are tons of ways to create a table and we are going to see one of that. In this blog post, We are going to see how we are going to see how we can create a field for private fields in the table.

So what we are waiting for. Let’s get started.

Flexible Object Mapping in Entity Framework Core 2.0:

So in this example, We are going to create a Console Application like following.

new-project-entity-framework-core

Once we are done with Creating Application We are going to insert NuGet Package for EF Core like below.

nuget-package-entity-framework-core

You can also install it via running following command.
Install-Package Microsoft.EntityFrameworkCore -Version 2.0.2
Here we are going to install SQL Server Express as a database so we need to install Nuget Package for that also.

enttity-framework-core-sqlserver-nuget-package

You can also install via running following command.
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.2
Now it’s time to write some code. First of all, We are going to create A model called Student and In that, we are going to have two private fields.
namespace EFCore2Mapping
{
    public class Student
    {

        public int StudentId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }


        private string _standard;
        private string _division;


        public void AssignStandard(string standard)
        {
            _standard = standard;
        }

        public void AssignDivision(string division)
        {
            _division = division;
        }

        public string GetStandard()
        {
            return _standard;
        }

        public string GetDivision()
        {
            return _division;
        }
    }
}

Here in the above code, You have seen that I have created few fields for Student Model. If you see it carefully, You can see that there are two private fields _standard and _division.  And there are two methods for each field to get and set private variables.

Now let’s write our Entity Framework Data Context. That’s where the Magic going to happen.
using Microsoft.EntityFrameworkCore;

namespace EFCore2Mapping
{
    public class StudentContext : DbContext
    {
        public DbSet<Student> Student { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Data Source=Your Server;Initial Catalog=Student;User ID=sa;Password=Jalpesh@123");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Student>().Property<string>("Division").HasField("_division");
            modelBuilder.Entity<Student>().Property<string>("Standard").HasField("_standard");
        }
    }
}

Here in the above code, If you see I have created a dataset for our student model that is a standard way to create a table for the map. There is another method OnCofiguring for giving database connection string. There are is another method called OnModelCreating which Entity framework core uses to create tables. Here If you see that I have written code to map private fields to Table Fields so that two fields Division and Standard will be created with tables.

Now let’s run the migration to create a table like following.

migration-to-create-private-fields-in-table-entity-framework-core

You need to go to the Nuget Package Manager Console and then run the following command.
Add Migration Initial Create
Once you are done with it. It will create the tables in the database like following.

fields-created-in-SQL-Server

Now let’s write some code insert data in the table. So following is code for the same.
using System;
using System.Collections.Generic;
using System.Linq;

namespace EFCore2Mapping
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Student> ExistingStudents = new List<Student>();

            Student student = new Student
            {
                FirstName = "Joe",
                LastName = "Tailor",
            };
            student.AssignStandard("5");
            student.AssignDivision("A");

            using (StudentContext studentConext = new StudentContext())
            {
                ///Adding a new Student;
                studentConext.Student.Add(student);
                studentConext.SaveChanges();


                ///Retriviting newly added student
                ExistingStudents = studentConext.Student.ToList();
                
            }

            foreach(Student s in ExistingStudents)
            {
                Console.WriteLine($"Student Id: {s.StudentId}");
                Console.WriteLine($"First Name: {s.FirstName}");
                Console.WriteLine($"Last Name:{s.LastName}");
                Console.WriteLine($"Standard:{s.GetStandard()}");
                Console.WriteLine($"Division:{s.GetDivision()}");
            }
        }
    }
}

Here you can see that in the above code, I have created Student called Joe Tailor and then I have saved it in the database and then I have written the code for fetching the student data and print it via for loop.

When you run this application It will show like following.

output-entity-framework-core-flexible-column-mapping

That’s it. Here you can see that It’s very easy to Manipulate the Columns with Entity Framework core 2.0. Hope you like it.
This complete blog post source  code available on github at - https://github.com/dotnetjalps/EFCoreFlexibleObjectMapping
Share:
Saturday, April 7, 2018

Register for Global Azure Bootcamp 2018–from Ahmedabad User Group

Friends, We are back with new events again. We are going to host Global Azure Bootcamp 2018.

bootcamplogo


About Global Azure Bootcamp:

If you don’t know what is Global Azure Bootcamp then it is a worldwide Event for Azure.  All around the world user groups and communities want to learn about Azure and Cloud Computing!

On April 21, 2018, all communities will come together once again in the sixth great Global Azure Bootcamp event! Each user group will organize their own one-day deep dive class on Azure the way they see fit and how it works for their members. The result is that thousands of people get to learn about Azure and join together online under the social hashtag #GlobalAzure!

Ahmedabad User Group is also organizing the same. This time we want to put it on a bigger level. So there will be lots more to learn, A great opportunity to network with like-minded people. Best Part you will get a chance to meet lots of awesome speakers. It has also got lunch included!.

I am also one of the speakers and will be presenting About Azure Active Directory.

So what you are waiting for go Grab a chance to meet and learn about Azure Cloud Platform.
Here is a link from where you can register for Global Azure Bootcamp 2018.

https://www.eventbrite.com/e/global-azure-bootcamp-2018-by-ahmedabadusergroup-tickets-40757803688


I am hoping to see you everybody there!!. Go Grab before it gets full!.
Share:

Video Recording : Webinar on ASP.NET Core on Linux

I have done a Webinar for DotNetTricks.com about ASP.NET Core on Linux. There were lots of curiosity about it and lots of people asked for the recording of Webinar so here we go Following is a link of recording of Webinar for ASP.NET Core on Linux. You can watch the full webinar on youtube at following.

https://www.youtube.com/watch?v=Hf0F7nZCTXM&t=706s



I would also like to Thanks Shailendra and Whole DotNetTricks team for the having me on this webinar.

Thank you. Stay tuned there were lots of stuff coming from ASP.NET core and Node.js.
Share:
Saturday, March 10, 2018

Solution: (Error)Unable to Correct problems, You have held broken packages–Install .NET Core on Ubuntu

Recently, I have been using Linux(Ubuntu) machine on my virtual box quite a bit and as  Microsoft.NET Developer I want to install .NET Core on Ubuntu. So I tried following the instruction from below link to install .NET core on Ubuntu 16.04.

https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x

But I was not unable to install .NET Core and I was getting following errors.

The following packages have unmet dependencies:
  dotnet-sdk-2.1.4 : Depends: dotnet-runtime-2.0.5 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
You can see the same error in the following screenshot.

error-ubuntu-net-core-214

I was a little bit curious about that and then I have read carefully the documentation mentioned on the above link and I have found that .NET Core require following require following Ubuntu distributions as Prerequisites.

  • libunwind8
  • liblttng-ust0
  • libcurl3
  • libssl1.0.0
  • libuuid1
  • libkrb5-3
  • zlib1g
  • libicu52 (for 14.X)
  • libicu55 (for 16.X)
  • libicu57 (for 17.X)

Most of the time it should be installed by default with sudo apt-get update and sudo apt-get upgrade command but sometimes it does not. So what I did install all these distributions one by one via sudo apt-get install command and in my case, I was not having liblttng-ust0 distribution on my machine. So I have installed it via the following command.

sudo apt-get install liblttng-ust0
You can also see below screenshot for the same.

install-ubuntu-distribution-for-netcore

Then I was able to install .NET Core on my machine via following above link on documentation.

install-netcore-ubuntu

And now it’s running .NET Core v 2.1.4 on my Ubuntu VirtualBox as you can see it in below dotnet version command.

net-core-version-command

I was able to solve my problem this and so I thought it will be a good idea to share it on my blog so it will help people who are having the same problem.

Stay tuned for more there is a lot more coming for asp.net core and node.js on Linux on future blog posts.
Share:

How to install Robo3T(Formerly RobMongo) on Ubuntu

Robo3T formerly known as RobMongo is one of best GUI tools for the managing and querying MongoDB database. I have been using it quite often for creating applications with MongoDB and Node.js. Recently a new version made available but there were no instructions for installing it on Ubuntu. So I thought it would be a great idea to write a blog post about it.

To install it. First, we need to open terminal and download tar.gz file from official site with the following command.
wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-linux-x86_64-3e50a65.tar.gz
robo3t-install-download-ubuntu

Once download will complete we need to extract tar.gz file with following command
tar -xvzf robo3t-1.2.1-linux-x86_64-3e50a65.tar.gz
extract-tar-file-robo3t-robmongo-ubuntu

As now we have extract it into the folder we need to move that under /usr/local/bin directory to make it available for all the users. So todo that we need to first create robo3t directly under /usr/local/bin folder with following command.
sudo mkdir /usr/local/bin/robo3t
mkdir-robo3t-robmongo-ubuntu

Then we need to move all the files of extracted folder into robo3t folder under /usr/local/bin directory with following command.
sudo mv  robo3t-1.2.1-linux-x86_64-3e50a65/* /usr/local/bin/robo3t
move-content-to-robo35-ubuntu

Now, We need to give permission to newly created directory with following command.
sudo chmod +x robo3t ./robo3t
permission-robo3t-robmongo-ubuntu

Once you are done with it. You are ready to use robo3t. You can run it from command line from usr/local/bin directory via following command.
./robo3t
run-robo35-command-line

And it will run the Robo3t like following.

robo3t-on-ubuntu

That’s it. Hope you like, Stay tuned for more!!.
Share:
Wednesday, March 7, 2018

How to install Visual Studio code on Ubuntu

Recently I presented a Webinar on ASP.NET Core on Linux. Here I have displayed how we can use Visual Studio Code a Free Open Source Editor from Microsoft to develop application. One of frequently ask question was How I can install Visual Studio Code on Linux. Since lots of People are using Ubuntu as Development Machine I thought it will be a good idea to write a blog post about how to install Visual Studio code on Linux(Ubuntu Machine).

So first thing we need to do it open a terminal in Ubuntu and then we need to type following command one by one.
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
Install-Visual-Studio-Code-Ubuntu

Then you need to run the following command to move gpg file download via Curl to etc/apt folder.
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
Install-Visual-Studio-Code-Ubuntu2-move-folder

Now, You need to run following command.
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
package-cache-insert-vscode-ubuntu-linux

After that, you need to run Apt-get Command to update package cache and Apt-get install command to install visual studio code.
sudo apt-get update
sudo apt-get install code
pakcage-install-visual-studio-code

That’s it. You are done with installing Visual Studio Code. You can find more information about different Linux flavors at following  link on Visual Studio code.

https://code.visualstudio.com/docs/setup/linux

Hope you like it. Stay tuned for more!!
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews