Nov 3 2011

OnDeleteCascade Attribute for Entity Framework 4.2

Category: — Duke @ 19:22

So boring that EF is still under construction but I love the multiple ways it allows me to do the thing.

I think that for small size projects, attribute decoration is ideal, and much more convenient than code mapping.

 

anyway I’ve found myself in a situation where I’ve a class “product” that has multiple images, and I want that when someone deletes a product, all the associated images goes away too.

For sure I can do this by code, but my project is quite small and I would like to have an attribute that does the trick

So as this is a quite challenging thing I’ve started writing my custom attribute More...

Tags:

Oct 30 2011

Asp.net MVC3: entity framework 4.1 UniqueValue attribute validator

Category: ASP.NET | C# | Linq | MVC3 — Duke @ 15:28

I’m playing with MVC3 and EF4.1 (magic unicorn version) for a nice project I’m working on, and I’ve found myself in the situation to validate some fields against value duplication

(think about username, or other things that must be unique).

Unfortunately we don't have [Unique] constraint attribute in EF4.1 yet, so I’ve thought to leverage on the validation infrastructure of MVC3 to ensure uniqueness of the values.

I like the simplicity and expressiveness of the attributes then I’ve implemented my UniqueValueAttribute class.

this is remarkable because now I have a simple way to decorate every model attribute I want to be unique

(lot of fun code follows) More...

Tags:

Sep 26 2011

Visual Studio 2010 my tests doesn’t pass if I run them, but pass if I debug!

Category: — Duke @ 18:45

today I had a bizarre experience using nhibernate.

I’m writing an audit trail system on the top of nhibernate, that have to satisfy certain regulatory requisites.

Of course I’ve a lot of test that proves that my work is proceeding in the right direction. But this morning I’ve added few line of code to introduce a new feature that at the end requires a “references” clause in the nhibernate mapping.

Suddenly some of the test went broken.

I’ve tought that I’ve wrote something wrong in my code so I’ve run the test in debug mode… and guess what? all the test pass…

I’ve then tought that there was some particular situation (maybe some file locked or god-knows-what-like situation that have caused the transient… or what I’ve supposed to be a transient) and then everything was returned normal.

so I’ve wrote other few lines of code and run the test again and BANG! red again…

this time I’ve started investigating more, I’ve lost the whole morning

the symptoms were the followings.

1) if I RUN the test form the test list windows of visual studio, some of the test (and always the same tests) systematically fails.

2) if I DEBUG the failing tests from within the test result window or the test list window the exact same test pass

3) from the moment when the test PASS in DEBUG, I could RUN the test again from the test result windows, and they KEEP passing

4) if for any reason I RUN again the same exact test from the test list window they start failing again.

 

the error message when they fail was something like “the property method get_Xyz could not be found”

and in fact that was the property I was working on.

 

at the end I’ve discovered the cause.

the Code coverage instrumentation was activated on the assembly I was editing.

for a reason that nobody will ever know, visual studio have cached somewhere (probably over the rainbow) something about how to instrument the assembly I was working on.

no mater how I’ve tried to delete the BIN and OBJ and TestResults of all the involved projects… they keep presenting the same behavior.

Guess what? this visual studio magical cache went for another unknown reason out of date.

and the result (I’ve figured out) was that out of date code was keep injected in my code causing all the sort of the bad things… but only if I RUN the test.

Luckily the DEBUG of the test doesn’t inject (outdated) the instrumentation code.

 

Once understood that the code coverege was the cause it was easy to solve.

just shut the code coverege down, recompiled everything, runned the code => green, debugged the code=> green!

after this I’ve turned on the code coverage again and tested the tests another time: green again! YAY!

 

So, if your test starts to become reds without any reason, give a look at the code coverage Sorriso

Tags:

Sep 22 2011

Visual Studio 2010 Expression Tree Debugger Visualizer: how to make it work.

Category: C# | Visual Studio — Duke @ 19:21

I’m developing a quite complicated (it is always complicated when you have to manage big trees) project.

As in the project there are a lot of trees that are moving around (never seen a tree walking down the streets?)  and transforming themselves I’ve thought that it could be nice to use a Debug Visualizer to have a clearer view of the structure of these expression trees.

