Phone : 964569569   |   Mail :info@sharpdev.net

Blog

Our Thoughts and Ideas.

Reference unsigned NuGet package within a signed assembly

If you want to add a reference to an unsigned NuGet package from a signed assembly you need to take a few steps to make it possible.
In summary, what we need to do is, take the assembly from NuGet we want to reference, merge its dependencies onto it, sign it and repackage it.

tools needed:

Let’s get started:
  1. Open NuGet Package Explorer
  2. Open a package from an online feed

  1. Search for it and click open

  1. Save as into a known location
  2. On the package metadata, download all the dependencies by clicking on each one of them in the dependency list and then the download link
  1. locate all the dependencies dlls as well

  1. Extract the resulting main .nupkg file (as a zip) into a folder
  1. Locate the assembly file, keep that location

 

  • Open ILMerge GUI
  • Add the main assemblies and all the dependencies assemblies using the add assemblies button

 

  1. Tick the Sign with key file checkbox and select the strong name key file
  2. Now back to the package explorer, we want to replace the package’s assembly with the newly merged one and also change this new package’s metadata, to distinguish it from the original package
  1. we’re going to add a .Signed.Merged suffix to the name
  2. on the EDIT menu click Edit Metadata
  3. change only the Id field, by appending .Signed.Merged
  4. on the content drill down to the .dll and right click Replace with and select the newly merged assembly file
  5. save the package and you’ll see the file name is automatically updated

 

  • Place the new package in your own package repository
  • Add a reference to the new package from Visual Studio using the Manage NuGet references or the Package Manager console and you’re done!

 

JavaScript mini TPL library: RunAsync

Having learned about HTML5 Web Worker functionality, and that most of the modern browsers implement it, together with the File API BlobBuilder capability I thought about building a library that resembles C#’s Task Parallel Library, allowing JavaScript to be executed in a background thread, but at the same time, adding the ability to dispatch execution to the UI thread and also invoke callbacks and continuations to a “task”. I figured the dispatcher functionality was important since usually whatever is executed in the background can/probably will need to interact with the UI while executing. Also this opens a door to use UI frameworks such as jQuery and/or any other DOM access. The problem of passing state while dispatching to the UI thread still exists, but it’s on my queue to be solved.

Do check GitHub’s page: https://github.com/ricmrodrigues/runasync 

Usage:

Task:
Dispatch execution to the UI thread (from within a Task.run or continueWith function callback):
Promise:

Performance:

Running code synchronouslyhttp://jsfiddle.net/UejYX/3/
Example:

 

Moq and IoC experiments

Hello all. It’s been a while since I wrote something, why not restart with some interesting piece of code? I’ve embraced the whole TDD (Test Driven Development) movement and felt the need to use a mocking framework. What is does for you is basically mock the dependencies of the code you want to test, this way, you’re not worried about your code accessing a service or a database, you just test your code and ignore the rest, as you’re supposed to. Moq does this pretty well, imagine you have:

 

To test your code, you don’t need to execute the logic within GetValue, because your test subject is something else, so, you want to mock the behaviour of GetValue, so it returns a predefined value, and move on. You can do it with Moq, like this:

 

This creates a new mocked instance of an object implementing your interface, IMyClass, and sets the return value of its method to what we need, so in order to use it, we just use the Object property of the mocked object, which gives us something of type IMyClass:

 

Use case for this? imagine you have this code to get tested:

 

This way, you could easily test this, right? Now, to go the extra mile with this, you could even use something like AutoFixture, that provides a plugin for Moq, so you can’t use IoC to inject these mocked instances onto your test subjects classes, so easily as:

 

Using MEF to extend applications

I’m back for another post, haven’t posted for a long time now and hope to never do so again.

Getting to the point here, today I started using MEF, after months of tech talks, webcasts and tutorials, and I have to admit, it’s really easy, clean and effective.

My scenario is this:

I have an application that needs to have a caching system, which will be implemented on a different project, and needs to abstract itself from the caching system being use.
So I coded an interface, obviously, to define what does the caching system need to implement.

Then I created two example caching classes that implement this interface, which both reside on different visual studio projects:

So, we have an interface named IPersist, we have two classes that implement this interface, and now we need to dinamically load one of these classes on our application, so we cannot add a reference to neither of them, because that way we would have to change our code each time we changed class, so how do we do it?

Our application code looks like this:

What do we have to change to use MEF?
Lets assume the assembly file of the class that implements our cache system will be on the executing directory of our application (it could be elsewhere, or the class could be on the same assembly as our application even, but on this example it will be on the executing directory).
First we have to specify that our classes “export” themselves:

So we added an Export attribute, specifying which type we are exporting, and this is important when things get more complicated, when you are exporting and importing lots of types.

Then on our application, we must create a catalog, on this case a DirectoryCatalog, since we are getting our exported classes on a directory, and then we create a CompositionContainer and pass our catalog as a parameter. The CompositionContainer is responsible for browsing the provided catalog and resolve connect the dots (match the exported classes to the imported ones on our application), and the code we need to do this is:

Which is, create a property that imports something of the type IPersist, create the catalog for the current directory, create the container which will resolve the export/import stuff, and order this resolution with the ComposeParts(this) method call, and finally compile one of our IPersist classes, put it on the executing dir and run our application.
Bam! Magically all is tied up and we can switch cache systems without compiling, just switch the dll file and you can change, just like that, our caching system implementation.

So, how powerful is this?

Please give feedback.

Thank you all, take care.

Convert DataTable into POCO using attributes

Hello there.
A while ago, I had this issue, I have an older system which *still* works with the DataTable class, but still, I want to create my POCO (as we call it these days, the plain old CLR object) and work with it, LINQ it, etc, instead of the ugly DataTable object.
So I decided to do a method (that could be used as extension method on the DataTable class) that converted my DataTable to my POCO, on the fly.

