Jano's Software Ramblings
Code & Pray from Monterrey

Synchronization with Postsharp

April 9, 2008 00:29 by jano

Like I said in my last post, Postsharp is a very interesting AOP tool for .net. Most of the starting up samples relate to the OnMethodBoundaryAspect. I find OnMethodBoundaryAspect really close to how decorators work in Python. In fact, tracing and logging are two of the most common examples found for both on the net. But there are a couple of more samples of decorators in Python than there are in Postsharp, so I will trying to even things a little by posting a synchronization example (which is another common example of python decorators).

The idea will be to create a SynchronizedAttribute that basically works like Java's synchronized keyword. There are many reasons why this type of synchronization isn't the brightest idea, but this is only an example dammit!! So basically what I'm trying to achieve is:

[Synchronized]
public void SynchronizedMethod()
{
   
//.. do sync'd stuff
}

So that it is equivalent to this:

public void SynchronizedMethod()
{
   
lock(this)
    {
       
//.. do sync'd stuff
   
}
}

Basically, the idea is to do a more elegant and readable version of the second code (ok, it's really just a new sample). After adding the Postsharp.Public and Postsharp.Laos references. You create a class named SynchronizedAttribute with the following code:

    [Serializable]
   
public class SynchronizedAttribute:OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            Monitor.Enter(eventArgs.Instance)

       
}

       
public override void OnExit(MethodExecutionEventArgs eventArgs)
        {
            Monitor.Exit(eventArgs.Instance)

       
}
    }

That's it!! Now you can have a beautiful synchronization attribute. The best thing about this is that you can change this code to do a more powerful synchronization (like a timed lock) without changing the rest of your code. You can also validate that the method is not static at compile time, so that you don't fall into a NullException you can check at compile time if the function is static.

public override bool CompileTimeValidate(System.Reflection.MethodBase method)
{
   
if (method.IsStatic)
    {
        SynchronizedMessageSource.instance.Write(SeverityType.Error,
"staticError", new object[] { method.Name});
        return false;
   
}
   
return true;
}

This will throw you a message in visual studio if the Attribute is added to an static method.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Postsharp

March 23, 2008 19:51 by jano

I have no idea how I missed Postsharp, it seems it has been working since year 2006. I have been working a lot looking for information about product lines in .net and I have missed this one. Postsharp isn't exactly Software Product Line technology, it is an Aspect Oriented Programming technology. However Aspects, and Product lines are closely related. In both of them the idea is adding new code in specific places of an already finished application to add new functionality. The difference is too which type of requirements their changes are related. Software Product Lines are related to functional requirements, i.e. they add new features to the existing software. On the other hand, Aspect Oriented Programming is more oriented to non-functional requirements, like performance or security. After reading about a bunch of AOP technologies such as AspectJ, ComposeJ and HyperJ, I like Postsharp implementation best. It is closer related to AspectJ (the most popular AOP language right now) but its implementation makes more sense to me. Postsharp aspects can be apply to any .net language assembly, and it doesn't bring any new construct to C#. In fact basically what it has done is create Python decorators for C#. I hope this project can keep people attention and may be even integrated closer to the .net framework.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Microsoft Internship

March 9, 2008 14:08 by jano

I am really excited I have recently got invited to work for Microsoft as an SDE-T intern. This is not the first time I have worked for Microsoft, before now I was twice and intern in Redmond as a SDE. One would suspect that this time I should be less excited that the last two times, but in fact I think I am more excited. First, this time I am going to work for an important Microsoft product that everyone knows: Live Hotmail. This group have some of the biggest challenges, just to keep their servers available for millions of users. The learning I can gain there will be a key tool in any work I might do in the future. Second, my job, obviously, is at Silicon Valley the most exciting place for a software developer to be with big software giants (including Microsoft with the Xbox, Hotmail, Microsoft Research)  sharing a wall with up-and-coming start-ups.