I know, the visual studio 2010 have the new DebugView property that makes it inspect the tree.. but it is still complicated to read when you have kilometric long namespace names that ends in generic object that in turn uses generic parameters with (again) long names.

it is a mess Triste

so I’ve decided to use the old but still good Expression tree visualizer available from the Visual studio 2008 C# code samples.

Downloaded, changed the reference to the Microsoft.VisualStudio.DebuggerVisualizers.dll library so to reference the newest one (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.VisualStudio.DebuggerVisualizers.dll) and then compiled.

But here starts the problem:

My configuration is Windows 7 64 bit Ultimate and Visual studio 2010 Ultimate (with latest service packs and everything)

My projects are all in .Net 4.0 at the moment I'm writing.

I’ve compiled the visualizer, put it in the right folder

C:\Users\Duke\Documents\Visual Studio 2010\Visualizers

and BAM! …. nothing Triste

nothing appear, no magnifier  when I hover an expression reference.

I’ve tried to moving the visualizer in all the possible valid location (also considering the 64 bit architecture) but noting!

Binged and Googled… I’ve found that many people have had the same issue.

So I’ve started looking inside the actual code, and here it is!

 

[assembly: DebuggerVisualizer(typeof(ExpressionTreeVisualizer), typeof(ExpressionTreeVisualizerObjectSource), Target = typeof(Expression), Description = "Expression Tree Visualizer")]

Sooooo wrong! I’ve fixed it this way

 

[assembly: DebuggerVisualizer(typeof(ExpressionTreeVisualizer), typeof(ExpressionTreeVisualizerObjectSource), Target = typeof(Expression<>), Description = "Expression Tree Visualizer")]

Note typeof(Expression) transformed in typeof(Expression<>)

The type to visualize should be the generic one to work with the last version of the framework.

Recompiled, dropped in the right folder and this time IT WORKS!

Ok! now it works like a charm I hope this could help also other people with the same issue Sorriso

Tags:

Aug 19 2011

SyncWcf published!

Category: — Duke @ 18:49

I’ve published SyncWcf on codeplex: http://syncwcf.codeplex.com/

there is a lot of reason that have moved me to do this work during my holidays.

First of all I need it. it is too complicated to maintain the code that silverlight require to call a webservice. the fact that it needs an asynch version of the service interface is inaceptable to me.

the fact that there are two unrelated interfaces that logically represents the same thing, it is substantially wrong to me

Second: I think that my team need it. I’m working with people on a business-line project. these people are very good developers but they have no experience with silverlight. it is the first project they works on that uses such kind of technology.

I think they are still try to handle the correct way to implement model-view-viewmodel, and I don’t think they need to understeand the extra complexity of the async call for wcf.

Third: “Add service reference” generates code difficult to understand, hard to handle and very prone to generate errors. I really think that there is a better way if the server side code and the client side code are in the same hands, I want the two things are strongly related, I want to use refactoring tools at server side  and the modifications are propagated on the client side as well, I don’t want to remember to myself to do “update service reference”. Anyone has got good reasons to dislike the automatically genererated client proxies…

to me they are good in case the service I have to interface to is from a 3rd party or it comes from a technology that is not .NET or in any case it is not something “internal” to my project (some external system I have to interface to)

 

All these reasons moved me to write SyncWcf.

One of the nicer part to me is the fact that SyncWcf uses only Silverlight assemblies: no need to include any other 3rd party library (no need of castle or any other proxy generator)

Another think I like most is the way you call an operation by using lambdas… and the fact that out and ref parameters really works in the way it is intended!

Last, thewre is the full support to intellisense. To me intellisense is one of the greatest inventions, I have payed a lot of attention in supporting it the best way possible.

I’m using SyncWcf in a real project, so I have the chance to see it in action. anyway I think there is still lot of things to do to improve it.

For example I suppose there should be place to enhancement in the area of the duplex communications, but unfortunately my project are not using these at the momet… so feedbacks will be greatly appreciated (not only on the duplex part… on everything you think could improve the library)!

So, the library is in a stable state now, there is documentation, how tos, manuals source code and binaries ready to be downloaded. just run on codeplex and press “download”

