Tuesday, June 30, 2015
Domain Driven Design, CQRS & Event Sourcing
Tuesday, June 16, 2015
Creating Nuget Packages using Visual Studio 2015
Visual Studio 2015 (currently RC) lets you create nuget packages extremely easy!
First create a new Class Library project:
Notice that it is under ‘Web’ category.
Now we’ll enable the nugget package creation just as written in the comments above.
I just love their UX in this window.
Now we’ll need to fill the package details in the project.json file.
The project.json is almost the same as the .nuspec file. We can include the package dependencies and the target frameworks.
Now if we compile the project and navigate to the solution directory we’ll see the nuget package in the artifacts folder.
Good luck
Friday, June 12, 2015
Aligning Elements using CSS3 “Flex” options
CSS3 introduces the “Flex” options that makes aligning elements extremely easy! It is currently supported in all the major browsers (not supported in IE 11 and below);
Let’s say that I want to align four elements in a line:
The CSS:
.container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
}
.item {
background: green;
padding: 5px;
width: 150px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;
-ms-border-radius: 30%;
border-radius: 30%;
}
The only new property here is “display: flex”. Not only that flex aligns the elements in a single line it also makes them responsive when the container size changes (overriding the element’s explicit width property;
If you wish to keep the element’s original width you can add the flex-wrap:wrap property which will break the line if needed:
You can reverse the order of the elements that break the line using flex-wrap:wrap-reverse:
You can align the whole line to center or to the first/last item (flex-start/flex-end) using the justify-content property. By default it is set to flex-start:
.container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
There are two more options for justify-content: space-between and space-around which adds equal spaces between each element:
.container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
If you choose space-around then the first and the last elements will also have the same spaces from the borders.
Vertical Alignment
You can vertically align the elements using flex-direction:column.
Using align-items you can control the vertical starting point (the same like justify-content):
.container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
flex-wrap: wrap;
align-items: center;
flex-direction: column-reverse;
}
So far we’ve applied the flex properties only to the elements container. We can also apply some flex properties directly on the elements, like making some item be 3 times bigger than the rest:
.container > .grow {
flex-grow: 3;
}
I’m using the flex-grow property on the wanted item and setting it’s value to 3 as for 3 times bigger.
Flex has more properties that can help you easily align your elements . Go ahead and try!
Wednesday, May 20, 2015
Chrome Developer Tools Tips and Tricks
In the last ALT.NET meetup @Shay Friedman talked about “Chrome Developer Tools Tips & Tricks”. All the audience, myself included were amazed from the features that Shay has shown that neither of us knew.
The Tips & Tricks:
1. Functions navigation in JavaScript source file:
You’re all familiar with this window:
Some of you might know that if you hit Ctrl + P you can open a JS source file from your workspace. However did you know that if you hit Ctrl + Shift + P you can navigate to a function which is included in the opened file!
No more endless scrolling!
2. Style search
This window is also pretty common.
We scroll down in order to figure out which Style messes our element’s css. However did you notice that you can Search (!!!) for a style property or value???
3. Color picker
This is a color property.
Usually in order to change a color you will double-click the area and manually write the new color. However if you click on the colored box itself you will get a color picker dialog!
And if you move the cursor over the page you could even pick a color!!!
4. Disable Cache and reload cache
You remember that time tried your best to hit F5 to refresh the page but had no luck? And then you realized that your cache wasn’t disabled. First and foremost disable it. However if for some reason you still think that your changes are not loaded to the browser you can right click the refresh icon (while the developer tools is open) and try the Hard Reload or the Empty cache and Hard reload it. That should do it.
5. Filtering the network files
If you click on the filter icon in the network tab you could filter all the request’s files by a wanted file type. Hold Ctrl and you can multi select the file types
6. Black box script in Call Stack
When you debug your app and hit a break point you’ll sometimes want to check the call stack window to see previous calls.
Since I use angular this call stack has a lot of angular’s calls which I couldn’t care less about them. However, if I right-click on the angular script file I can select it as a Blackbox script.
And now I won’t see its calls in the call stack window J
If you don’t know some of those features you don’t need to feel embarrassed. Google uses tons of UX best practices in all its products in order to reveal all the available features, but not in Chrome. Here I think they actually tried to hide some of those features. J
If you have more tricks and tips then feel free to comment
Thursday, April 9, 2015
Improving Angular.js performance using One Time Binding
In the previous post I explained how the $digest loop works and how to increase performance using the ng-model-options directive. In this post we’ll take a look at ‘one time binding’ feature.
As I mentioned before the $digest loop goes through all the registered watches every time in order to check whether they need to be updated. However not all our scope variables and models should have binding or be updated after they have been initialized, for example: a logo path, a title, names, some constant options that we used ng-repeat for them and so on. All those variables should be initialized and not to be updated until a full page refresh.
One time binding is a feature that was introduced along with ng-model-options in Angular 1.3. In order to set that a scope variable should not be updated all you have to do is add :: before the variable name:
<input type="text" ng-model="name" value="text" />
{{::name}}
{{name}}
Adding it before the scope variable tells Angular not to register a watch on the variable and so decreases the amount of watches for the $digest loop to check which leads to increase in the app’s performance.
Enjoy
Improving Angular.js performance using ng-model-options
Every app has a search ability. Some apps wait for the user to hit “Enter” and then send the search string to the server, others (especially when using Angular) bind every user’s key stroke to a request to the server. In my opinion the second option has better user experience since the user starts to receive results while he is typing and most likely will get what he is searching for before he finished typing the whole search string and he is no more required to press another button after he finished typing.
However if you have a large Database and your search request take more than several milliseconds then there is no use to send a request to the server after every keystroke – the user will not get the relevant results. So how do you control when to send the request to the server?
Ng-model-options
Ng-model-options is a directive that was introduced in Angular 1.3. It has some really nice features:
updateOn:
With the updateOn parameter, we can define which events our input should be bound to. For example, if we want our model to be updated only after the user removed the focus of our input element, we can simply do so by applying the ngModelOptions with the following configuration:
<input type="search"
ng-model="searchQuery"
ng-model-options="{ updateOn: 'blur' }"/>
Debounce:
debounce defines an integer value which represents a model update delay in milliseconds. Which means, if we take the example mentioned above, that we want to update our model 300 milliseconds after the user stopped typing, we can do so by defining a debounce value of 300 like this:
<input type="search"
ng-model="searchQuery"
ng-model-options="{ debounce: 300 }" />
Advanced options: we can configure how the update delay should be done for different events. Controlling the debounce delay for specific events can be done by defining an object instead of a integer value, where keys represent the event name and values the debounce delay. A delay of 0 triggers an immediate model update.
The following code generates a model update delay of 300 milliseconds when the user types into our input, but an immediate update when removing the focus:
<input type="search"
ng-model="searchQuery"
ng-model-options="{ updateOn: 'default blur',
debounce: { 'default': 300,
'blur': 0 } }">
How does it work?
The ng-model-options controls how the ngModel is updated.
By default Angular automatically registers a watch for every scope variable for updating the binding, the update itself happens due to the $digest loop that is triggered at every key stroke. The $digest loop checks if the model has a new value and if so it updates every model instances in the app without making us add event listeners or manually update the DOM.
As you can see this means that the $digest loop has to go through all the registered watches on the scope at every key stroke and depending on the watches callbacks that can be very expensive. So the debounce triggers the $digest loop after the defined milliseconds allowing the user to type a meaningful search phrase and reduce the amount of the $digest loops.
In the next post we’ll see how ‘one time binding’ also increases our app’s performance.
Ng-Hint – Intellisense for Angular.js
Angular is great. I love it and in my opinion is the best thing that happened to the web development community in the last several years. However as much as I love Angular there is nothing more annoying than searching all other your code while trying to figure out why the #@$% it doesn’t work! And then finally you see it - you forgot a module or misspelled ‘ng-repeat’. If only Angular would have some sort of intellisense what would warn me when I make those mistakes.
Is a great library that scans your code and writes warnings to your browser console.
It has several modules:
- angular-hint-controllers - Warns about use of global controllers and hints about best practices for controller naming
- angular-hint-directives - Hints about misspelled attributes and tags, directives and more: <ul ng-repaet="point in points">
<li>{{point}}</li>
</ul>
The console will show this hint:
- angular-hint-dom - Warns about use of DOM APIs in controllers. Angular best practice is to avoid using DOM API from the controller so that this code will trigger a warning:
angular.module('sample').controller(function(){
//Accessing the DOM API `document.getElementById()` triggers an AngularHintDOM warning
var list = document.getElementById('list');
var newListItem = document.createElement('div');
newListItem.innerHTML = 'Item 1';
list.appendChild(newListItem);
});
angular-hint-events - Identifies undefined variables in event expressions. For example if you have a ng-click targeting some function that doesn’t exists in your controller :
<a ng-click="increments">Count!</a>
$scope.increment = function(){
++$scope.count
};
The docs say that this code should trigger a hint, unfortunately it didn’t work for me– they have an open issue for that.
- angular-hint-interpolation - Notifies of undefined parts of interpolations chains and suggests available variables
- angular-hint-modules - Identifies missing module namespaces, undeclared modules, multiple uses of ng-app and more:
var myApp = angular.module('myApp', []);
I’ve also included :
<script src="src/angular-messages.js"></script>
which has a module in it. So the console has a warning:
The first warning is a known issue and should be ignored.
This is a great library however is still has some open issues and the worst part is that if you reference the script hint.js it will crash your app . They have several open issues addressing this problem. I truly hope that they would fix it soon since it is a great library. Meanwhile you could add the script before you commit your work just to see if there are no warnings and you have applied all the best practices.
Hint: Keep your eye on Angular-Hint!