Implementing a Restful API with .NET Core 2.1
Total Page:16
File Type:pdf, Size:1020Kb
8 Jaakko Heikkilä Implementing a RESTful API with .NET Core 2.1 Metropolia University of Applied Sciences Bachelor of Engineering Software Engineering Bachelor’s Thesis 31 January 2021 Abstract Author Jaakko Heikkilä Title Implementing a RESTful API with .NET Core 2.1 Number of Pages 48 pages Date 31 January 2021 Degree Bachelor of Engineering Degree Programme Information Technology Professional Major Software engineering Instructors Janne Salonen, Principal Lecturer Tuomas Jokimaa, supervisor The objective of this thesis was to design and implement a bespoke backend API using Microsoft’s .NET Core framework. The work included the architectural planning, writing ap- plication logic and unit tests, setting up a version control system in GitHub Enterprise and implementing a continuous integration pipeline using TeamCity and Octopus Deploy. The implemented API is part of a larger group of intranet applications which are used by the end users to manage their sales materials and to partition them for different sales teams. The implemented API works as a facade between the user interface and a SQL Server in which the actual logic was implemented. The thesis explores briefly the technologies which were used, and explains the .NET fram- work’s history and some of its web development tools. The three external tools: GitHub En- terPrise, TeamCity and OctopusDeploy are explained. The software development process and the structure of the code are then looked at in more detail. The thesis was carried out in order for the customer to improve their business critical oper- ations. The application was made as a part of an already existing set of applications which dictated the use of programming technologies. The following technologies and tools were used in the implementation: Visual Studio 2017 Enterprise Edition, GitHub Enterprise, TeamCity, OctopusDeploy Keywords Microsoft .NET Core, ASP .NET Core, REST Api Abstract Tekijä(t) Jaakko Heikkilä Otsikko REST API:n toteutus .NET Core 2.1 Sovelluskehyksellä Sivumäärä 48 sivua Aika 31.1.2021 Tutkinto Insinööri (AMK) Koulutusohjelma Tieto- ja viestintätekniikka Suuntautumisvaihtoehto Ohjelmistotekniikka Ohjaaja(t) Janne Salonen, Principal Lecturer Tuomas Jokimaa, supervisor Tämän opinnäytetyön aiheena on suunnitella ja ohjelmoida asiakkaan tarpeisiin räätälöity backend API käyttäen Microsoftin .NET Core ohjelmistokehystä. Työhön kuului sovelluk- sen arkkitehtuurin suunnittelu, sovellus- ja testauskoodien kirjoittaminen, versiohallintajär- jestelmän pystyttäminen sekä jatkuvan integraation työkalujen pystyttäminen. Työssä tehty API on osa ohjelmistoa, joka on intraverkossa oleva palvelu, millä loppukäyt- täjät voivat hallita myyntimateriaaleja ja niiden jakamista eri myyntitiimeille. Toteutettu API toimii eräänlaisena fasaadina businessäännöt sisältävän sovelluksen ja käyttöliittymän vä- lillä. Opinnäytetyössä perehdytään käytettyihin teknologioihin ja .NET ohjelmointialustan histori- aan, ohjelmointiprosessiin ja ohjelmakoodin rakenteeseen, sekä automaatiotyökaluihin TeamCityyn ja OctopusDeployhin Opinnäytetyö tehtiin, jotta asiakasyritys voisi hallinnoida ja seurata liiketoiminnalle tärkeän materiaalin myyntiä. Sovellus tehtiin osaksi isompaa kokonaisuutta .NET Core -kehyksellä tehtyjä ohjelmia, joissa on pyritty pilkkomaan isompaa sovelluskokonaisuutta pienempiin hallittaviin osiin. Ohjelmistokehys .NET Core valittiin sen ollessa jatketta kypsälle .NET Frameworkille ja myös sen monialusta-tuen vuoksi. Sovelluksen toteutuksessa käytettiin seuraavia Microsoft .NET työkaluja: Visual Studio 2017, GitHub Enterprise, TeamCity, OctopusDeploy. Avainsanat Microsoft .NET Core, ASP .NET Core, REST Api Contents List of Abbreviations 1 Introduction 1 1.1 Background for the reasoning of the project 1 1.2 Commissioning the work, challenges faced 2 1.3 Outline for the thesis 3 1.4 Key concepts 3 2 Goals for the project 4 2.1 Implementation limitations 4 2.2 Version control, continuous integration and delivery (CI/CD) 5 2.3 Pursued benefits for the related parties 5 3 Overview of .NET Core framework 7 3.1 A brief history of .NET Core 7 3.2 .NET Standard 8 3.3 .NET Core features 8 3.3.1 ASP.NET Core 8 3.3.2 NuGet Package Manager 9 4 The implementation of the project 12 4.1 Application development tools and environment 12 4.1.1 Limitations set by the development environment 12 4.1.2 Docker 13 4.2 CI/CD tools 13 4.2.1 GitHub Enterprise 14 4.2.2 TeamCity 15 4.2.3 Octopus Deploy 16 4.3 Application architecture 16 4.3.1 REST (REpresentational State Transfer) 17 4.4 Overview of the server application architecture 18 4.4.1 The program class and the WebHost 20 4.4.2 Startup class, middleware 21 4.4.3 Routing 25 4.5 Controllers 26 4.5.1 Controller implementation 27 4.5.2 DTO models 28 4.6 Data-access layer 30 4.6.1 Entity classes 30 4.6.2 Updating the database through entity’s methods 33 4.6.3 DbContext 35 4.6.4 Repositories 36 4.6.5 Services 39 4.7 Unit tests and fake implementations 40 4.7.1 Unit test naming convention 40 4.7.2 Unit test frameworks and libraries 41 4.7.3 Structure of the unit tests 42 4.7.4 Fake services and data initializers 43 4.8 Summary of the implementation 44 5 Conclusions 46 5.1 Successes 46 5.2 Further improvements 47 References 48 List of Abbreviations .NET Microsoft’s leading software framework .NET Core Modern version of the framework ASP.NET A software framework for web based applications On-premises Residing in a local server and not in the cloud REST API HTTP based architectural model introduced in 2000 for web-based appli- cations Docker Virtualized operating systems in small packages. Azure Public cloud service for Microsoft. T-SQL Query language for Microsoft SQL Servers Entity Name for objects that describe the database rows and columns. Used in Entity Framework. Environment In software development there are different environments for the software to exist depending on the stage of the version of the software. Develop- ment, testing, production are just a few examples Pull request For version control systems, the review of code is done via pull requests. A code change is asked to be pulled into another version of the code. AAA Arrange, Act, Assert principle for unit testing. Appendix 2 1 (49) 1 Introduction The work carried out for this thesis was commissioned by the employer company Elisa Oyj. Elisa Oyj was founded in 1882 and has been developing the infrastructure and tel- ecommunication technologies as a pioneer in Finland ever since. In 2010 Elisa Oyj ac- quired Appelsiini Finland Oy, which later was fused into the parent company. Appelsiini Finland Oy had a history of software development in Microsoft's .NET technologies and had many clients and their projects which were transferred to Elisa Oyj in the fusion. One of these clients had an application critical to their business. This application included number of different business functions that were programmed in a monolithic codebase with .NET Framework 4 using ASP.NET MVC. The applications consisted of a user in- terface that was tightly coupled with the server code logic. The server application handled the requests from a web browser and passed those requests onto an SQL Server in- stance where business logic was handled with parametrized stored procedures. The stored procedures were not in the scope for the project. The application resided on a single, on-premises server that is accessible only through the client's network. It was running on a Windows Server 2012 in an instance of IIS. Starting from 2017, this application was given greenlight to be rewritten in smaller indi- vidual applications reminiscent of microservices and using .NET Core as their technology stack. 1.1 Background for the reasoning of the project As software becomes larger and its codebase grows older, the upkeep of the software becomes more time consuming, difficult and costly. Years of hotfixes and patches can leave software filled with undocumented code that violates many principles of good soft- ware craftmanship. At some point it will be more efficient for long term costs to re-write an application. In 2018 .NET Framework 4 was already on a path to be replaced by .NET Core, an open- source version of Microsoft’s technology stack. For many projects made during that time, or after it, there was no discussion on whether to use the newer stack. But not all kinds Appendix 2 2 (49) of software could be - or can yet be - ported to the new .NET Core framework. As for simpler web applications that use the ASP.NET framework, the groundwork done in .NET Core was already feasible to be used in production applications in its version 2. This change in the direction for the framework came from Microsoft and subsequently also became a priority for the employer. As the employer is also focusing heavily on Azure cloud competence, the need to make applications with .NET Core rises. 1.2 Commissioning the work, challenges faced New applications for the replacement to the client's application were started to be pro- grammed late 2017, and by late 2018 three out of four of the first applications were ready. The overall software architecture for these was in place, the initial requirements and schedule was in place. However, when the last application was to be started, the original team was unavailable, so a new team was tasked to finish the one remaining application. The team consisted of two frontend developers and two backend developers. Requirements for the backend application was to be able to pass the queries from the new React.js frontend to the database which had not been changed during the migration to the new architecture. During the development, the visibility to the database was limited to excel sheet descriptions of the database which included the names of stored proce- dures and the parameters they used. The database also had views which were queried for the data that was returned for the client application. The project was to be done agile, in sprints, but the client was availably minimally, and was only asked to clarify requirements.