vineri, 18 septembrie 2009

WordPress doesn't bother to have a nice design on Google Chrome also

So word press doesn't like Google Chrome very much. Look at how they're Apple theme behaves.










The same problem with the Search button. Guess there aren't enough Chrome users on line for wordpress to consider it being worth the attention.

vineri, 4 septembrie 2009

Did you think only Google Chrome has funny messages?

Today an interesting thing happen with firefox, it displayed the following message




Looks like Google Chrome has a competitor for those Aw, Snap! messages.

sâmbătă, 15 august 2009

IoC in .NET part 1: Autofac

This is an interesting IoC project that I used in a couple of occasions, nothing big, but I sure enjoyed it immensely.

Autofac is wrist friendly so to speak and it tries to solve your configuration problems in a simple and concise manner.

Without further ado go at http://code.google.com/p/autofac/ to get the latest version.

Next I’ll try to create a simple example that I hope we’ll use for the most part of this series.

In learning the inner workings of IoC containers we’ll develop a console application that provides some simple autocomplete for it’s features.

The features of our application are:

  • Autocomplete for internal commands
  • Commands
    • Alert – displays a message box
    • NewLine – goes to the next line
    • Calculator – with support for add

All of this features will be organized under the Components folder as functions and services.

In general you will want the concrete classes to be linked to an interface, and is by default the way I like to handle it.

A lot of voices in the community are against the use of xml files for configuration, and to that regard Autofac offers the option of using a fluent interface for configuring the builder. Although good for contracts that you are not likely to change without recompiling the application, when you need to have functionality similar to that of a plug-in you will prefer to use an IoC capable of reading xml configuration. Obviously Autofac has support for xml files as well.

Note xml configuration support is very limited in Autofac, so I’ll only show the bootstraping for this file

As I said earlier an IoC is just a form of factory, the only real difference is that this is a factory with nice configuration capabilities.

In more complex applications I recommend you use modules for configuring but for this example is sufficient to have everything configured in only one place.

As a preference I always configure the IoC I’m using in a class called Bootstrap in my highest level module ( commonly identified as the assembly containing the Main function, or if in a web app the assembly that contains the Global.asax).

The code for the Bootstrap.cs looks like this:

public class Bootstrap
{
public static IContainer Components(){
var builder = new ContainerBuilder();

builder.Register<ConsoleClearScreen>().As<IClearScreen>().SingletonScoped();
builder.Register<ConsoleWriteString>().As<IWriteString>().SingletonScoped();

builder.Register<AlarmFunctionState>().As<IFunctionState>().Named( "AlarmFunctionState" );
builder.Register<NewLineFunctionState>().As<IFunctionState>().Named( "NewLineTransition" );
builder.Register<CalculatorFunctionState>().As<IFunctionState>().Named( "CalculatorTransition" );


builder.Register<IConsoleInputService>(
c => new ConsoleInputServiceImpl(
new IFunctionState[]{
c.Resolve<IFunctionState>("AlarmFunctionState"),
c.Resolve<IFunctionState>("NewLineTransition"),
c.Resolve<IFunctionState>("CalculatorTransition")
} ) );

return builder.Build();
}
}


Autofac uses the ContainerBuilder class to register services and it offers a simple method of directly creating those services through the use of lambda expressions.



For the actual implementation of the app you can download the source code here.

joi, 13 august 2009

Checkout the Windows 7 API Code Pack for .NET

The individual features supported in this version (v1.0) of the library are:

  • Windows 7 Taskbar Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars.
  • Windows 7 Libraries, Known Folders, non-file system containers.
  • Windows Shell Search API support, a hierarchy of Shell Namespace entities, and Drag and Drop functionality for Shell Objects.
  • Explorer Browser Control.
  • Shell property system.
  • Windows Vista and Windows 7 Common File Dialogs, including custom controls.
  • Windows Vista and Windows 7 Task Dialogs.
  • Direct3D 11.0, Direct3D 10.1/10.0, DXGI 1.0/1.1, Direct2D 1.0, DirectWrite, Windows Imaging Component (WIC) APIs. (DirectWrite and WIC have partial support)
  • Sensor Platform APIs
  • Extended Linguistic Services APIs
  • Power Management APIs
  • Application Restart and Recovery APIs
  • Network List Manager APIs
  • Command Link control and System defined Shell icons.

 