NOTE: I am also testing posting with Microsoft Live Writer and I sure hope the image at the bottom is Microsoft SVC (Silicon Valley Campus).

Map image

Currently rated 2.0 by 1 people

  • Currently 2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Hotmail | Microsoft
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

.Net Database Migrations

February 3, 2008 02:01 by Jano
Not so long ago I was looking for a migration utility for .net framework. Unfortunately I was unable to find one, so I built my own. It was a fast trick and it didn't brought all of the benefits of Ruby on Rails Migrations implementation. One of the biggest one, was that it basically ran Sql Scripts; but the idea was sound. Now for a new project where quick development is a most (I hope we can make it, but it will be a rough ride) I decided that it should use Subsonic that in someways it is still stronger than LinQ. One of these ways is that it supports migrations. I love LinQ, and I think is a unique and powerful tool for C#, but I sure hope that Subsonic survives as an ORM, one of the few ORM truly designed for the .net framework.

Currently rated 2.5 by 2 people

  • Currently 2.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

The Luxuries of a Monopoly

January 23, 2008 00:44 by Jano

There is several issues that are a hidrance for the economic development of Mexico, but one of the few that worries me the most are the monopolies.We have public and private monopolies, but we have monopolies in basically all our industries (telecommunications with telmex, oil with pemex, banks with banamex and bancomer, media with Televisa and TV Azteca). We all know the damage monopolies do, but there are some effects even more sutile. I found a story on slashdot about a new vector of attack called drive-by pharming. It basically configuring your router to change the IP address associated with a DNS name, you can read the detail in the article (btw I am quite sure I received that email). The first time this attack has been found on the wild was in Mexico. That doesn't surprise me the least; the big problem is that Telmex (and prodigy which is their ISP) being a monopoly have basically create a humongous homogeneous network of misconfigured routers. I am also guessing that the bank that is attacked is Banamex, which is another big monopoly.  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Forgot your key, don't worry, here have some cutting pliers

January 23, 2008 00:05 by Jano
I have a love-hate relationship with Telmex. They are a monopoly which I hate, and their services is expensive. But their service  have some redeeming qualities: they give you a real ip adress, they don't filter the content, and they don't block any port; at least they didn't. That have recently changed when they decided to outgoing calls to port 25 just after christmas. They decided to do that because they had been transform in one of the biggest spamming source in the world (to reactive your port 25 access click on this link). I think they decided to do this silently so that only people who are technically saavy would discovered it. However, it did took me a couple of hours figuring why I couldn't send emails and finding the link to correct the issue. Anyway, after this was done, my college which prides itself  of their technology decided that the best thing to do to overcome this issue is giving everyone the link to open their port 25. They should have opened a new port, which is the standard way most smtp servers are working right now instead of giving you the pliers to break your own lock.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Hosting Apps in Virtual Directories in Godaddy

September 10, 2007 03:43 by Jano

Usually at work I built new Web applications everytime I have add a new server. Unfortunately, with GoDaddy Hosting(or any as a matter of fact) you have to work with virtual directories. Right now I have been using blogengine.net as my blog application, why? because I hate how GoDaddy works with the database and I cannot be bothered to work with it anymore, so I got lucky and found that BlogEngine.net that works purely with files. Then I decided to add a wiki to my hosting so that I could work with my master's Liderazgo para el Desarrollo Sostenible team. I wanted to build a wiki so that we can easily work together, and share information about energy savings. Then I found screwTurn wiki, and I was really happy to find out that it can also work exclusively with files. So I tested it in my local machine, made it work and uploaded it to the server. Then I tried to start the server at energy.janosoft.com and the installation failed miserably. I receive an error message that seemed to be related to the app trying to upload the settings from the base application (in this case it was bloglines.net application). I initially tought that it was a bug at Godaddy setting the root but when I removed I found an error that was definately relating to the missing root. So I googled around and found the sneaky poorly documented answer to my prayers. Asp.net applications settings are automatically inherited to children virtual directories. Can we stop this? yes we can you just need to enclose the system.web tag with a location tag and set the inheritInChildApplications attribute to false. This attribute doesn't even appear in the Visual Studio 2005 intellisense. So you get something like:

