May 2010 - Posts
if you want to learn more about MEF backstage you can find
more information under the following post:
MEF: What and why
http://blogs.msdn.com/b/hammett/archive/2010/05/29/mef-what-and-why.aspx

new C# IntelliSense version release is available
the version include fix for the Pascal Casing, single selection, and other selection errors.
you can download the source from here.
the install available at Visual Studio Gallery (here).

Published Friday, May 14, 2010 7:53 PM by bnaya | [Edit Post] 









Select Tags...
SaveCancel
תגים:DEV, SELA, VS2010, IntelliSense [Edit Tags]
Comments
# re: C# IntelliSense Update (V2.1)
[Remove this Comment] Tuesday, May 25, 2010 5:28 AM by J.C.Ködel
Don't know the bug report mail, so, here it goes =)
Using version 2.3, open a MVC 2 project, go to a controller. Type in public AR.
The intellisense shows ActionResult as the selected item, but [Enter] inserts an AreaRegistration =\
Selecting the ActionResult (is the first match) using down arrow key and pressing [Enter], it inserts the correct ActionResult class.
# re: C# IntelliSense Update (V2.1)
[Remove this Comment] Tuesday, May 25, 2010 11:11 AM by bnaya
I will check it out
TNX
Leave a Comment
Title
(required)*Name
(required)*Your URL
(optional) Comments
(required)*Remember Me?
Enter the numbers above: *
T 4 beginners – part 4