More details at : http://code.msdn.microsoft.com/WindowsAPICodePack and http://www.infoq.com/news/2009/08/WIndows-7-DotNET

Javascript gotcha


Usually the style you organize code is just that A MATTER OF STYLE, in javascript however it’s not.




If by any chance you end up writing the following code:



 



var display = function() {
this.x = 10;
return
{
alert: function(message) {
window.alert(message);
}
}
}


you might be in for a surprise but it won’t return anything because by default the javascript interpreter inserts ; after every line that doesn’t already contain it or }.



Instead you should write:



var display = function() {
this.x = 10;
return {
alert: function(message) {
window.alert(message);
}
}
}


More interesting notes you can find in the next video:






Hope you enjoyed this I sure did :D.


What is an IoC ?

Disclaimer: This is a series of posts in which we’ll go through the IoC’s that are used in today’s applications

The most simple answer I can give you is this:

It’s an abstraction over Factory Method.

It helps, in my opinion to think of this as a factory method, because then you know what to expect of it, since it constructs/resolves a specific implementation based on the configuration you provide it.

In a factory method you write, aka “hard code”, the implementation you will be using. In an IoC container you basically do the same except it’s easier to change, if you write it in the xml file since using it in code leaves you with what you started and never got to changing it.

However, an IoC has an additional plus over the hand coded factory, that is “It is able to construct objects  based on the configuration you supplied”.

Does it help ?

Oh yes it helps. There’s a principle in software that states “High level modules should not depend on low level modules, both should depend on abstractions”. And guess what the principle I just stated is the Dependecy Injection Principle, it is an important point to note since IoC containers are often referred  to as Dependency Injectors. So you will probably find the IoC/DI whenever reading about an IoC.

With the help of an IoC you can declare dependencies of you object using abstractions, usually in the form of interfaces and/or base classes.

In the next part will explore a couple of IoC’s that the .NET has to offer as well as from the outside of the .NET ecosistem.

I hope that by the end of this series we can form a summary of benefits and deduct some ground rules for choosing a certain IoC, or deciding if we need it.

joi, 25 iunie 2009

Starting a simple ASP.NET Web App

Summary:

In this post we will discuss the tooling that is at our disposal and also some of the benefits or not so beneficial features it brings to the table.

 

Content

 

  • Introduction
  • Tooling
  • Benefits of using it

Introduction

ASP.NET MVC is Microsoft's way of getting on the MVC wagon. It offers a lot more control over the application you're building, no more WebControls ( personally I never and hope to never have to use it ), pure XHTML as W3C would have you doing anyways.

At this moment, and I suspect for years to come, the market of web-apps is under the heavy influence of dynamic languages like Ruby, PHP, Groovy, Python etc. Although scripting languages, a couple of strong frameworks have arisen, from them the most important of all is Rails, a MVC framework that set the bar for all the frameworks to come, so to speak. The other contestants that I find worth mentioning are Joomla (PHP), Grails ( Groovy ), Pylons and Django (Python).

We, the .NET guys, needed something like that for a while, of course there was MonoRails for years, but it didn't get enough publicity. Now there is the Microsoft's solution ASP.NET MVC, and also another project from the OSS ( Open Source Space ) called FubuMVC, wich is an opinionated front-controller style framework. I'll talk about FubuMVC in another post though.

 

Tooling

First there is Visual Studio, and then there is this add-on you have to install from http://www.asp.net/MVC/download/.

Next you have to go to File->New Project. And select from the Web category the Asp.NET MVC Web Application projec type.

 

Create-New-Project

Next you'll be displayed the following screen that let's you decide if you'll be using tests or not

Add-tests-project

For this part of the post it is not needed. Press ok and a sample project is created for you.

Let's have a look at the solution:

Screen solutionYou can see that the content is organized in

 

  • Content
  • Controllers
  • Models
  • Scripts
  • Views
