Inisde this repo, I apply Dijkstra's Algorithm in order to calculate the shortest route from a start and end node. This has various applications, most commonly used for non player characters to navigate an environment.
No steps provided here, just pasting the source code if you are interested. There are plenty of tutorials about how this is accomplished online. Use as you wish.
Working Unity project available here
EF Documentation link
In the video below you can see me creating a basic console application to add Schools to a SQL local database using Entity Framework Core.
What you see in the video is available on GitHub link
Amongst other things, EF Core Migrations automates a lot of otherwise manual coding in order to create, read, update and delete SQL databases. It even allows you to recreate your classes and context from an existing database.
In order for this automated process to work, it has to make assumptions about the way you would like your database structured. It does this by starting with your context and inferring the rest using conventions.
In EF Core, we have the benefit of being able to inspect this process and makes changes along the way, to ensure everything is setup as we want and still benefit from automation.
This example migrations file below is generated for us and uses the migrations API. You can see it populating the Up function from existing context classes and properties. It also includes a Down function which will drop the database.
A snapshot file is also created so migrations can keep track of modifications for its update feature, by way of comparison to a newer version. It looks like this:
The benefit of being able to access this file, is that in production environments, where mistakes can be costly and there are other people involved in a project, you may want to have your changes checked by an authority before they get applied against the database.
To run a command, you can use the NuGet Package Manager Console, however you need to first import the Microsoft.EntityFramworkCore.Tools (which includes the Design package too as a dependency), also make sure to select the correct "Default Project" from the dropdown in the console.
Get-help entityframework will show you a list of available commands, however the basic ones are:
SQL is a query language used to interface with relational databases and stands for Structured Query Language.
To understand its use, let us pretend we are making a supermarket application to track prices of products across different supermarkets. You could go to these supermarkets and note down the price of the products or use their websites to populate the data on the spreadsheet. Logically, you could track the data as follow:
Or indeed like this:
Then you being writing your program that will interact with this data (please ignore the Id property in each class):
Now you are ready to start filling these classes with information from the spreadsheets. You could convert the spreadsheet into a CSV, to be able to parse it as a string in your code. The second spreadsheet example generates this:
Each line represents a row inside the spreadsheet. This is a workable solution, however Edgar F. Codd invented the relational model for database management, which through complex math, ensures, “…data integrity, …logical and physical data independence from database applications, …guaranteed data consistency and accuracy, …easy data retrieval [and more]” (source).
Lets take a look at how our excel data & code would look like inside a relational database set of 4 tables, instead of using a spreadsheet:
Looks neater and more scalable don’t you think?
Each table could very well be new tabs & sheets in excel; Supermarkets, Isles, Shelves & Products. We would know which products belong to which supermarket by the SuperMarketId, IsleId & ShelfId columns and this would be navigable by code by using those fields, hence this property is referred to as the navigation property.
In a program called Microsoft SQL Server Management Studio or SSMS, we can generate a database diagram from the code, here is what it looks like for this example:
This helps us to visually understand some of what is going on behind the scenes.
Each Id is the “primary key” used to identify the row in the table and each link is represented by the keychain and is called the relationship and contains the “foreign key”.
Each row in the table will have a foreign key pointing to the row in the table it belongs to (its related data), by using a combination of the primary key of that row (or its Id value) and the primary key (the Id value) of the row in question itself. The order of the pairing depends in the direction of the relationship.
A good way of interpreting the keychain symbol is imagining that the key is pointing towards the table that it belongs to, like the Isle belongs to the supermarket, as the supermarket has isles, as defined in our spreadsheet and our code.
Once you have a database, you would need to query it inside your C# code. Before you do that, it’s worth using SSMS to run some SQL commands on the database to view the data inside it, to know what we should be looking for inside our code:
So here you can see the select commands at the top, executing against our database, with the results tables below for each class. You can see I have inserted some data into the database already.
Now that we know what data our database contains, let us try to query the database in our C# code. Say you wanted to find all the raspberries in every supermarket, listed by price, you could try using foreach statements to loop through the SuperMarkets DbSet, by first converting the DbSet to a list, however this would be the wrong approach, I will show you why.
Here I convert the supermarket DbSet to a list:
Then use nested foreach statements like this:
We should get something like:
However, we get this:
You can see that it is printing out the supermarket names only, as the data contained within the Isles collection is not being pulled in. That is because the .ToList() function is returning the SuperMarkets’s names inside its database table, to a list of SuperMarkets (the type we expect, thanks to EF (Entity Framework)), but not the data within the related tables.
To access the data within the related tables, EF uses the query language LINQ. Intellisense provides a hint to us when we remove the “.ToList()”, shown here:
Say we have a supermarket price tracker application and we want to setup the data, adopting a code first approach, to eventually provide the user with the ability to Console.WriteLine the prices of all Fruit, by cheapest first, we would create the following: a supermarket with shelves, isles and products.
We would then run “context.Database.EnsureCreated()” to create the database structure for us, then manually enter the data inside SSMS.
Now we have some data, and code, we are set to use LINQ to show the:
We can do this by using the “Include”, “Where” and “OrderBy” LINQ extension methods:
Running this code products exactly what we want, as we filtered out the raspberry by using the “.Contains(“Strawberry”)” filter:
Model View Controller is a popular software architecture design pattern used to arrange code in your project, so it easier to read and use, as some projects can get quite complex, this becomes necessary.
So in the example of a command line application “Trade Manager”, where the user can type commands to interact with a cryptocurrency exchange, the Controller would contain the code to take the input from the user, request the data from the model (which will get it from the exchange), then provide the view with the data it obtained from the model, in exchange for a formatted set of data to show to the user.
The key point to make is that the model and the view don’t talk to each other.
Comming soon...
Comming soon...
The division of related elements into classes & methods, so that they each serve one purpose, resulting in cleaner, less complex & more readable code.
This is the use of virtual methods and inheritance to ensure that derived types can have different behaviour, without modifying the base class. This isolates breaking changes, reducing overall impact.
The simplification of class elements into smaller classes, suitable for all derived types, so that no inheriting classes access methods or properties that aren’t relevant to them, reducing errors.
The separation of methods & properties into an interface that can be optionally implemented, allowing more flexible derived types, avoiding unused code.
The parameterisation of code blocks via for example, an interface, to keep high level logic separate from low level, increasing flexibility and stability.
Imagine trying to create a software only version of money, that is secure, inflation resistant & a perfect store of value. That's a pretty big ask. Bitcoin's answer to this is having distributed ledgers, strength by numbers, ensuring multiple copies of the data set, so that one person can not corrupt it.
In order to get as many people as possible to run this software that keeps the distributed ledger, amongst running other functions, Bitcoin rewards its hosts with denominations of its own currency. It first started out by rewarding people who ran the software with 50 new Bitcoins for every addition to the ledger, along with smaller fees for each transaction in the block. Every 210,000 blocks (ledger additions), this reward halves, the reward is currently 6.25 new Bitcoins, which means we have had 3 halvings.
Importantly if the reward (50 new Bitcoins in this case) halves, so does the total amount of new Bitcoins created in that cycle, as mining is the only way to create Bitcoins. This makes Bitcoin anti inflationary, reducing inflation by ~12.5% per year or 50% every 4 years (halving cycle):
This effectively makes less Bitcoins available over time, increasing its rarity, shown by the price over time, on the price chart below.
The way the Bitcoin software conducts this is by way of competition. The people running the software each guess what the magic code is (mining for the nonce), as fast as possible many many times per second, until one of them guesses it correctly. This loop is designed to last and repeat every 10 minutes. It is also secure as the cryptographic algorithm it uses verifies the nonce against the hash for that block, only 1 nonce will work for that block's hash.
Once this nonce is found, it is sent to all the other hosts to be verified against their copy of the block too. If it comes back verified (proof of work), the block is added to the blockchain, along with all of its transactions and the rewards are given to person who won the challenge through a "coinbase" transaction, which is included inside every block by default. The winner can choose where to send the rewards using this function.
You can use btc.com to browse blocks and transactions. I found an interesting comment inside a transaction's input that says: "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks". See if you can find it.