Becoming a software developer – episode XXII

Becoming a software developer – episode XXII

Welcome to the twenty-second episode of my course “Becoming a software developer” in which we will use SQL Server database along with Entity Framework Core library.

All of the materials including videos and sample projects can be downloaded from here.
The source code repository is being hosted on GitHub.


 

Scope

  • SQL Server
  • Entity Framework Core

Abstract

SQL Server

At first, we need to have access to the SQL Server where we could store our data. You can download SQL Server here and install it on your own or just connect to the instance running somewhere in the cloud or locally by using Docker like I did.

Once the SQL Server is installed, there are many tools that provide a graphical interface for managing the database.
One of the most popular ones (and also free) is SSMS, yet there are also others such Datagrip, Team SQL or SQL Toolbelt. You can also use the mssql plugin for the Visual Studio Code.

Finally, execute the following script to create a new database and a table for the User type.

Entity Framework Core

Entity Framework Core is a new version of EF ORM (Object-relational mapping) designed to provide an access to the SQL Server database. It’s one of the most popular libraries for storing and retrieving the data from the SQL Server, as well as configuring the classes and the mappings between our models and database tables. Once we install the required dependencies, we can create a new DbContext which will be responsible for handling the connection:

Now, we can implement the UserRepository:

And finally, include the required services within Startup class:

The proper connection string using some default credentials may look like this: Server=localhost;User Id=SA;Password=abcd1234;Database=Passenger.

Next

In the next episode, we will focus on publishing and deploy our application using tools such as Docker, Nginx and uploading it to the cloud where we will run a virtual machine containing Ubuntu Server.

23 Comments Becoming a software developer – episode XXII

  1. Mark

    Can you do an tutorial about automating publishing applications to windows server (IIS) with automated tests ? And can you recommend something like https://travis-ci.com but up to 10-20 $ a month for closed source projects ? (or even better for free )

    Reply
    1. Piotr Gankiewicz

      Hey, please take a look at my previous posts. I did a tutorial about Travis CI, BitBucket Pipelines (that’s a great alternative) and others. I’m not using IIS unfortunately, but it should be pretty much the same scenario as publishing the application to the Ubuntu Servers, especially if you use Docker.

      Reply
  2. Pingback: Dew Drop - July 17, 2017 (#2521) - Morning Dew

        1. Piotr Gankiewicz

          Raczej na pewno nie, przynajmniej nie w najbliższej przyszłości. Praktycznie nie używam już baz SQL, do tych zagadnień jest masa przykładów w sieci, a poza tym i tak nie użyłbym aktualnej wersji EF Core produkcyjnie.

          Reply
  3. Łukasz

    Why create repository? This is abstraction over abstraction. Why not use the EF in the services directly? What is the point?
    Second question. This is a thing that I have seen many times. Returning the entire record (entity) from the database. Retrieving record using this repository for the User, it gets all the fields from database. What if e. eg. we want only user id and name for drop down list? We can have 20+ fields in the table, but only need two for this specific user case. So what to do? Get all of them this is a waste of resources. Moreover this is a simple SELECT * FROM … This is not how databases should be quering.
    Using the repository pattern I was created specific methods for specific queries with custom DTOs, but this is a garbage in the repository. Even creating them as a extension methods. So I’m not sold for this pattern. But still looking for a hints from others that may have different different points of view.

    Reply
    1. Piotr Gankiewicz

      Thanks for pointing this out, I’m aware of all of these things, but in case you have not noticed, this was a basic video about SQL Server + EF, not an in-depth tutorial lasting a few hours. Besides that topic, DbContext is a repository and UOW itself, but due to its poor design is not an interface, thus injecting a class directly into application services makes them impossible to test. I’m not a fan of EF and not a fan of using a repository with DbContext either, however, if you look at the repository like at some generic interface, you’re missing the whole point of the domain.

      Reply
  4. Arek

    W jaki sposób do projektu byś zaczął implementować frontend np. Angular4 od strony architektury ? Nowy projekt typu Passager.WebSite czy w samym Api i komunikacja między 2 stronami? Mógłbyś troszkę nakierować jak by można to prawidłowo wykonać ?

    Reply
      1. Arek

        Może moje pytanie śmiesznie zabrzmi ale czy na produkcji nie wzrośnie delay pomiędzy odpowiedziami API do Website ? (Wszystko by było na 1 serwerze)

        Reply
  5. Damian

    Witaj Piotrze, jakie masz plany na przyszłość rozumiem że w najbliższym odcinku jak wspomniałeś docker, a co w 24 ? I potem planujesz jeszcze tworzyć jakieś kursy płatne / bezpłatne ? Masz jakieś realne plany na chwilę obecną ?
    I kiedy możemy się spodziewać kolejnego odc kursu : )?

    Ogólnie świetna robota z całym kursem. Planujesz również jakiś update artykułów pod .NET Core 2.0 ?

    Reply
  6. Piotr Gankiewicz

    Cześć, na ostatni odcinek mam pewien pomysł ale wyjdzie w praniu czy go zrealizuję :). Natomiast nr 23 powinien być dostępny jutro.
    Wydałem ostatnio jeden kurs dla Packt Publishing (po angielsku), pracuję teraz nad kolejnym, prawdopodobnie również przygotuję coś dla Udemy Polska. Natomiast co do samego YouTube to mam trochę pomysłów co dalej z kanałem ale myślę jeżeli coś ruszy to na przełomie sierpnia/września. Jak wyjdzie stabilna wersja 2.0 to pewnie się za nią wezmę.

    Reply
    1. Mateusz

      A masz w planach poruszać w kursach jakieś bardziej złożone tematy lub bardziej się zagłębiać w pewne tematy ?

      Reply
        1. Mateusz

          Ogólnie moja propozycja jest taka żebyś może uaktywnił newsletter ? Bo niby jest ale w sumie nic z niego nie przychodzi 😛 Fajnie by było dostawać informację co wrzucasz / co się dzieje w świecie / jakieś nowe info o planach / kursach

          Co sądzisz ??

          Reply
          1. Piotr Gankiewicz

            Jak najbardziej, mam to w planach tylko ostatnio co innego zajmuje mi czas, ale co do samego YT mam sporo pomysłów więc na pewno wszystko ruszy w niedalekiej przyszłości :).

    1. Piotr Gankiewicz

      Nie za bardzo, context lepiej wstrzyknąć, jak stworzysz nowy “z palca”, to nie masz nad nim kontroli + trzeba pamiętać, że wszystko to co się dzieje w bloku using należy tylko do tego contextu.

      Reply

Leave a Reply to Piotr Gankiewicz Cancel reply

Your email address will not be published. Required fields are marked *