There are three files left out, Default.aspx, Global.asax and Web.config. They are files that the ASP.NET engine expects to find in a web-project, kind'a like Java web-servers expect a certain layout of your project. It is a convention so we'll leave it at that.

In the Content directory you'll put your css files, images, documents and whatever else is considered to be content. The Scripts directory contains javascript files. This is the way they organize by default, you can roll your own, for instance you could have an Assets directory that has everything organized a certain way. There are no conventions so you're free to invent them.

The import part, the C in the MVC pattern, are the Controllers.They direct the flow and supply the models to the views, and that's enough of a responsability.

The nice part, the V in the MVC pattern, are the Views. They display data to the user and accept input through HTML Forms.

The core part, the M in the MVC pattern, are the Models. This layout is mostly inspired by the Domain Driven Design book, Eric Evans wrote a long time ago, but it changed the way many people, myself included, think about design.
I think that the Model part of the application is the most missunderstood part of the hole MVC pattern. The model is, in my vision, composed of the distinct parts: The Domain and The Services. The Domain is composed of entites and value objects, more info on the subject you can find at InfoQ Domain Driven Design Quickly. The Services are the business part of the model.
Don't worry I'll talk a bit more on the subject on the next part of this post.
Tools, tools and tools. If you left click a controller you'll see there's a button ready to generate the controller for you.
add-controller
Next let's create a simple EchoController.
add-controller-screen
Don select the add actions for.. since we'll not use it, just press Add. As you'll se the controller already has a Index method provided. That's a convention set in the Global.asax.cs file, by default the Index method is assumed when accesing a controller. Just to shed some light on the matter let's Debug (F5) the project. Press Ok when it asks you if you're to enable debugging on the Web.config file.

By default the Home/Index action is executed when a request is sent to the http://localhost:{development-port}/. That's because it was registered as the Default controller and action in the Global.asax.cs file. If you want to access the EchoController you'll have to write http://localhost:{development-port}/Echo. That will match the request to the EchoController and the Index action. If you do this you'll se a nice Yellow Screen Of Death.
Why? because we haven't added a view. The convention is that you have define in the Views folder, a Echo folder and add a Index.aspx page. Let's see to go about doing this. There are basically to options, but first stop the debugging ( since this is not a scripting language ).
Option No.1 By adding the folder Echo to the Views directory and Add-> View from the context menu, or Option No.2, right click inside the Index method in the EchoController class. And select Add View from the context menu. That is going to create everything for us,
and it let's us choose what type of view we want ( Details, Create, Edit, List, Empty), that's if we supply a model, but for the sake of simplicity we won't at this time


If you right click on the view, you'll have the option to go to the controller, similar, you can navigate from to the action to the view.
The rest of the tooling support is the standard editing and intellisense you've probably used with .NET previously. The Editor for the aspx pages could use alot of improvements, but that's for VS2010 I suppose.

Benefits of using it

For one you get a much richer experience since you have :

 

  • Friendly URLS
  • Alot more control over the generated XHTML and Javascript ( since you write it )
  • Easier testing of you workflow and services
  • Better separation of concerns
  • A powerfull architecture that allows you to even ditch the hole View-Engine and implement your own, or you could try Spark for a run.
In the next post I'll discuss how to set up a startup project on a simple theme that I've done with Spring MVC a couple of months back.

miercuri, 3 iunie 2009

Finally Windows Mobile 6.5 is out

Too bad it didn't arive sooner, I had to make a small app using some mobile technology this year at the faculty and I had to settle on Windows 6 SDK, wich was well what you would have expected from something written in 2007.




It looks pretty nice, but that's to be expected since it will try to expand on the market that smartphones like iPhone is gaining more and more terrain. Microsoft is trying to keep it's products in line with the design that operating systems like Vista or Windows 7.



It's my feeling that this only the beggining of the other releases they have scheduled for this year (or the next), releases like: Windows 7, Visual Studio 2010, Office 2010, .NET 4.0. They might be comming with other things also, but I those I named are the ones I'm interested in, office not so much, but who knows what my work will lead me to.