this is the 4rd post on this series.
in this post we will focus on T4 Class feature control blocks.
you can download the code for this post here.
the series TOC is available here.
What is class feature control block?
class feature control block is one of the T4 extension mechanism that enable reuse of
T4 sections.
using class feature control block you can defines helper properties or methods,
that can be used from different T4 templates.
The syntax of class feature control block
class feature control block is having the following syntax <#+ ... #>
Code Snippet
- <#+ // Start of class feature block
- private int Square(int i)
- {
- return i*i;
- }
- #>
this extension can be use within control block or expression control block, as demonstrate:
- // This is hello number <#= i+1 #>, square <#= Square(i+1) #>
as you can the second expression block is using the Square (class feature method).
Where class feature control block can be define?
class feature control block can be both inline (which mean inside the T4 template)
or external (external helper file), which is more appropriate.
How to define external class feature control block?
to define class feature control block in external file, you should:
1. add text file into your project
(I'm changing the text file extension to *.tt.extension, but you can use whatever extension you like).
update: it is better to name the file extension *ttinclude
2. the class feature control block should be added into the extension file.
How to include external class feature control block?
if you want to use external class feature control block you should add
include directive to the T4 as demonstrate below:
<#@ include file="ClassFeatureBlock.tt.extension" #>
Summary
class feature control block is one of the strength, you can simplify the T4 template
by using predefine reusable methods.
Code sample available here.
T 4 beginners – part 3

this is the 3rd post on this series.
in this post we will focus on basics T4 Control blocks.
you can download the code sample from here.
the series TOC is available here.
T4 Control blocks is where dynamic text is added into the template output,
this is the heart of T4 concept
(C# is the default language but you can change it to VB).
Standard control blocks
A standard control block is a section of program code that generates part of the output.
You can mix any number of text blocks and standard control blocks in a template file.
However, you cannot place one control block inside another.
Each standard control block is delimited by the symbols <# ... #>.
Code Snippet
- /*
- * generate on <# WriteLine(DateTime.Now.DayOfWeek.ToString()); #>
- */
the above snippet include control block that write the current day of week.
this is the code it generate:
Code Snippet
- /*
- * generate on Friday
- */
Expression control blocks
An expression control block evaluates an expression and converts it to a string and insert it into the output file.
the syntax of expression control blocks is using the <#= … #> syntax instead of <# … #> .
Code Snippet
- /*
- * generate on <#= DateTime.Now.DayOfWeek.ToString() #>
- */
both the WriteLine and the ; were omit.
the following snippets is using expression control block combine with standard control block which
use for iterations:
Code Snippet
- <#
- for(int i = 0; i < 4; i++)
- {
- #>
- // This is hello number <#= i+1 #>: Hello!
- <#
- }
- #>
lines 1-3, start the iterations.
line 5, write text block which embedded expression control block that write the current iteration count.
lines 6-8, close the iterations.
the output will look like:
Code Snippet
- // This is hello number 1: Hello!
- // This is hello number 2: Hello!
- // This is hello number 3: Hello!
- // This is hello number 4: Hello!
Summary
the control block is what T4 is doing, create dynamic section
T 4 beginners – part 2

this is the second post on this series.
in this post we will focus on basics T4 Directives.
you can use the code sample of the previous post here.
the series TOC is available here.
Directive syntax:
any of the directives is using the following syntax:
<#@ DirectiveName [ParameterName = "ParameterValue"] #>
it start with <#@ follow with the directive name,
includes zero or more name value parameters and
end with #>
All parameter values must be surrounded by double quotation marks.
If the value itself contains quotation marks, they must be escaped with the \ character.
For example: <#@ DirectiveName Parameter=" \" quote \" " #>
T4 provide the following Directives:
Output
- <#@ output extension=".cs" encoding="UNICODE" #>
the output directive define the encoding and extension of the generated file.
Template
The template directive allow the following parameters (all the parameters are optional):
- <#@ template debug="true" language="C#" #>
- language: the language define the programming language of the Control blocks within the T4
(this is the language that used to process the T4 and it has nothing to do with the T4 output).
example: language ="C#" or language="VB" - debug: indicate whether the template can be debug
(we will cover debugging on later post) - inherits: is one of the possible ways to extend T4
(we will discuss the inherit parameter in further post).
Assembly
The assembly directive define the T4 dependencies.
for example the following T4 Control block:
- <#= System.DateTime.Now.ToString() #>
will result with exception unless the following assembly directive will be define:
- <#@ Assembly Name="System.Core.dll" #>
The assembly name should be one of the following:
-
The file name of the assembly, if it is located in the same directory as the text template
-
The absolute path of the assembly
-
The relative path of the assembly (with respect to the directory in which the text template is located)
-
The strong name of the assembly, as in the GAC.
-
You can use the $(variableName) syntax to reference Visual Studio or MSBuild variables, and %VariableName% to reference environment variables.
Import
the import directive behave as using statement for the T4 control block.
as the case with other directive it add nothing to the T4 output.
using the following import directive:
- <#@ import namespace="System" #>
you can replace the following full path control block:
- <#= System.DateTime.Now.ToString() #>
with the following control block:
- <#= DateTime.Now.ToString() #>
Summary
the T4 directive is fairly intuitive and simple to use.
Resources
http://msdn.microsoft.com/en-us/library/bb126421.aspx
http://www.codeproject.com/KB/codegen/T4CustGtc.aspx
T 4 beginners – TOC
The current available posts on the T 4 beginner series (T4 template) are:
T 4 beginners – part 1
this is the first post of a series that will focus on T4 template.
this post discuss the T4 in general while the following pose will focus on the T4 practice.
the code for this post can be found here.
the series TOC is available here.
What is T4 template?
T4 is sanding for Text Template Transformation Toolkit.
T4 is all about automating code or content generation.
the usual extension for T4 files is the *.tt
When to use T4?
whenever you identify repeatable pattern of code or content, that can be
infer from other item within the project (like the case of code that is
generate from a designer, or entity framework self tracking entities),
you should consider to automate it by using T4,
instead of writing it over and over again.
What are the benefits?
T4 can bring the following benefits:
- speed-up the development process.
- increase the code consistency.
- make is easier to apply best practice.
- it is easy to start with prototype then modify it's dynamic sections.
- T4 is very powerful concept because it is using C# or VB.net as it script language,
therefore it is lower the learning curve.
The T4 structure
T4 are composed of the following parts:
-
Directives - elements that control how the template is processed.
<#@ output extension=".txt" #>
-
Text blocks - content that is copied directly to the output.
-
Control blocks - program code that inserts variable values into the text,
and controls conditional or repeated parts of the text.
<#= System.DateTime.Now.ToString() #>
The syntax
T4 syntax resemble old ASP 3 syntax, it is an hybrid of text and <# control blocks #>
Code Snippet
- <#@ template debug="true" hostSpecific="true" #>
- <#@ output extension=".cs" #>
- <#@ Assembly Name="System.Core.dll" #>
- <#@ Assembly Name="mscorlib.dll" #>
- <#@ import namespace="System" #>
- <#@ import namespace="System.Security.Principal" #>
-
- using System;
- //using System.Security.Principal;
- namespace Bnaya.Samples
- {
- // Last update on <#= System.DateTime.Now.ToString() #>
- public static class Utils
- {
- public static void GenerateBy ()
- {
- Console.WriteLine ("The code was generate on <#= Environment.MachineName #>");
- Console.WriteLine (@"by <#= WindowsIdentity.GetCurrent().Name #>");
- }
- }
- }
we will discuss each T4 command in the following part of the series, but in general:
lines 1-6: are directives.
Line 12 and lines 17,18: has control blocks.
any thing else is just text blocks.
the project will look as follow:
the generated HelloWorld.cs will look as follow:
Code Snippet
- using System;
- //using System.Security.Principal;
- namespace Bnaya.Samples
- {
- // Last update on 5/18/2010 7:29:10
- public static class Utils
- {
- public static void GenerateBy ()
- {
- Console.WriteLine ("The code was generate on BNAYA-PC");
- Console.WriteLine (@"by Bnaya-PC\Bnaya");
- }
- }
- }
notice that the date at line 5 and part of the text at lines 10, 11 was generate by the T4.
What do you need to get it operating?
T4 is part of Visual Studio 2010, which mean that you can add T4 template to any of
your project (just by adding item and selecting one of the T4 item template).
what's missing is the IntelliSense and syntax coloring capability.
you can add the syntax coloring and IntelliSense by installing the following VS extension gallery:
http://visualstudiogallery.msdn.microsoft.com/en-us/60297607-5fd4-4da4-97e1-3715e90c1a23
Summary
T4 can boost your coding productivity by automating repeatable patterns.
C# IntelliSense Update (V2.1)

new C# IntelliSense version release is available
the version include fix for the Pascal Casing bug
you can download the source from here.
the install available at Visual Studio Gallery (here).

Rx Contrib has new version (1.3)

Release Notes
- Bug Fix
- BufferWithTimeOrCount with flexible time period enabled
when ever the time period elapsed it will use the lamda for getting the next period
this feature is a join effort of Amir Shitrit and Bnaya Eshet
Code Snippet
- IObservable<long> obs = Observable.Interval(TimeSpan.FromMilliseconds(10));
- var bufferQueue = obs.BufferWithTimeOrCount(() => TimeSpan.FromMilliseconds(500), 10);
you can download the source or binaries from here.
you can install the Rx (the Rx framework is a prerequisite) from here.
C# IntelliSense extension for VS 2010 V1.9

new release to my C# IntelliSense extension.
this is a major release that focus on bug fix, tooltip support and styling.
you can download the extension from Visual Studio Gallery here.
the extension code is available here.

Performance tips

recently I was working on the Reactive Queue (which is part of the Rx Contrib).
the requirement for the Reactive Queue was to to achieve the highest throughput possible
for each queue provider (2,000,000 messages of 500 byte per second using Concurrent Queue provider
running on simple quad server).
while working on this project I was encounter the the following performance hits:
1. ManualResetEventSlim
there is a big difference between using the old ManualResetEvent Vs. ManualResetEventSlim the hit
factor can go up to 2 or 3 times slower if you having a very short synchronization period
(the ManualResetEventSlim does not escalate to kernel mode on short period waiting).
2. ManualResetEventSlim.WaitHandle
do not expose the ManualResetEventSlim instance using the WaitHandle, as shown in the following code snippet:
Code Snippet
- private readonly ManualResetEventSlim _syncWhileWorkingHandle;
- public WaitHandle WaitWhileWorkingHandle
- {
- get { return _syncWhileWorkingHandle.WaitHandle; }
- }
consuming ManualResetEventSlim though its WaitHandle property does not gain
performance improvements benefits.
if you do want to expose synchronization API, use the following code:
Code Snippet
- private readonly ManualResetEventSlim _syncWhileWorkingHandle;
- public void WaitWhileWorking()
- {
- _syncWhileWorkingHandle.Wait();
- }
3. CancellationToken
when ever using CancellationToken:
Code Snippet
- var cancel = new CancellationTokenSource();
- Task.Factory.StartNew(Dequeuing, cancel.Token);
do monitor GC collect counter, especially the 3rd generation collection.
trying to use this technique I got a huge hit (3.5 times slower), because of
extra (multiple) GC 3rd generation collection.
instead of using the CancellationToken the Reactive Queue is using old good
dispose pattern.
4. Thread.Yield()
do consider to use Thread.Yield().
putting it on the right places may improve overall performance,
but you have to be careful and measure the actual performance, because
doing it on the wrong places may result as performance hit.
Summary
performance improving required continuously measurement which should
be compared with historical measurement.
be careful with synchronization and do use the new .net synchronization types like ManualResetEventSlim.
Rx Contrib
release V1
Rx Contrib is ongoing effort of community extensions for Rx.
you can can find the project at http://rxcontrib.codeplex.com/
Current features are:
ReactiveQueue: is ISubject that does not loose values if there are no subscribers.
The reactive queue is using the IQueueAdapter for injecting concrete
queue adapters (which mean that it can be extend to support other queues).
the out of the box implementation is using ConcurrentQueue which have extreme throughput.
(up to 2,000,000 messages of 500 byte per second on basic quad server).
It is also support Weak Subscribe option which holds the subscriber using week reference,
(those subscription will be removed after it been collected by the GC).
Add contribution
If you want to contribute to this efforts, you can contact bnayae@sela.co.il
Credits
The Reactive Queue, Weak Subscription, Unsubscribe Disposable are join effort of Bnaya Eshet and Yuval Mazor.
Yuval Mazor blog: http://blogs.microsoft.co.il/blogs/yuvmaz/
Read more about Rx
http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/02/25/rx-for-beginners-toc.aspx