Status Checker To Control System Flow

As we have more implementation in our ongoing project, more and more criteria we need to take care in our system. That includes business logic, flexibility of system, and system flow. Our topic is about managing system flow.

As what you should know, I am implementing function to getItemData() everytime I select an item in list. However, when I select an item, it trigger selected event that would call event1, which event1 would call selected event, forming an infinite loop. I then figure out some idea to handle this situation as I saw my supervisor’s existing code implementation.

To achieve my my implementation, I have to add a custom status checker to control the flow of system. Below are code for my status checker:
private enum SelectedStatus
{
Normal,
Selected,
}

private SelectedStatus _selectedStatus = SelectedStatus.Normal; //declare default value of SelectedStatus

public void SelectedEvent
{
if (_selectedStatus == SelectedStatus.Normal)
{
_selectedStatus = SelectedStatus.Selected;
_eventAggregator.GetEvent().Publish(id); //call event1 that cause loop
}
else
{
//logic//
_selectedStatus = SelectedStatus.Normal;
}

}

When Event1 callback to current function, _selectedStatus would now be SelectedStatus.Selected and will not publish another event to cause infinite loop. At the same time, I am able to get latest item data each time I select the data. My intention of getting latest item data is to ensure I can update data changes and minimize concurrency situation.

In future, if there is more features to add on, we can always add few more variable in SelectedStatus. This way, It provide more alternative for us to control/restrict our system flow.

Fresh start!

My first week have just passed at Tong Hin and all what I can say is, it is a new experience for me.

The first week wasn’t easy, I was exposed to many areas I have never encountered before or heard about, but honestly, it is a great experience to explore more and get to learn new things especially with the guidance and help of my supervisor.

I got exposed to Bitbucket, Sourcetree, and Dropbox. I got to know that Bitbucket and Sourcetree are alternatives that can be used instead of GitHub.

Creating stashes was also something I got to learn, in which you can save changes that have been made to the codes and apply them again after some time.

InputBindings is used to bind an input to a command or invoke a command upon input given by the user. The input can be given from the keyboard or mouse gestures. However, the focus should be adjusted in which the application is set to have an area of focus where the command is executed depending on the area where the user gesture is detected.

Delegate Commands are part of the interface ICommand, in which the delegate commands are used to invoke methods that are assigned to commands when the command’s execute and canExecute methods are called.

Next I was introduced to converting ICommands to become delegate commands and using multiple views into another view. In other words, to use methods in a class in another class.

Resource dictionary are XAML files that can be reused across the application without the need of writing the same code over and over again. With resource dictionary, referencing to the XAML will allow the class or the view class to get access to that part of the code. However, I was having trouble using the Inputbindings method into the resource dictionary, because the dictionary is not able to recognize the function.

Basically, for this week I was able to use the Escape key to open dialogues, and the dialogue displayed uses the cancelDialogueView as well as I was able to create a resource dictionary which needs adjustments to be made next week.

Hopefully by next week I would be able to accomplish more and learn more to share it with you guys, see you all on my blog next week 🙂

Trying out Domain Event

As the system implementation getting bigger, It may be hard to spot/fix important function. Therefore, we came up with an idea of using Domain Events to organize each function explicitly. As you may know, domain event is a simple class that constitute event in domain.

For the understanding, I go through some example and article regarding domain events. After that, I try out the implementation on my actual project. As my project is using Unity container, It is a bit differ from most examples on the internet (mostly structure maps). With few trials here and there for unity, my wrapper service class does directed to the event class, however it skip the event handler. Thus, nothing is trigger upon the button that I clicked and error prompted.

I then proceed the implementation based on various example that uses structure maps. After setting up the implementation, same thing occurs. Despite altering multiple declaration, system still unable to run as what we wanted. Even though it is said to be a great organized implementation, but it is still hard for me on how to implement domain event. In conclusion, domain events is yet suitable for me to apply in my actual project, thus, I have to look into other options available.

Remove File from Git Repository History

As I my implementation goes on, I push few commit upon my progress to bitbucket origin/develop branch. However, there is files/class that I want to remove from previous commits. After search up the internet, I get to know that git rm "filesname" command in new commit will not completely remove it from previous commits. Thus, after referring to my supervisor, I get to known there is a way to remove those files, which is by rebase and rewrite history of commit.

Beside, fix IsReady happens to have bug when I close and reopen the form. Given that I faced this situation quite often, I know that the bug is due to event aggregator is not unsubscribed during OnCloseDialog(). Thus, I just add a unsubscribe function and dispose the event for IsReady‘s view model class.

Lastly, I also add try catch statement and dialog when updating/creating data. This, is to notify user if there is error when updating changes onto database. I also add in new logic for IsCancelled in my repository class.

Row Version Update Upon Saving & The Importance of Clean Code

As my supervisor plan to print details in the system, I am required to and in new attribute in my current context. This attribute would actually be printed and customizable according to liking.