marți, 14 aprilie 2009

Another TCP/IP Server client



Well, it seems I'm supposed to write another Socket based server client application. Since usually all you find on the web are Chat Clones, I decided
to use the time and get a really basic framework going.


First off, we have a basic client



namespace BaseNetworkProtocol
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.ComponentModel;
using System.IO;
/// <summary>
/// The class that contains some methods and properties to manage the remote clients.
/// </summary>
public class Client
{
public ProtocolContext Context;

/// <summary>
/// Gets the IP address of connected remote client.This is 'IPAddress.None' if the client is not connected.
/// </summary>
public IPAddress IP
{
get
{
if ( this.socket != null)
return ( (IPEndPoint)this.socket.RemoteEndPoint ).Address;
else
return IPAddress.None;
}
}
/// <summary>
/// Gets the port number of connected remote client.This is -1 if the client is not connected.
/// </summary>
public int Port
{
get
{
if ( this.socket != null)
return ( (IPEndPoint)this.socket.RemoteEndPoint ).Port;
else
return -1;
}
}
/// <summary>
/// [Gets] The value that specifies the remote client is connected to this server or not.
/// </summary>
public bool Connected
{
get
{
if ( this.socket != null )
return this.socket.Connected;
else
return false;
}
}

private Socket socket;

NetworkStream networkStream;
private BackgroundWorker bwReceiver;

#region Constructor
/// <summary>
/// Creates an instance of ClientManager class to comunicate with remote clients.
/// </summary>
/// <param name="clientSocket">The socket of ClientManager.</param>
public Client(Socket clientSocket, ProtocolContext context)
{
this.Context = context;

this.socket = clientSocket;
this.networkStream = new NetworkStream(this.socket);
this.bwReceiver = new BackgroundWorker();
this.bwReceiver.DoWork += new DoWorkEventHandler(StartReceive);
this.bwReceiver.RunWorkerAsync();
}
#endregion

#region Private Methods
private void StartReceive(object sender , DoWorkEventArgs e)
{
while ( this.socket.Connected )
{
//Read the command's Type.
//byte [] buffer = new byte [sizeof(long)];
//int readBytes = this.networkStream.Read(buffer , 0 , 4);
//if ( readBytes == 0 )
// break;

BasePacket packet = new BasePacket();
try
{
packet = Context.Reader.Read(networkStream);
}
catch (IOException ioex)
{
Disconnect();
}

this.OnPacketReceived(new PacketEventArgs(packet));
}
this.OnDisconnected(new ClientEventArgs(this.socket));
this.Disconnect();
}

private void bwSender_RunWorkerCompleted(object sender , RunWorkerCompletedEventArgs e)
{
if ( !e.Cancelled && e.Error == null && ( (bool)e.Result ) )
this.OnPacketSent(new EventArgs());
else
this.OnPacketFailed(new EventArgs());

( (BackgroundWorker)sender ).Dispose();
GC.Collect();
}

private void bwSender_DoWork(object sender , DoWorkEventArgs e)
{
BasePacket packet = (BasePacket)e.Argument;
e.Result = this.SendPacketToClient(packet);
}

//This Semaphor is to protect the critical section from concurrent access of sender threads.
System.Threading.Semaphore semaphor = new System.Threading.Semaphore(1 , 1);
private bool SendPacketToClient(BasePacket packet)
{

try
{
semaphor.WaitOne();

Context.Writer.Write(networkStream, packet);
networkStream.Flush();
#region Removed Source
////Type
//byte [] buffer = new byte [4];
//buffer = BitConverter.GetBytes((int)cmd.PacketType);
//this.networkStream.Write(buffer , 0 , 4);
//this.networkStream.Flush();


//if (cmd.PacketType != PacketType.Frame)
//{
// //Meta Data.
// if (cmd.MetaData == null || cmd.MetaData == "")
// cmd.MetaData = "\n";

// byte[] metaBuffer = Encoding.Unicode.GetBytes(cmd.MetaData);
// buffer = new byte[4];
// buffer = BitConverter.GetBytes(metaBuffer.Length);
// this.networkStream.Write(buffer, 0, 4);
// this.networkStream.Flush();
// this.networkStream.Write(metaBuffer, 0, metaBuffer.Length);
// this.networkStream.Flush();
//}
//else
//{

// WepFrame encryptedFrame = new WepEncryption(this.Context).For(cmd.Frame);

// new WepFrameWriter(networkStream).Write(encryptedFrame);
//}
#endregion
semaphor.Release();
return true;
}
catch
{
semaphor.Release();
return false;
}
}
#endregion

#region Public Methods
/// <summary>
/// Sends a command to the remote client if the connection is alive.
/// </summary>
/// <param name="cmd">The command to send.</param>
public void SendPacket(BasePacket packet)
{
if ( this.socket != null && this.socket.Connected )
{
BackgroundWorker bwSender = new BackgroundWorker();
bwSender.DoWork += new DoWorkEventHandler(bwSender_DoWork);
bwSender.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwSender_RunWorkerCompleted);
bwSender.RunWorkerAsync(packet);
}
else
this.OnPacketFailed(new EventArgs());
}



