Worst code ever seen

May 15th, 2013
Hi all, It's been a long time. Yesterday, I felt upon the worst code ever seen in just 5 lines !
public IEnumerator<T> GetEnumerator()
        {
            var pos = 0;
            while (pos < _data.Count())
            {
                yield return _data.ElementAt(pos++);
            }   
        }

jnode : jquery on nodejs

July 28th, 2012

 

a nodejs api using jquery simplicity.

jnode uses the file jquery-core which is at https://github.com/jquery/jquery/blob/master/src/core.js.

jnode aims to be used by an http server. You may also use it without but you might have useless features

What jnode brings :

  • an easy configuration of http server
  • a static file server
  • an api server (with the ability to handle automatic wrapping on result depending on content type expected by the client)
  • the $ function of jquery : you can load a module using the $ function or export an handler
  • the isFunction, isArray, ... of jquery
  • the extend, each, inArray, merge, grep and map functions of jquery

The way we build UI is changing

October 18th, 2011

For some time now, I am working on a UI framework in .NET. This framework potentially can generate a UI for everything. For now, WPF and HTML rendering are implemented. What I have discovered when working on this framework, is that our UI needs have changed.

The UI controls we want are not low level controls like buttons or TextBoxes, but high level controls : Master/Detail control, a ToolBar. I won't have more friends by saying the following but design does not matter in the end. What matters is to have a functional rendering.

An application is described in a functional way : a menu, a master/detail view, an export function on the master view. If the button is on top of the grid or in a contextual tab of a ribbon, who cares. Having a consistent behavior in an application is what really matters. It is even better if this behavior can be applied on every application on an OS. This what Microsoft tends to apply with its WinRT API. One common UI for all Metro applications.

What .NET is missing, is interface standardization. Thanks to XAML, it indeed tends to this standardization but it is not a single Library : Silverlight has its own, as WPF does, and so will WinRT have. This is where my framework being developped comes : 1 language, many targets. I tried to push the concept as far as possible by proposing to have only one DLL containing the UI. After it's the project you are making that will specify which kind of UI you want to generate : Web, Win, . Voilà. Of course, it is just an humble contribution, full of bugs, where functionalities are terribly missing. The aim is to proof the concept.

IMO, application development will take a new turn after Windows 8 is out. I think the trend will be 1 language => n platforms, 1 plaform => 1 uniform UI. The second trend already exists if you have a look to iOS or Windows Phone. In Best Practices (mainly web), we are asked to test features instead of rendering engine. Isn't it a first step to a universal language to describe UI ? Would it be possible to have all UI described under a single universal language ? What do you think about it ?

Asynchronous EUSS !

February 4th, 2011

Euss evolves ! From now on, asynchronous request are available. At least, the most basic engine can deal with them (the memory engine). It is almost the end of DataService Clignement d'oil

How to enable true intellisense on javascript files

February 9th, 2010

Hi all,

I have just read a post I thought I should share with you. You may have already heard about jquery and the fact that with the version 1.3, there was a vsdoc file that you could download so that you could have intellisense while writing javascript using jquery. Now, a patch is released for Visual Studio which, enables support for vsdoc files. Not only for jQuery, but for any javascript file.

Here is the link to the original post : http://blogs.msdn.com/webdevtools/archive/2008/11/07/hotfix-to-enable-vsdoc-js-intellisense-doc-files-is-now-available.aspx

Here is the link to the jquery web site, where you can download the vsdoc file : jQuery web-site (look for the "Documentation: Visual Studio")

Of course, vsdoc are not supposed to be deployed. They exists only to help developpers.

How to improve your intranet ASP.NET web application performance ?

January 7th, 2010

Hi,

Have you ever had a look on the number of request made by an intranet web application ? I put a premium on Intranet because I am talking about the windows authenticated web applications. Generally, for applications like these, we configure them to deny any anonymous user to connect to it. However, it means that every single file would need authentication, which is non sense : javascript files, css, pictures, . all these kind of file do not need authentication. That's why you should configure you application to allow anonymous users on them.

Why would it increase performance to disable authentication for them ? Because when using windows authentication, the browser sends a request as an anonymous user, the server replies that 401 (not allowed) and then, and only then, the browser sends a request as "you", and now you can get the file. It then means 2 request/response pairs for a single file. It make sense for pages, but not for style and scripts. IMHO, the best way is to put non sentive files in a folder. Typically, you should use the Themes functionality (even if you would have only one theme).

Now let's come to the practical point of view and allow non sensitive files to be access anonymously. For each folder with non sensitive data, you should add a web.config file like the following :

<?xml version="1.0"?>
<configuration>
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
</configuration>

It is now better but not enough. Indeed, there is also this script resources loaded by the ScriptManager. Since we cannot easily redirect the path to ScriptResource.axd and WebResource.axd, we have to change the root web.config file by adding the followings in the root configuration element :

<location path="WebResource.axd">
    <system.web>
        <authorization>
            <allow users="?" />
        </authorization>
    </system.web>
</location>