The bits are waiting for you! and I’m waiting your feedbacks!!

Tags:

Jul 8 2011

Windows Phone Marketplace error c101c404

Category: Windows Phone — Duke @ 11:34

It is a couple of month I’ve started receiving the error c101c401 when I try to buy something using my windows phone.

I’ve tried to fix for what was possible to me, but I’ve not found the solution.

Today I had a day off from work and I’ve thought to call the support: first thing about the windows phone support I’ve to say: they are amazing!

I’ve never ever found a support service so gentle and competent, I rate them 10 out of 10!

The girl that have answered to me called me back at least 5 times today proposing new ways to solve the solution, searching in their KB and providing useful information to solve the issue.

At the end we have solved the problem and it was (as often happens) a stupid thing that have caused the issue: or better there was two issue

More...

Tags:

Mar 14 2011

Stairs the game

Category: — Duke @ 15:27

It’s a couple of week I’ve started to work on a project, the codename is “Stairs” (twitter hashtag #stairsgame) and it is an Indie game for Windows Phone 7 (and therefore XNA).

At the moment it will be just for WP7, no plans to port in on XBOX or PC.

The game is a sort of puzzle, but rather than point on the innovation in the gameplay It will focus on the visuals.

I want to make it appear as an anime. I love Hayao Miyazaki and I want to make something that may remember the feelings of his movies.

Of course I’m not that good in drawing, unfortunately, but I want to make the character very “live” and fort his reason the game will be animated using traditional MANUAL animation.

I’ll draw all the frames on paper, but I don’t want another game where the object appear made by paper… I don’t like that style too much.

I want to make the real thing, real traditional animation: with animation disk, peg bars, punched paper and alcohol-based markers. Pencils and rubbers will be a must, the background will be painted and all the art will be produced outside the computer.

Of course the pc will play a role in the digitalization and maybe in the fixing of some defects, but I hope it will be the minor part.

I’ve always loved to draw, and sadly in the last year I don’t had the chance to practice too much so my skill was lowered a lot… from when I was at my top, but I’m studying and practicing every evening and watching a lot for anime and reading manga, also I’ve studied some books and other I will study soon.

I hope to make something visually pleasant, and that feels different from the other heartless games.

if you are interested in a daily update on the project follow me on twitter A bocca aperta.

Ok the today plan is to setup the Visual studio solution, not so hard, huh?

I hope to post some screen shoots of the game soon but not today obviously, I hope you like the idea behind the game!

More about the gameplay in the next post!

Tags:

Mar 14 2011

How to wake-on-lan by Windows Phone

Category: — Duke @ 15:26

Last friday I was at work and a collegue of mine asked me some info, that I know to have on my own home pc.

Unfortunately there was 2 issue: my home pc was shutted down and nobody was at home. So I had no way to remotely log in my machine as it was unpowered.

Therefore today I’ve decided to configure my home network and pc to be remotely turned on, and of course to do this I have to use the Wake-on-Lan protocol.

This is my configuration

ADSL modem-router: LynkSys WAG120N V1.00.12

Motherboard: Asus p7p55d-e premium

Operative system: WIndows 7 Ultimate

Mobile: LG-E900 (Windows Phone 7)

The goal was to turn on the pc by using my Windows Phone. But to achieve this all the 4 pieces above have to cooperate.

More...

Tags:

Jan 6 2011

Running VirtualBox as 64 Bit windows service

Category: C# | Virtual Machines — Duke @ 13:19
It is some time that I’m interested in virtualization: I think it is a nice way to keep my machine healty and clean, and in the same time it allows me to experiment and do “distruptive” things.
I love the possibility to take snapshots and recover a previous state in a moment!
Probably some of you have noticed that “photoatomiclab.net” is not always online(there is a reason for this you’ll discover in a moment), right? I know it is boring and frustrating when a resource is not available when you need it, and for this I’m really sorry.
So… I’ve started thinking on how to improve the thing. First of all: my favorite virtualization environment is VirtualBox (too bad Oracle have bought it, I hope it will not ruine all this good work) I like it because is free, is fast (in my opinion far better than VMWare) and it is supported by an opensource community… and having the source code in your hands you can do great things.
Anyway… my site is not always available: that’s because it is hosted on a virtual machine on my real machine! ANd this because I always like to know the internals and from time to time I had some crazy ideas: like streaming the town fest in the square in front of my windows using my HD camera ( LOL, usually I did this on xmas night) or connect some netduino robots and drive it from a web interface… this requires me to have a host where I really can do everything. and these host services are not cheap, unfortunately Triste 
But hey! VirtualBox really saves the day! So I  have this virtualmachine “Bear” that runs my site… the one you are reading now. And it is hosted on my real machine… and my machine is housed in my bedroom… and at night I like to sleep A bocca aperta 
It is natural that I shut id down at night… and therefore the site becomes unavailable….
Again, my pc is setup to start up at about 14.00 CET every day (that’s because it have to record my favorite tv series…. but this is another story) and since I’m forgetful, I usually don’t remember to turn the VM on, so I’ve tough to improve the situation… I’ve started searching for a tool that allows me to automatically starts my VM up when the pc starts.
I’ve found VirtualBox headless mode… nice! but… I have not found a way to use it… expecially because for the shutd down part… I want my VM freezes its state when the pc is shutted down.. I don’t want to loose the last change on the site, nor to wait minutes for the virtualized machine shut down…
I’ve serched for days but nothing appears to be a strong solution…. there are some services that tries to emulate this but at the end they open a shell and invokes the command line version ov VBOX and “do things”
this have the bad behavior (in the major part of the occasions) to open console windows for some seconds, or to leave consoles opened…. and if you close the console .. the VM dies Triste
this is ugly and completely unprofessional.
But fortunately VirtualBox is opensource! and there are SDKs!!

Tags: ,

Oct 13 2010

3 ways for splitting a collection into pages with Linq

Category: C# — Duke @ 18:27
There are situations when you have a collection of objects (IEnumerable<T>) and you need to split it in chunks or pages so to process these resulting pars in different moments.
There are a lot of way to achieve this but the nicer is probably using Linq (it makes also your code looks a lot more modern and cool!)
the focus of this post is to discuss some of these ways and it’s pros and cons.
Digging around the web I've found this first example ( unfortunately I've lost the link, anyway the code in this first sample is not mine)
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> enumerable, int chunkSize) 
{ 
    var total = enumerable.Count(); 
    for (int i = 0; i * chunkSize < total; i++) 
    { 
        yield return enumerable.Skip(i * chunkSize).Take(chunkSize); 
    } 
}
 