/// <summary>
/// Disconnect the current client manager from the remote client and returns true if the client had been disconnected from the server.
/// </summary>
/// <returns>True if the remote client had been disconnected from the server,otherwise false.</returns>
public bool Disconnect()
{
if (this.socket != null && this.socket.Connected )
{
try
{
this.socket.Shutdown(SocketShutdown.Both);
this.socket.Close();
return true;
}
catch
{
return false;
}
}
else
return true;
}
#endregion

#region Events
/// <summary>
/// Occurs when a command received from a remote client.
/// </summary>
public event PacketReceivedEventHandler PacketReceived;
/// <summary>
/// Occurs when a command received from a remote client.
/// </summary>
/// <param name="e">Received command.</param>
protected virtual void OnPacketReceived(PacketEventArgs e)
{
if ( PacketReceived != null )
PacketReceived(this , e);
}

/// <summary>
/// Occurs when a command had been sent to the remote client successfully.
/// </summary>
public event PacketSentEventHandler PacketSent;
/// <summary>
/// Occurs when a command had been sent to the remote client successfully.
/// </summary>
/// <param name="e">The sent command.</param>
protected virtual void OnPacketSent(EventArgs e)
{
if ( PacketSent != null )
PacketSent(this , e);
}

/// <summary>
/// Occurs when a command sending action had been failed.This is because disconnection or sending exception.
/// </summary>
public event PacketSendingFailedEventHandler PacketFailed;
/// <summary>
/// Occurs when a command sending action had been failed.This is because disconnection or sending exception.
/// </summary>
/// <param name="e">The sent command.</param>
protected virtual void OnPacketFailed(EventArgs e)
{
if ( PacketFailed != null )
PacketFailed(this , e);
}

/// <summary>
/// Occurs when a client disconnected from this server.
/// </summary>
public event DisconnectedEventHandler Disconnected;
/// <summary>
/// Occurs when a client disconnected from this server.
/// </summary>
/// <param name="e">Client information.</param>
protected virtual void OnDisconnected(ClientEventArgs e)
{
if ( Disconnected != null )
Disconnected(this , e);
}

#endregion
}
}



As you can see this is a basic wrapper for the System.Net.Socket class, and it uses a ProtcolContext (listed below ).



namespace BaseNetworkProtocol
{
using System;
using System.Collections.Generic;
using System.Text;
public class ProtocolContext
{
public IProtocolWriter Writer { get; set; }
public IProtocolReader Reader { get; set; }
}
}

which in turn uses a ProtocolReader for reading, and ProtocolWriter for writing to the stream.



public interface IProtocolReader
{
BasePacket Read( Stream stream);

}

and



public interface IProtocolWriter
{
void Write(Stream stream,BasePacket packet);
}

And they read everything into a



public class BasePacket
{
public byte[] Data { get; set; }
}