<location path="ScriptResource.axd">
    <system.web>
        <authorization>
            <allow users="?" />
        </authorization>
    </system.web>
</location>

Hope you liked the tip. See ya

How to make an HTTP proxy

October 24th, 2009

As you have seen, I have recently updated my zeroconf codeplex project. This project not only contains an implementation of the DNS protocol and of the Bonjour protocol. It also contains an implmentation of the HTTP protocol to be able to send some HTTP request on UDP, which is not possible with the implementation given with the .NET Framework. At work, I have to check that a web application I developped works correctly with IE6 (yes, it still exists. unfortunately). Since I am on Windows 7, I thought it could be great to use IE6 as virtual app. I think you know where I want to come to. On a virtual machine, you have no access to the web development server started by visual studio. What I could have been doing is deploying to the virtual machine, which would mean installing IIS, configuring it, spend time to check the web.config. Well not a straight forward solution. So I told myself : What I need is a proxy, and I already have the HTTP protocol implementation !

Here is the code of the famous proxy :

    public class Proxy
    {
        RestServer proxy;
        string hostToRedirectTo;

        static void Main(string[] args)
        {
            Proxy p = new Proxy(ushort.Parse(args[0]), args[1]);
            p.Start();
            Console.ReadLine();
            p.Stop();
        }

        private void Stop()
        {
            proxy.Stop();
        }

        private void Start()
        {
            proxy.StartTcp();
        }

        public Proxy(ushort portToListenOn, string hostToRedirectTo)
        {
            proxy = new RestServer(portToListenOn);
            this.hostToRedirectTo = hostToRedirectTo;
            proxy.HttpRequestReceived += proxy_HttpRequestReceived;
        }

        void proxy_HttpRequestReceived(object sender, HttpRequestEventArgs e)
        {
            e.Request.Host = this.hostToRedirectTo;
            e.Response = e.Request.GetResponse(true);
            e.Response.Body.Position = 0;
        }
    }

As you can see, the solution is straight forward and is only a ten lines of code !!!

Bonjour Updated

October 23rd, 2009

Hi everybody,

I have updated my zeroconf codeplex project. The main thing I have done is some refactoring. This project not only has a bonjour implementation, but also a server implementation. With such a server implementation, you can do almost anything you want. The only drop on this is that I can't deal with sessions. To understand what I am talking about, I would suggest you to download the new bonjour release and look at the Network.dll assembly. I promise I will give some more documentation about it. You already can have a look the documentation on the codeplex site.

Have fun !

Reusable singleton in ASP.NET Context

August 5th, 2009

Hi everyone,

After reading Ayende Rahien's post, I told my self that I had to share my Http Singleton module I am using for some time now. This module enables me with a few lines of code to have a single instance of what ever I could want. If like Ayende, you are using NHibernate, you just would need to inherit from the code I'll give you to make your own HttpSessionModule, and thus have one session created by httpcontext. I use it to have not only an ObjectContext (I am using euss), but a service which hides this persistence layer.

Here is my abstract Http Singleton module implementation :

    public abstract class HttpSingletonModule<T> : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
        }

        public void Init(HttpApplication application)
        {
            application.PostAuthorizeRequest += 
                new EventHandler(application_AuthorizeRequest);
            application.EndRequest += 
                new EventHandler(application_EndRequest);
        }

        void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            if (context != null)
            {
                T instance =
                    (T)context.Items[typeof(T).AssemblyQualifiedName];
                if (instance is IDisposable)
                    ((IDisposable)instance).Dispose();
            }
        }

        public static T Instance
        {
            get 
            {
                return (T)HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
            }
            set
            {
                HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = value;
            }
        }

        void application_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            if (context != null)
                context.Items.Add(typeof(T).AssemblyQualifiedName,
                    CreateInstance(context));
        }

        protected abstract T CreateInstance(HttpContext context);

        #endregion
    }

As you may have understood now, To implement your module, it is quite easy. Here is the implementation I am using in a projet :

    public class PortalServiceModule : HttpSingletonModule<PortalService>
    {
        protected override PortalService CreateInstance(HttpContext context)
        {
            if (context.User.Identity.IsAuthenticated)
                return new PortalService(context.User.Identity.Name);
            return new PortalService();
        }
    }

You see ? so few lines of code for such a simplicity of use : to get my portal service instance, I just have to call the static Instance property of my PortalServiceModule. How could it be simpler ?

Parameterized Modal Popup

Août 3rd, 2009

Hello everyone,

Maybe some among of you ever had this issue : open a modal popup (here with AjaxControlToolkit), and being able to give it a parameter, the id of the control which opened the popup for example. It is entirely doable, it just need some code. The first thing to know it that with AjaxControlToolkit framework, there is a BehaviorID on any Extender. This property enables you to then manipulate this extender on the client side (in javascript). Let's have a look to the code before any explanation.

<asp:ModalPopupExtender runat="server" BehaviorID="myPopupBehavior"
PopupControlId="myPopup" TargetControlId="dummyLB" />
<asp:Panel ID="myPopup" runat="server">
<asp:HiddenField runat="server" ID="myParameter" />
</asp:Panel>
<asp:LinkButton ID="dummyLB" runat="server" style="display:none" />