<location inheritInChildApplications="false">

    <system.web>

    .

    .

    </system.web> 

</location> 

 

I have to thanks Rick Strahl's Weblog  for the solution.  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C# Tip #5: Anonymous delegates

September 6, 2007 02:04 by Jano

The first idea of anonymous delegates is to do your code a little cleaner specially for one-time functions. But there are two things for which I actually like them better.  First, it comes to style, it is part of C# constant move to a dynamic language. Even in version 1.0 C# included autoboxing (inferring how you wanted to use primitives), but in C# 2.0 it began creating delegates for you and in Asp.net, for example, turned DataBinder.Eval(Container.DataItem, "MyProperty") into Eval("MyProperty"), which I thought was a huge step foward. In C# 2.0 the compiler infer part of the signature of your method by looking in the return value. So in:

delegate(string s)

{

    return s.Length;  

The compiler automatically knows you are returning an integer. The future looks bright for us who love dynamic programming, since C# 3.0 includes var variables (which aren't variants just inferred) and anonymous classes (Tuple ugly cousin).  The second reason I love delegates is that you can use local variables, so it makes posible code that would be otherwise difficult or imposible. 

 private List<string> myList = new List<string>(new string[]{"Alex", "Jerry", "Joe", "Jimmy", "Dean"});

So you can  easily built a method that searches for all names that starts  with a certain letter.

public string[] FindAllStartsWitch(char c)

{

    return myList.FindAll(delegate(string s){return s[0]==c}).ToArray();  

C# 3.0 even includes lamda functions which will make the code even cleaner.  


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C# Tip #4: Tilde path in Asp.net

September 2, 2007 22:26 by Jano

One of the most useful, but also one of the more poorly documented features of Asp.net is the Tilde (~) the tilde can be use almost anywhere in any url appears on the server control. The tilde is automatically translated to the equivalent of HttpRuntime.AppDomainAppVirtualPath (or Request.ApplicationPath). This give you the path to access your application files from its base, this is really helpful for being able to install anywhere without troubles. It also works on most configuration files in asp.net and when using Server.MapPath. In fact most of the asp.net API that receive Url's are able to run it. 

Usage: 

<img id="myId" src="~/images/image1.jpg" runat="server"/>

turns into:

<img id="myClientId" src="/ApplicationPath/images/images.jpg"/>

 

the runat attribute is important since otherwise the path won't be processed by the server.  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C# Tips #3: The forgotten iterators

September 1, 2007 14:23 by Jano

I have no idea why, but one of the most interesting C# 2.0 features where iterator (Similar to Python generators). But its power that goes well beyond interating over objects (which is useful by itself), haven't been use that much in C#. I suspect it has something to do with the community I think that the C# community is an enterprise community just like Java and love doing things the traditional way. What is the power of iterators? Well first of, iterator use lazy evaluation, that means that you don't get the object from the iterator until you actually need it, this is specially helpful when you are using objects that use too many resources such as images, video, etc. Second you can use iterators to implement the pipeline design pattern, you can transform, modify, filter or any other operation over the iterators you want to build. Third you can suspend a program execution by using iterators as continuations.  Finally, when you have multiple threads that should work at the same time, one of the lightest implementation that exists (that is one whose cost of context changing is the lowest). In fact I used this pattern to do a simulation of a process in Python for my Performance Evaluation course on the MS it was really fast and was the cleanest of the solutions. Is there something that iterators can do? definately but I still have a lot mor tips to post.

Some useful pages about iterators, where you can find detailed information of how you can implement all of this:  

Implementing weightless threads in Python

Iterators, not just for iteration

Iterators with C# 


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5