EF Core Performance

Last week , I worked on optimizing the runtime of loading inventory items . The problem is a “Cartesian Explosion” that is related to performing joins . When we perform a join , one’s table repeat the X number of time the matched records of another joined table . There are a few ways to avoid the cartesian explosion especially when loading related entities in a huge database :-

  1. AsSplitQuery() introduced by EF core 5.0 . It only works when using Include(). Instead of joining the tables , split queries generate additional SQL query instead of single queries .
  2. IncludeOptimized() introduced by EF core pro . It works similarly to the first method as it drastically decreases the amount of data transferred to avoid generating excessive data that is needed .
  3. Explicitly loading the data needed in the single query .

In conclusion , unfortunately there isn’t one solid solution for loading all entities that fit every scenario . IncludeOptimized() does not support AsNoTracking() and cannot be mixed with Include() . In hindsight , explicit loading should be the way to fix the cartesian explosion problem as there are limitations to using built in functions .

Implementing wrappers

Last , I was given a few issues in my git repository. Previously in the SandBox application , data were selected in the ComboBox with SelectedValue , showing names with DisplayMemberPath . I was advised to use wrappers instead for more maintainable code . After implementing the wrapper instead of SelectedValue and DisplayMemberPath the codebase became less complex .

A Wrapper class “wraps” an object of another class. It is used to implement a design pattern that has an instance of an object and presents its own interface to that object, without changing the original class .

I also fixed some bugs in the ViewModel and integration tests between modules . In the ViewModel , methods were called twice causing errors, because EventAggregator was subscribed more than once, once in a method and another in the constructor , to fix this I removed the subscription from the constructor , fixing the bug while making the integration tests pass.

That is it for last week . For next week , I will be planning a model for stock threshold in the application .

UI Navigation Tabs

Last week , I have been given tasks to create navigation tabs using INavigationAware in the SandBox App . There are a few things implemented once I applied the INavigationAware interface into the view model .

First , IsNavigationTarget is a bool method method that allows an existing view or view model to indicate whether it is able to handle the navigation request .

Then , OnNavigatedFrom method is to allow the previous view to save or deactivate any current events from EventAggregator. The OnNavigatedTo method is called when the view is initialized , to pass on data from the navigation parameters .

Also , the NavigationContext that prism provides is to receive parameters from the NavigationParameters class , that is passed from the RequestNavigate method .

Prism also provides the IRegionMemberLifetime interface that has a KeepAlive boolean , to control the views to set them whether to be deactivated or not .

Lastly , after the implementation of navigation activities . I created some tests for services in the project , restricting the deletion of table if foreign key exists .

Domain Driven Design

For the past week , I have been working on creating repository patterns in the project .

Throughout my coding experience in the Tong Hin Sandbox Application . I have implemented some of the common DDD patterns in the project .

In DDD , the Domain normally refers to the “pillar and activity around which the application logic revolves around” . The DDD pattern aims to ease the complexity in an application .

Common terms used in DDD practices :
– Ubiquitous Language: A language structured in the project between team members to solve conflicts
– Bounded context: A description of a boundary within a model
that is defined and applicable to team members
– Aggregate : An aggregate is an encapsulation of entities and value objects
– Service : A functionality that must exist but cannot relate to an entity or value object
– Repository : An interface to allow CRUD around a project

With these practices , it helps to minimize the complexity of the code . These are just the common terms used and there is more to know as I complete new tasks.

Testing phase

The past week , I did concurrency tests , automatically creating item names with concatenating strings , set bindings and some UI updating .

UI updates were setting auto-generated item names to read-only so user cannot edit textboxes , adding new textboxes with new property. Also, adding migration to the database with the latest updated property .

String concatenating (meaning linking a string in a chain or series) is to join different property of strings together , while implementing the logic in Wrappers to save different strings concatenated into database.

The concurrency problem I faced was when two users execute a transaction simultaneously . Ideally in this situation, the first user transaction will succeed and the second will fail .The common concurrency problems that potentially happen in DBMS transactions are usually :

  1. Temporary Update Problem
  2. Incorrect Summary Problem
  3. Lost Update Problem
  4. Unrepeatable Read Problem
  5. Phantom Read Problem

https://www.geeksforgeeks.org/concurrency-problems-in-dbms-transactions/

However , one way to fix a concurrency problem is when the second user fails transaction , is to reload its transaction entry . Then resave it by clicking the save button again .

Testing / UI week

For the past week , I have been creating integration tests on my task (create short code with sequence generators) . I also did some fixes on the UI and set textboxes to “ReadOnly” during view mode .

Also , excited to be part of the launching of Tong Hin’s App to be used in the real world soon . The more that I’ve worked with my supervisor , I found out that code is easy to write , but it’s challenging to write good code . Reminds me that if I want to improve as a programmer I need to go back to basics and develop good habits . Therefore , always remember to :-

  1. KISS (Keep it simple, stupid) , never write code that is too “clever” . Use meaningful variable names so that it’s easy to read and know exactly what is going on .
  2. Clean / Refactor code at all cost , making it more efficient and keep the result same at the end .
  3. Document your code , write comments on functions , variables , entity relationships etc to make it easier to understand when revisiting your code after months .