For convenience I've written down two implementations, that should be sufficient for any type of extension use



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BaseNetworkProtocol
{
public class BaseProtocolReader:IProtocolReader
{
#region IProtocolReader Members

public virtual BasePacket Read(System.IO.Stream stream)
{

byte[] dataLength = new byte[sizeof(int)];
stream.Read(dataLength, 0, dataLength.Length);
int length = BitConverter.ToInt32(dataLength, 0);


byte[] data = new byte[length];
stream.Read(data, 0, length);
BasePacket packet = new BasePacket();
packet.Data = data;
return packet;


}

#endregion
}
}


And



using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace BaseNetworkProtocol
{
public class BaseProtocolWriter : IProtocolWriter
{
#region IProtocolWriter Members

public virtual void Write(System.IO.Stream stream, BasePacket packet)
{
byte[] dataLength = BitConverter.GetBytes(packet.Data.Length);
stream.Write(dataLength,0,dataLength.Length);
stream.Write(
packet.Data,
0,
packet.Data.Length
);
}

#endregion
}
}


As I said this should be very testable, and here is a simple test for it



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BaseNetworkProtocol;
using System.IO;

namespace BaseNetworkProtocolTests
{
[TestClass]
public class TestBaseProtcols
{
[TestMethod]
public void should_write_and_read_the_packet_sent()
{
// Arrange
IProtocolWriter writer = new BaseProtocolWriter();
IProtocolReader reader = new BaseProtocolReader();

BasePacket packet = new BasePacket();
packet.Data = new byte[] { 1, 2, 3 };

MemoryStream communicationChannel = new MemoryStream();
// Act

writer.Write(communicationChannel, packet);
communicationChannel.Position = 0;
BasePacket receivedPacket = reader.Read(communicationChannel);
// Assert
Assert.IsNotNull(packet);
Assert.IsNotNull(receivedPacket);
Assert.IsNotNull(packet.Data);
Assert.IsNotNull(receivedPacket.Data);
Assert.AreEqual(packet.Data.Length, receivedPacket.Data.Length);
for (int dataIndex = 0; dataIndex < packet.Data.Length; dataIndex++)
{
Assert.AreEqual(packet.Data[dataIndex],receivedPacket.Data[dataIndex]);
}
// Clean up
communicationChannel.Close();
communicationChannel.Dispose();
}
}

}



All that's left is to provide some type of protocols to it. And, that's through the use of two Services the ClientService, and the ServerService.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using FinancialServer.Properties;
using BaseNetworkProtocol;
using Core.Utils;
using System.Threading;

namespace FinancialServer.Services
{
internal class ServerService : IServer
{
Socket _socket;
Client _currentClient;
ProtocolContext _context;
public ServerService(ProtocolContext context)
{
_context = context;
}
#region IServerService Members

public void Start()
{
_socket = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
_socket.Bind(new IPEndPoint(
IPAddress.Parse(Settings.Default.ServerAddress),
Settings.Default.Port));

_socket.Listen(5);
//using(var resolver = Program.Container.CreateInnerContainer()){

new Thread(AcceptClients).Start();
//}

}

public void AcceptClients()
{
while (Program.Resolve<IServer>().IsConnected)
{
try
{
_currentClient = new Client(
_socket.Accept(), Program.Resolve<ProtocolContext>());
}
catch (SocketException soex)
{
// TODO: Log it
break;
}
_currentClient.PacketReceived +=
(sender, @event) =>
{
Client _sender = (Client)sender;
string request = new ResponsePacket(@event.Packet).Message<string>();
var result = Program.Resolve<IRequestProcessor>().Process(request);
if (result != null)
{
_sender.SendPacket(
new RequestPacket(
result
).Packet
);
}
//if (request.Equals("1 + 1"))
//{
// _sender.SendPacket
// (new RequestPacket("2").Packet);
//}

};
}
}

public void Send(string message)
{
BasePacket packet = new BasePacket();
packet.Data = System.Text.Encoding.Unicode.GetBytes(message);
_currentClient.SendPacket(packet);

}

public bool IsConnected
{
get
{
return _socket.IsBound;
}
}
public void Close()
{
if (IsConnected)
_socket.Close();
}
#endregion
}
}



The client



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseNetworkProtocol;
using System.Net.Sockets;
using System.Net;
using FinancialClient.Properties;
using Core;
using Core.Services;