Next, rowVersion of current context won’t update whenever the changes is made in sub attribute. Therefore, I am required to add attribute “ModifiedDateTime” with attribute type of datetime. This attribute is used upon save button been clicked. Whenever a new/existing set of data is updated, ModifiedDateTime would update the latest date and time to the database, thus update the rowVersion.

Alongside of these implementation, I by accident altered my code. Which the alteration cause failure of updating ReadyToPick in my context. Thankfully I did commit my code frequently, hence, I am able to track my changes and revert it to working implementation.

I read through a few pages of Clean Code by Robert C. Martin. I found one interesting topic that more or less related to me which is about Bad Code. Many implemented application/system went down due to bad code. Bugs appears all over the system, crashed the system or even load time kept on increases as the system is not repaired. Major factor is mainly due to Rush, thus making big mess in code. As these mess builds, it may drag the productivity of one team. Eventually, it may cause the need of having more staff to help increase the productivity to its original phase. Which also may result more and more mess towards the project, as everyone trying to increase their productivity under pressured environment.

As the conclusion, I shall make sure my code is clean and easy to understand by others. It will bring benefit towards future me or other team member that is working on the same project. Therefore, Repairing, maintaining or adding implementation can be a lot more efficient.

Code Review on Modules

For the last week of my internship, I’ve performed code review on each module that I responsible for. I’ve clean up some redundant codes and functions, renamed variables to be more understandable. Not only that, I’ve also wrote comments to clarify the implementation logic for some functions.

Secondly, during the code review, I’ve also learned on the usage of Enum, which can be used to replace or represent a group of constants. In my case, instead of assigning magic strings as the line item’s pick status, I’ve replaced it with Enum which contains the variety of pick status.

Lastly, I’ve moved all the magic strings to Parameter class. By using parameter class, it become more maintainable to all the strings inside the whole solution. For example, changing of dialog message will only require to change the string in parameter class.

Implement WrapperService class & Retrieving Other Modules

Adding new attribute “isCancelled” to my ongoing project code. It is a simple checkbox with type of tinyint(1) and logic is validate in boolean. After adding the checkbox, I proceed with fixing initialization on various test class to ensure all tests pass the assertion.

Next, Implement WrapperService to manage multiple transaction for my current module. To support my implementation, I need to reference some context from multiple modules. Therefore I need to retrieve latest implemented modules that is needed in my WrapperService class.

As I am currently working on the project in a group of three. Each of us will works on our own modules. However, there is time we need to use element/attribute from other modules. Hence, we use git to retrieve and merged commits to keep my code at latest version. Sometimes it can be hard, combining latest code from origin git repository and ongoing code might cause few issues. Issue such as refactored variable name, overloading and missing function situation might occurs. Thus, I will need to refactor those code and discuss on which function to be maintain/removed.



Dispatcher Interface for Mocking

As I’ve wrote the integration tests for the pick module which also included the multi-threaded class, I’ve faced problem that the dispatcher in the multi-threaded class fails to mock. Since the multi-threaded class is used to refresh the data grid view within time interval, therefore fails to mock had caused all the integration tests fail to receive the data grid binding source to perform testing.

In order to solve the issue, I’ve decided to use the solution which is implement an interface for the dispatcher for mocking.

I’ve created the interface which called IDispatcher which include the BeginInvoke method. Then, instead of calling dispatcher, I’ve changed it to fire IDispatcher and its method as well. Lastly, in the integration tests, I call the mock for IDispatcher and setup its callback as (Action a) => a() to invoke the method in the multi-threaded class.

Refactoring code based on Integration Tests

There is few thing had been done, as shown as below that involve Test-Driven Development (TDD):

As some of my repository integration tests failed, I then refactor/alter my existing implementation of updating database from ReadData to GetData. ReadData in repository class has AsNoTracking(), where entity will not be tracked. When we make changes on the entity and will not be updated anyhow after calling context.SaveChanges(). By changing to GetData which allow entity to be tracked and any changes will be updated upon context.SaveChanges(). It also make sure latest data been retrieve after they are changed.

There is many repository test been done such as should add new, should update, should remove, should throw error, should roll back. I make sure these context integration tests passes and proceed to ViewModel classes tests. Following tests is mainly to make sure my implementation works fine under multiple scenario.

Most existing ViewModel integration tests failed as refactor of initialization is needed. ViewModel tests passed after few initialization, however there is few test involving DialogServiceMock failed to launch action of dialog in ViewModel class upon debugging. Despite that, the particular scenario works well when I run the application. Therefore, I need to figure out the factor behind failure of my test.

Multithreading with EventWaitHandle

For the week, I’ve found the bug caused the pick module thread fails to refresh the sales order data grid view. This is because when the data grid collection had been initialize with new data at outside of the thread, it will no longer able to update the data grid collection and reflect in UI.

Therefore, I’ve implement EventWaitHandle to solve the issue. When user had clicked on the filter sales order check box, it send signal to the thread in order to refresh the sales order data grid view immediately. While the thread will always loop the function at 5 seconds interval by using WaitOne(TimeSpan.FromSeconds(5));

Not only that, I’ve also set the EventResetMode to AutoReset which will always set the event state to false after received signal.