<style type="text/javascript">
function openPopup(parameter)
{
    $find("myPopupBehavior").show();
    $get("<%= myParameter.ClientID %>").value=parameter;
}
</style>

After having seen the code, I guess you need some explanations. First of all, the first thing to know is that an AjaxControlToolkit extender always need a TargetControlID. I made an invisible LinkButton for that purpose. The second this to notice, is that in the panel which will be displayed, I have an hidden field. This is this field which would hold the value of my parameter. To assign my parameter, I made this javascript function which hides this assignment behavior. Thus, I have a reusable function to open that popup. Here is a sample use case :

<a onclick="openPopup('http://www.google.fr')>lien vers google</a>

With this code you also can have the value of your parameter on the server side because the hidden field is a server control !

Voilà, hope you enjoyed it.

jQuery Control Toolkit

June 6th, 2009

Hello everyone,

It's been a long time since I posted something on my blog. I was a little busy with my work. But this work enables now me to introduce you a new codeplex project : jQuery Control Toolkit. The aim of the project is to be able to use jQuery trough extenders in ASP.NET, but more globally, it adds new functionalities as jQuery plugins. The project is not so detailed yet, but if you can't wait for a description of each functionality, just have a look at the source code, there is a web application project which uses each (main) functionality. There are also more functionalities in the javascript source file jQuery.ACT.js, but, it is not documented at all (yet).

Have fun,

Powershell for Visual Studio

March 23rd, 2009

Hello,

Here is my new project : Powershell for Visual Studio. There is already syntax coloration, and basic intellisense (available cmdlets, with their help). Currently, it seems that the VS2008 SDK is needed to make it work. This will be solved in the next release...

Have fun !
Powershell for Visual Studio

XNA Designer 0.1

February 24th, 2009

Hello everyone,

I've updated my XNA codeplex project. Now, the designer works, is (more or less) beautiful. Moreover, you may also download a component library containing game components : cameras, model viewer, cube, stairs, wall, ...

You can have a look at this designer on the wiki home page of the project.

You want to try it by yourself, go to the release.

Bonjour MEF

February 7th, 2009

Hello everyone,

Do you know MEF (Managed Extensibility Framework) ? It is a new library in .NET that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed. I let you go to codeplex to get more information.

MEF is based on catalogs. There are a DirectoryCatalog, an AssemblyCatalog, ... Well, I am proud to introduce you to a BonjourCatalog.
For this, still the same address : http://www.codeplex.com/zeroconf.

"What is the aim, you may ask ?" It simply enables to detect bonjour services using MEF.

I have not published any release regarding this BonjourCatalog yet, but it wont last.

Concrete Singleton pattern implementation in .NET

January 14th, 2009

Hello everybody,

A pattern we regularly find is the singleton pattern. What I suggest you is a .NET implementation. This one deals with a singleton context dependent (singleton by ASP.NET context, by WCF context, by thread, for the whole application)

public class Singleton<T>
{
private Singleton()
{
if (CreatingInstance != null)
CreatingInstance(this, EventArgs.Empty);
}

private static T instance;

[ThreadStatic]
private static T threadInstance;

private static object instanceLock = new object();

public static T Instance
{
get
{
//ASP.NET case
T instance;
if (HttpContext.Current != null)
{
if (UseContextInstance)
instance = (T)HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
else
instance = (T)HttpContext.Current.Application[typeof(T).AssemblyQualifiedName];
if (instance == null)
{
instance = new Singleton<T>().SingletonInstance;
if (UseContextInstance)
HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = instance;
else
HttpContext.Current.Application[typeof(T).AssemblyQualifiedName] = instance;
}
return instance;
}
if (OperationContext.Current != null)
{
throw new NotImplementedException();
if (instance == null)
{
HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = new Singleton<T>().SingletonInstance;
}
return instance;
}
//In case of no HttpContext nor OperationContext
if (UseContextInstance)
{
if (Singleton<T>.threadInstance == null)
Singleton<T>.threadInstance = new Singleton<T>().SingletonInstance;
}
else
{
if (Singleton<T>.instance == null)
{
lock (instanceLock)
{
if (Singleton<T>.instance == null)
Singleton<T>.instance = new Singleton<T>().SingletonInstance;
if (Singleton<T>.instance == null)
TryActivate(out Singleton<T>.instance);

}
}
}
return Singleton<T>.instance;
}
}

private static void TryActivate(out T p)
{
try
{
p = Activator.CreateInstance<T>();
}
catch (MissingMethodException) { p = default(T); }
}

public T SingletonInstance { get; set; }

public static event EventHandler CreatingInstance;

/// <summary>
/// Set this property to <see cref="true" /> to have your singleton dependant on your context :
/// - one per thread in Windows Forms/WPF
/// - one per Context in ASP.NET
/// </summary>
public static bool UseContextInstance { get; set; }
}