namespace FinancialClient.Services
{
public class ClientService : IClientService
{
ProtocolContext context;
Client client;
public ClientService(IProtocolReader reader,
IProtocolWriter writer)
{
context = new ProtocolContext();
context.Reader = reader;
context.Writer = writer;
}

#region IClientService Members

public bool Connect()
{
Socket socket =
new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
try
{
socket.Connect(
IPAddress.Parse(Settings.Default.ServerAddress),
Settings.Default.Port);
client = new Client(
socket,
context);


}catch(SocketException ex){
//using (var resolver = Program.Container.CreateInnerContainer())
//{
Program.Resolve<IErrorService>().Log(ex);
//}
return false;
}
return true;
}
public void SendPacket(BasePacket packet)
{
client.SendPacket(packet);

}
public void ReceivePacket(PacketReceivedEventHandler executeOnReceive)
{
client.PacketReceived += executeOnReceive;
}
public bool IsConnected
{
get
{
return client.Connected;
}
}

#endregion
}
}


The communication is done through serialized objects like this



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseNetworkProtocol;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Core.Utils
{
public class RequestPacket
{
BasePacket basePacket;
public RequestPacket(object request)
{
basePacket = new BasePacket();

IFormatter formatter = new BinaryFormatter();
MemoryStream buffer = new MemoryStream();
formatter.Serialize(buffer, request);
this.basePacket.Data = buffer.ToArray();
}
public BasePacket Packet { get { return basePacket; } }
}
}


And




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseNetworkProtocol;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

namespace Core.Utils
{
public class ResponsePacket
{
private readonly object message;
public ResponsePacket(BasePacket response)
{
if (response.Data == null) {
message = null;

return; }
IFormatter formatter = new BinaryFormatter();
using(MemoryStream ms =
new MemoryStream(response.Data.ToArray()))
message = formatter.Deserialize(ms);
}
public TOBject Message<TOBject>(){

return (TOBject) message;

}

}
}



Note: This has been an extreeemly loong post. And it's so for me to remind myself all of this classes, so I don't go and search the web for solutions that I don't find simple and extensible enough for my needs. At least it's my code, so if something doesn't work I know who to blame.

luni, 13 aprilie 2009

Creating a 2 steps expression based link

This is a simple post highliting how to build a quick extension to HtmlHelper.
You'll need to use the MVC Futures extensions

For the extension all you need is this

namespace Microsoft.Web.Mvc
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Microsoft.Web.Mvc;
public static class MyLinkExtensions
{
public static void BeginLink<TController>(this HtmlHelper helper,
Expression<Action<TController>> action
) where TController : Controller
{
BeginLink(helper,
action,
new { });
}
public static void BeginLink<TController>(this HtmlHelper helper,
Expression<Action<TController>> action,
object htmlAttributes) where TController : Controller
{
BeginLink(helper,
action,
new RouteValueDictionary(htmlAttributes));
}
public static void BeginLink<TController>(this HtmlHelper helper,
Expression<Action<TController>> action,
IDictionary<string, object> htmlAttributes) where TController : Controller
{
TagBuilder builder = new TagBuilder("a");
builder.MergeAttributes(htmlAttributes);
string href = Microsoft.Web.Mvc.LinkExtensions.BuildUrlFromExpression(helper, action);
builder.MergeAttribute("href", href);

HttpResponseBase httpResponse = helper.ViewContext.HttpContext.Response;
httpResponse.Write(builder.ToString(TagRenderMode.StartTag));

}

public static void EndLink(this HtmlHelper helper)
{
TagBuilder tagBuilder = new TagBuilder("a");
HttpResponseBase httpResponse = helper.ViewContext.HttpContext.Response;
httpResponse.Write(tagBuilder.ToString( TagRenderMode.EndTag));
}
}
}


And to use it, all you need is something like :


<% Html.BeginLink < HomeController >( c=> c.About() ); %>
Link text
<% Html.EndLink(); >


Note: Don't forget to add Microsoft.Web.Mvc to the namespaces that the view uses