This allows you to write code like:
foreach(var page in myCollection.Chunk(30))
{
    foreach(var item in page)
    {
         process(item);
    }
}
anyway, this work well in limited scenarios, when the amount of record are quite low. To prove it imagine to have a collection with 1 million item in it, and you want to split it in chunk of 1000 elements each.
the Chunk method will first skip zero element (hey pretty fast!) and then take 1000. the total amount of time is c, given c the size of the chunk.
but the next round the method have to skip the first 1000(as they are already taken) and then take 1000. Hum… still not so bad as the first 1000 was already taken and then we have to process the next 1000.
third round…. skip 2000 this time… and then take 1000; ok but we have skipped 0 the first time, 1000 the second time and 2000 the third time… if we continue this way at the 10th page we will have to skip 0, 1000, 2000, 3000, 4000 … 9000! oh no, this means that at the kth page we will need to skip
((k * (k-1))/2 ) * c;
Old friend Gauss appears again! plus of course (k * c) for the take part.
that’s so bad, as you can see, we have a term that grows as O(k^2) and you for sure don’t want to have such complexity if your collection have 1 million of elements… moreover, one usually want to split thinks in chunks when the number is important, so exactly our scenario… therefore this algorithm is quite poor in a general situation: we are not going to use it in our programs, isn’t it?
So, let’s move forward and try to find something better: we have to solve the problem of restarting every time from the beginning, let’s try some caching, it usually works,right?
 
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> enumerable, int chunkSize) 
{ 
    var total = enumerable.Count(); 
    var current = enumerable; 

    for (int i = 0; i * chunkSize < total; i++) 
    { 
        yield return current.Take(chunkSize); 
        current = current.Skip(chunkSize); 
    } 
}

Tags: