CQRS Pattern

For the past week , I have read about applying the CQRS Pattern in a project . CQRS stands for Command and Query Responsibility Segregation . This design pattern is aimed to segregate the read and update operation in a project .

The CQRS pattern separates read and write into different models , using commands to update data , and queries to read data . The presentation layer for commands is typically separated into Validation , Command , Domain and Persistence . Queries is separated into a read-only layer to generate Data Transfer Objects .

The benefits of using the CQRS pattern is simply like the pattern named itself . The separation of command and query models , resulting in a more simplified design in a project .

However , this design pattern is more suitable for larger projects where the requirements for read and write operations that is more complex and challenging . For simple CRUD operations, we might not need to implement this pattern . 

Collection types

The past week , I have been working on creating features in Tong Hin’s inventory module , and used many different types of collections interchangeably to retrieve data such as IEnumerable , ICollection , IList and IQueryable .

IEnumerable vs ICollection vs IList vs IQueryable

IEnumerable is the most basic type of list container to store data . It has no order and does not allow us to modify the set . An IEnumerable supports the where clause to filter elements , but it does not hold the count of elements as we need to iterate over the elements with “foreach” to get the count .

ICollection is a modifiable set and allows us to edit the elements in the collection with .Add , .Update and .Remove . Such as IEnumerable , ICollection also has no order and does not allow us to sort the data without changing it to a list with ToList() .

IList is an ordered set and provides an object indexer to allow the user to access the collection with square brackets like myList[x] . It is useful when we want to sort elements in the collection . I used it to sort dates to show the latest data in a table .

IQueryable is used when we want to run an Ad-hoc query , an Ad-hoc query is created to provide a specific record set from the database , usually defined to serve for a particular purpose . I used it to include different entities from other classes in one query .

In summary , each function has its own characteristics and is adaptable to different scenarios , and it is better for us not to memorize but understand how things work .

Maintainable coding

The past week , I have been doing more refactoring in the Tong Hin’s inventory module , to allow maintainable code in the project .

Writing maintainable code simply means code that is easy to modify, read or extend , not just to be understood by ourselves so the program “runs” but easy to be read by current/future users involved in a project . In other words , not causing an immense grief to other group members .

Aside from the classic practices for newer developers such as writing comments , giving meaningful variable names , writing testable code , clean coding etc . Trust me , it really ain’t enough .

Any software developer will get into these situations where we face an “endless loop” of errors when creating a feature . Fixing an error from one part and another pops up within this feature , while repeatedly adding more messy if-else null checking statements to avoid/fix each error , and “forcing” maintainable code while losing our sanity on top of it.

In these situations , we should realize that simplicity is part of the answer , that no user interaction is also the best user interaction . Ask ourselves if it needs to be done at all ? Because throwing away 30% of the problem for 70% of the solution may fix the problem .

With that said , maintainable coding is really easier said than done . It requires us to think out of the box , which cannot be taught to anyone . There are cases where we can avoid/fix the “endless loop” of errors by trying weird things , adding/removing a button , twisting and avoiding the root problem and out of nowhere get the answer .