Will progress slowly by doing these mini steps . So that’s it , see you next week !

Coding week

For this week , I did my first two-phase commit (2PC) in my next task . When adding a new item into database, multiple databases are used . That is where 2PC is implemented , because there is more than one database involved. The terms committing and rollback is often used in 2PC, committing is when updating a record to a database. A rollback is the operation of restoring a database to a previous state by canceling a specific transaction or transaction set .

During this process , I also learned the difference between repository and service in the project. Repository is to create methods for CRUD and services is used to write methods for the business logic. This was very useful to me because back then in my academy, I mixed my business logic and CRUD in the same folder, caused a lot of confusion to my trainer and myself. I believe now that I know about services the project structure in the future will be much cleaner.

That is about it for last week , I also did some HotFixes on the UI of my first task (Convert WinForm to WPF MVVM project) to increase the font-size and layout, and recreated the container in my docker to setup for my second task.

For next week, I will move on to create the integration test for my second task ( auto increment for sequence count for short code).

First month in Tong Hin !

For this week , I have completed my first task (converting WPF to MVVM project) . I am finally able to move on to creating the unit tests for the completed task . For the unit tests , one of the important NuGet package used is Moq . Moq is a mocking framework for C#/.NET. It is used in unit testing to isolate your class under test from its dependencies and ensure that the proper methods on the dependent objects are being called. 

For Moq : Setup , Verify & Callback is the most common command . The “Setup” command mocks a method and “Return” the specified value called . “Verify” marks the expectation of the mock and you can verify how many times it is called with Times.Once or Times.Never . A callback is a piece of code that is passed into to a method parameter to be executed from within that method. When using Moq to create test doubles, you can supply callback code that is executed when an expectation is met.

Before completing the first task , codes have to be pushed into the branch. With lack of experience using git , I committed 4-5 of my commits into the wrong branch resulting in having to create a new feature in the branch . To fix this , my supervisor taught me how to cherry pick ( git cherry-pick [commit reference] ) , picking a commit from a branch and applying it to another to undo changes.

Another scenario that happened this week is pushing the commit into the wrong branch . https://dev.to/moshe/remove-accidentally-pushed-file-from-a-git-repository-history-in-4-simple-steps-18cg (will link the solution provided by my supervisor here if mistakes happens again) . Reminder : check if in correct branch in git history before git push, git pull if in incorrect branch.

Lastly , I learned about the ternary operator (which is a substitute for if..else statement used a lot in my academy back then) and is much shorter version . For example , Condition ? Expression1 : Expression2 ; if the statement is true , the statement will return Expression1 and return Expression2 if false . It is called a ternary operator because the operator takes 3 operands . That is all for this week .

Week 3 in Tong Hin !

For my 3rd week in Tong Hin, I was able to create many more functions in my current task (converting project Windows Form to WPF MVVM prism application). This App is created to add users , hash and salt the password for created users and save them into database.

Currently the App is able to generate new users , update salt , reset password , show data in grid , refresh and delete user . Functions can also work with validation (username & password validations etc) . I am also able to add success / warning dialogs into the App . Most of the functions went smoothly except my entire App was unable to run for 2 times which creates panic sometimes (fixed with the help of my supervisor). The remaining function for me to fix is password (which is the hardest function among all because prism doesn’t allow to password bindings directly in the PasswordBox) and add ComboBox to the FloorsInCharge in the update user function .

Currently , I’m doing a lot of research about using Git , Source Tree (committing code and writing commit messages) generally the basic necessities a software engineer needs throughout their programming life .

My supervisor also suggested me to look up on clean code , single instance vs singleton and two-phase commit transaction which is my goal for the next week since there is only hands-on coding last week and I believe understanding the theories in programming is also equally important than the practical hands-on part .

Finally , quite intense of the first 3 weeks but glad that there is progress better than none . That is all so see you next week ! =D

Second week in Tong Hin!

I was given my first task in Tong Hin and assigned to convert a WPF project to prism MVVM View Model and do some unit tests . As usual , I will note down the things I’ve done my second week .

For my second week , I started to work on the UI of my given task . I have completed most of the UI but without data bindings . I also fixed some minor problem such as connecting the config path to the database, created different checkbox buttons in my project .

Also , I finally did my first unit test (with the help of my supervisor) and learned some things about wrappers . I also managed to complete most of the “Prism MVVM View Model” tutorial and EF core tutorial .

Next , I learned a new delegate such as Func in c# , Func is a delegate that points to a method that accepts one or more arguments and returns a value .

That is all for this week . Will be ready for more challenges , so for next week , my goal is to will try to load the data from database to the data-grid in my UI in the project and tryout more unit testing .