More into coding

Last week was a merge of creating tests and starting UI codes.

After so long, I was done with writing tests. I had a better understanding of how tests work, and refactoring codes as well. Moreover, I was exposed to not writing hard coded codes.

After 7 weeks, I can say that I have learnt a lot about TDD and EF Core, which is something I was not exposed to during my studies and this is something that would definitely benefit me.

Collection and Lists might sound the same, but collections are preferred over lists due to their ability to group outputs. In other words, List does not provide the feature of grouping.

For the past 3 years, I had the idea that the difference between void and static functions was related to having returns.

Little did I know that the static functions and variables are like global variables that can be used across the files, and when a method is static all its members should occupy the word “static” as well.

I have also read about bindable base which is an implementation of INotifyPropertyChanged interface in C#.

More about Entity Framework

I started getting more familiar with EF Core, although I don’t fully understand everything, I am able to grasp the main idea of how EF Core works, because I helped a friend who is doing his internship as well and was asked to use EF Core and had no idea how to use it. So, I was able to guide here and there which shows that I did learn something about EF Core after all.

If you find difficulties in understanding what I’m about to discuss refer to my previous blog by clicking on this link before proceeding https://www.tonghin.com.my/blog/2021/03/04/entity-framework-core-week/.

I finally have a better understanding of how tests work, and how to write a unit test in general.

A unit test compromises of 3 sections. Firstly, the arrange part which sets the environment for the test components. Next, act initiates the test or does the testing. Lastly, assert validates and verifies the result we expect to get by comparing our expected value to the value retrieved from the act section.

Last week, I wrote around 7 tests with the help of my supervisor, who guides me when I don’t understand or get stuck at one point or another.

Among the widely used assert extensions are First( ), Select( ) and Where( ). More information can be read from the below link https://www.tutorialsteacher.com/linq/what-is-linq.

The concept of Rollback( ), Commit( ) and Complete( ) in a transaction is among the things that I have been exposed to as well, whereby rollback undoes changes that have been committed into the database. Commit on the other hand, saves changes into the database, while complete saves the transaction, it does not update the database if commit was not initiated, as well as if complete was not written in the codes, updates that have been made wont be saved even during debugging of codes.

The keyword using in C# is used to ensure that the method dispose( ) is called after a method has been called, or or an object has been created and many other things. Dispose( ) is used to release unmanaged resources, but it does not free a managed memory.

That’s all for this week 🙂

Entity Framework Core Week

Getting familiar with EF Core is not easy at all.

Last week was hectic, I really had a lot of trouble trying to understand how EF Core works and understanding its concept, to the extent that I told my parents, I guess my supervisor regrets taking me as an intern, hahhahah.

So entity framework can simply be defined as an object which maps database to .NET. Mainly, Entity Framework (EF) and Entity Framework Core exist, such that the latter is an advanced version of EF and it supports features that EF cannot support .

EF Core is what we use at Tong Hin. It works by creating model classes and context objects which represent sessions with the database. Migrations are then used to create the database and update it as the model evolves and changes. EF Core supports relationships in database, ranging from One-to-One relationships, One-to-Many relationships, and Many-to-Many relationships.

EF Core can be used for querying and retrieving data from the database, save, edit and delete data. Taking note that all these functions are done in the model class of the entities.

The concept of EF Core is a lot, I will mention more about it in the next upcoming blogs.

Next, I was introduced to fluent assertion, which makes the assertion statement readable to anyone who has a look at the codes instead of using the statement Assert(2, 4), the following statement is more readable and understandable Answer.Should( ).Equal(2).

Clean code is a concept which involves ensuring that the codes written are easier to understand and are easier to change. Clean code can be written by adhering to the following basic guidelines which are, writing meaningful names, writing correct comments, following the standardized format of code writing, removing and refactoring unnecessary classes, and many other guidelines that can ensure that the codes written are considered as a clean code.

See you all next week again 🙂

Allows User to Decide Least Wanted Situation in System

This will be the last week of me being a part of this group development team. So I will be completing my last few implementation, alongside with integration tests.

First and foremost, I will need to solve issue that I mention last week, where item.StockQuantity being negative. It is not wrong but it is least wanted situation to happen in real world. To handle this situation, we came up with an idea to force throw new DbUpdateException(). Hence, Implement try catch statement in ViewModel class to prompt a selection dialog using the error thrown (parameter = e.Message). In the dialog box, system users are given two options, "continue" or "cancel" on their consent that item.StockQuantity will be update as negative value if they would have pick "continue".

Next, I wrote few tests in service class and ViewModel integration tests. Service tests is mainly to test out if the transaction I about to implement would commit changes successfully and rollback if there is database update errors. While, ViewModel integration tests is to determine if dialog would be prompt upon error thrown and their behavior works correctly upon user selection.

Implementation is achieved by me using two variable to control the if else statement below:
///transaction///
///Codes that check if stockQuantityNegative is true or false///
if (stockQuantityNegative && rollback)
{
throw new DbUpdateException("Stock Quantity for item {id} will be negative.\nDo you want to continue?");
}

///rollback or commits transaction///
///end of transaction///

Initially Ready To Pick method in ViewModel would called as such _service.ReadyToPickVM(parameters, true). In service/repository class, method is ReadyToPickVM(Parameter parameters, bool rollback). As initial ReadyToPick function deemed rollback to be true, the system would check if stockQuantityNegative is true to meet requirement above. If the statement do meet, system will prompt error and rollback all changes. Dialog will prompt on UI, _service.ReadyToPickVM(parameters, false) will be triggered if user clicks “continue” and save item.StockQuantity as negative value; else the system will not do anything after rollback.

After implementation is done, I make sure the tests passes. I alter few logic and in implementation and tests, making sure it is good to go. I then push my latest code to repository and marks the end of my participant in this team.

Last but not least, special thanks to my supervisor Peter for allowing me being a part of this group development team!

Variables Value Change According to “Ready To Pick”

In my implementation, Ready To Pick works as a signaling mechanism to let other module know when they could access the sub data in current module. As I working with Ready To Pick function previously, there is new implementation to add on upon clicks on Ready To Pick button.

First of all, in order to press “Ready To Pick” or “Undo Ready To Pick”, we will select a data/item set for example. In this item set, there is multiple list of item will it’s own attributes. Given availableItem and standbyItem as item’s attributes, new implementation is to alter value of attributes by incrementing or decrementing the value upon status of isReadyToPick (bool). Implementation must alter all the value for each item available in item set adding/subtracting itemQuantity.

i) Clicking “Ready To Pick” button when isReadyToPick = false, would change it to true and alter attributes value such as below:
foreach (item in itemSet)
{
item.availableItem -= item.itemQuantity;
item.standbyItem += item.itemQuantity;
}


ii) Clicking “Ready To Pick” button when isReadyToPick = true, would change it to false and alter attributes value such as below:
foreach (item in itemSet)
{
item.availableItem += item.itemQuantity;
item.standbyItem -= item.itemQuantity;
}

To avoid infinite increment or decrement, implementation is done in a transaction, to make sure the logic rollback if there is error. By using transaction, all value changes for isReadyToPick(bool), availableItem(int), standbyItem(int), itemQuantity(int) will be reverted upon any update failure.

Few tests is done to assert value change of availableItem(int), standbyItem (int). Tests assertion pass after “Ready To Pick” button is triggered, the value of attributes add or subtract value of itemQuantity.

After the implementation done, I shall continue implementation logic for situation when item.availableItem -= item.itemQuantity appears to be negative value.