The problem: mapping DataTable columns to POCO properties.
The solution: create a custom attribute class so I can define the correspondent column of the DataTable for each POCO property.

Here is the code:

C#

Usage

 

POCO

 

The magic

 

VB.NET Usage

 

POCO

 

The Magic

 

C# Enum with char valued items

Hello again!
I’m finally back, and still I haven’t fulfilled my promise of writing about .NET 4 and VS2010, but I’m working on it, and maybe I’ll talk about VS2010 testing features as well and also Entity Framework 4 new hot stuff!

Focusing on the subject, what am I going to write about the Enum type?

– Can we assign an Enum item to anything that’s not of the int type?
– Why would I even bother thinking about this?
– How can I handle converting something to Enum and back to something again?

First of all, YES, we can assign an Enum to something else, a char!
And how?
Just like you’re thinking about it, yes:

 

And why would I think about this?
Have you ever had to write some DAL code or mapping and existing database to and ORM, say Entity Framework, and you had fields which contained, for some particular reason, a list of controlled chars, which aren’t related to any other table, like a field called State which had the possible values, ‘R’ for Ready, ‘P’ for Pending and ‘C’ for cancelled, and you want to have some nice and type-safe way of manipulating this field in code and at the same time a generic way, which can be used everywhere, well, you can do it using those chars on an Enum type:

 

And how can I handle this? I’ll sure try to do a State.ToString() and it will return me a number, why??
Because, actually, you can’t have an Enum item with an associated char, but .NET lets you do this, and internally it converts your char to it’s equivalent int representation (ASCII).
So, now you’re thinking, so now what? How can I get my char??
Simple enough, just cast the Enum item to a char first, and then you get it’s char representation:

This way you can extract the char from the int, and get your value!

This is for persisting your data to your datasource (convert the Enum item to the value that your datasource is expecting, the char).

But now you need to convert your char to the corresponding Enum item, when you get your char field from your datasource, right?
How can this be done?
Well I’ve coded a method to do that, with generics, lets see:

Code at Pastebin

 

So what you do here is accept a string (could be a char, it’s just to make it simplier, since many ORMs map a char on the database to string, not chat), and then you check your parameters, ir they’re not null, and if the length of the string is one (which matches a char), and then, you use the ToObject method from the Enum type, that accepts the return type you want to get, and the corresponding Enum item value (int or char) to convert to.

And that’s it, you can use chars with an Enum object, isn’t this awesome?
When i got around this, I just thought about the lots of times that I needed it…

Hope this helps you as much as it helped me.

Be back soon!

C# Word document manipulation without MSO Word installed!

Hi there folks!
I didn’t forget about the promise I made, blogging about .NET 4.0 and VS 2010 new and spicy features, but before that, I need to blog about an interesting experience on developing a MSO word solution.

For starters, the discovery was when I found the Open XML SDK that allows me to manipulate my Word document without having office installed on the server running my application, which is a major breakthrough!
The code I used to add custom properties was taken from MSDN and I show it to you here:

 

The purpose of having this is to write some custom properties, for my Word Add-in to work properly, which is quite handy in most situations.

Hope this is as much value to you as it was to me!

Be back soon

JCrop + Thickbox iframe + Internet Explorer 8 (Nasty IE bug fix)

Hello guys.
Before posting about .NET Framework 4.0, I have to tell you about an episode (another one) about the horrifying experience that is programming IE compatible javascript.
The scenario:

I have a web form with a input file, that I need to crop & resize the image (if it is one), on a thickbox iframe.

So I create submit my form to an iframe, and when the iframe is loaded, I run the JCrop plugin.
So far so good..on Firefox..
Then I test this on IE8, nothing, the image doesn’t appear or the image appears, but no JCrop functionality whatsoever, and why? Because IE8 (an 7 as far as I know) doesn’t load the image in a timely fashion, or when it’s supposed to be loaded! Nothing works! window load event, image load event, document ready, document load, nothing!

The Trick: having the jcrop call inside a setTimeout, with 1 ms only…

It’s a shame on 2010 we still need to hack to get things working on IE.

Hope this save you a good couple of yours, like it would if I had it.

.NET 4.0 and Visual Studio 2010 candy!

Hello.
I’ve been reading about Visual Studio 2010, and .NET Framework 4.0, and I’d like to share with you some really handy stuff that’s coming towards us, and I will talk about each on subsequent posts:

About .NET 4.0:

– Routing

– Compress Session

– Optional and named method parameters

– Generate control’s client ID

– Meta Tags

– View state for individual controls

– Chart Control

About Visual Studio 2010

– Web.config transformation (!!!)

– Performance sessions

– Multi monitor

– Code navigation (new search engine)

– Call hierarchy

All these features both from .NET 4.0 and VS 2010 are very significant to a developer’s daily routine.

jTemplates – client-side xslt-like template mechanism

Hello again, just found this awesome JQuery plugin and I had to write a post about, it’s just incredible…
If on my previous post I talked about jLinq’s untapped potential, this jTemplates leave me speechless.
Imagine you can have a web application, that consumes JSON services and you can have an entire templating mechanism, right here, live, transforming your data to something user friendly, instantaneously?

Some concepts:

Constants:
– $T – data
– $P – parameters
– $Q – XHTML element’s attributes

Look here at a simple example of it’s operation:

You can use some statements like IF, FOR, FOREACH, PARAM, CYCLE include templates on other templates, send parameters, use js functions, use static content, comments, etc.

IF

FOR

FOREACH

INCLUDE

PARAM

CYCLE

the cycle iterates through it’s values each time the foreach statement (or for) iterates.

Parameters

Functions

Static Content

Comments

For a complete reference, go to the owner page

Enjoy!