RSS

Monthly Archives: May 2018

C# Dynamic-Typed Parameter Trap

A little tricky lesson today in C#.

Passing a dynamic-typed parameter to a method that has defined return type (ie: string, int, etc) will not return the defined type, instead the method will return dynamic type.

class Program
{
	static void Main(string[] args)
	{
		FooBarClass fooBar = new FooBarClass();

		var result1 = fooBar.Foo("Foo");

		dynamic bar = "Bar";
		var result2 = fooBar.Foo(bar);
	}
}

public class FooBarClass
{
	public string Foo(string fooArg)
	{
		return "This is Foo";
	}
}

In the above example, type of result1 is string, but type of result2 is dynamic, even though they both call same method that return string type.
result1
result2

This is because compiler do not evaluate dynamic type until runtime therefore it can’t determine return type at compile time.

This is important to point out because if we apply extension method to result2, it will throw exception at runtime, not compile time.

Example of extension method.

public static class FozBazClass
{
	public static void Foz(this string fozArg)
	{
		fozArg = "Test..." + fozArg;
	}
}
result1.Foz(); // this will execute normally
result2.Foz(); // this will throw exception

result2 exception

Just another reason we should avoid dynamic type.

Advertisements
 
Leave a comment

Posted by on May 29, 2018 in General

 

Tags: , , , ,

Event Sourcing Design Pattern

What is Event Sourcing?

Is a architecture design pattern that uses append-only store to record full series of events that happen on specific data. The store also acts as publisher to all subscribers whenever an event is recorded.

Characteristics

  • Append-only store. Some implementation may be different.
  • Event store as system of record.
  • Materialized object is constructed from events.
  • Provide audit trails.
  • Fast performance.

Considerations

  • Undo is adding another event. Perform undo can only be done by adding compensating event since update is not allowed in Event Store
  • Migration to new format. All existing events must be updated.
  • Consistency. Multi-threaded applications may store events in the same event store. The consistency and order must be maintained.
  • Lack of transactions. Event store by its nature is not transactional and it’s eventual consistency. It’s possible to have event to reduce the inventory count while customer is placing order for the same item.
  • Performance. Replaying large amount of events can slow down the result for materialized view.

Reference
Microsoft Docs

 
Leave a comment

Posted by on May 25, 2018 in General

 

Tags: , ,

Angular – Call Child Component’s Method in Parent Component’s Template

You have ParentComponent and ChildComponent that looks like this.

parent.component.html
Image 2018-05-22 at 11.21.07 PM

parent.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
  constructor() {
  }
}

child.component.html

<p>
  This is child
</p>

child.component.ts

import {Component} from '@angular/core';

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  constructor() {
  }

  doSomething() {
    console.log('do something');
  }
}

When serve, it looks like this:
Image 2018-05-22 at 11.00.20 PM

When user focus on ParentComponent’s input element, you want to call ChildComponent’s doSomething() method.

Simply do this:
1. Give app-child selector in parent.component.html a DOM variable name (prefix with # – hashtag), in this case we call it appChild.
2. Assign expression value (of the method you want to call) to input element’s focus event.
Image 2018-05-22 at 11.22.59 PM

The result:
Screen Recording 2018-05-22 at 11.27 PM

 
Leave a comment

Posted by on May 22, 2018 in General

 

Tags: , , , , ,

SQL Server Concurrency Effects

Lost Update

Happens when one transaction update overwrites another, causing the update to be lost.

Dirty Reads

Reading uncommitted records.

Phantom Reads

Reading while another transaction is adding new record, result in different number of rows being returned.

Repeatable Reads

When the same query to read is executed, it will return the same result.

Reference

MSDN

 
Leave a comment

Posted by on May 22, 2018 in General

 

Tags: , , , ,

Application Insights Intrumentation Key in Web.config

When using Azure Application Insights in ASP.Net application, by default, Visual Studio insert IntrumentationKey in ApplicationInsights.config.

To allow multiple environments tracking, move IntrumentationKey to Web.config by following this steps:

  1. Remove IntrumentationKey from ApplicationInsights.config. If you have MVC application, don’t forget to modify ApplicationInsights’ script (usually in View\Shared\_Layout.cshtml), replace:

    {instrumentationKey:"your instrumentation key"}
    

    with:

    {instrumentationKey:"@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey"}
    
  2. Add new app settings for IntrumentationKey in Web.config under <appSettings>

    <add key="InstrumentationKey" value="your instrumentation key" />
    
  3. In Global.asax.cs, Application_Start() method, add:

    Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = System.Web.Configuration.WebConfigurationManager.AppSettings["InstrumentationKey"];
    

That’s it for the configuration changes. Everything else is the same including tracking custom event or page view.
With this configuration, you will be able to define InstrumentationKey in Release management for each environments.

 
1 Comment

Posted by on May 18, 2018 in General

 

Tags: , , , , , ,

Configuring Azure Application Insights for Angular 2+ App

1. Obtain InstrumentationKey from Azure.
-Create new Application Insights resource.
1

2

-Enter Name, Application Type (choose General for Angular app) and choose Resource Group.
3

-Wait for Azure to finish creating the resource.
-Go to the resource detail, by clicking name of the resource, select Overview menu and expand Essentials information to get the Instrumentation Key.
4

2. In Angular app, install applicationinsights-js package.

npm install applicationinsights-js --save --save-dev

3. In Angular component you want to track, add import statement and call AppInsights.downloadAndSetup() method in the constructor.
Normally, we only need to call AppInsights.downloadAndSetup() once in entire application lifecycle so it make sense to put this in app.component.ts.

4. To track each telemetry, call available methods in AppInsights class. In the example below, we track page view after the component initialized.

import {AppInsights} from 'applicationinsights-js';

@Component({
	selector: 'test-app',
	templateUrl: 'test-app.component.html',
	styleUrls: ['test-app.component.scss']
})
export class TestAppComponent {
	constructor() {
		// Download and setup Application Insights
		AppInsights.downloadAndSetup({instrumentationKey: 'xxxx-xxxx-xxxx-xxxx'});
		this.Init();
	}

	public Init(): void {
		// Example of how to track page view
		AppInsights.trackPageView('TestAppComponent');
	}
}

For more details on what Application Insights can track and methods to call, please see:
https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md

After everything is configured, browse your application for few minutes and let Azure Application Insights does its magic. You should be able to go back to Azure portal and see some activities, like this:
5

 
Leave a comment

Posted by on May 18, 2018 in General

 

Tags: , , ,

Columnstore Index

  • New in SQL Server 2012.
  • Stored by columns, a column-based index, instead of row-based like in traditional index. For example, if row-based index is consisted of Firstname and Lastname columns, the column-based index would have 2 different indexes: Firstname in its own index and Lastname in its own index.
  • The index is compressed, allowing high performance.
  • The compressed data is stored in-memory, reducing needs to read off the disks.
  • Compression ratio is generally high because the same data type in a column.
  • Generally a better choice for wide table with many columns, as commonly found in data warehouse tables.
  • Clustered and non-clustered index.
  • Can be combined with row-based index.

Reference
Microsoft Docs

 
Leave a comment

Posted by on May 16, 2018 in General

 

Tags: , , , , ,

 
%d bloggers like this: