Wednesday, December 9, 2009

Best place for data-binding

In the last 3 months I was involved in development of ASP.Net websites. All the projects were started when I joined them, so a lot of code was written.
All the data-related modifications were implemented in this manner:

  • make the databinding on page_load event

  • handle control events (button click, checkbox check, ...) and make the databinding after any change


This was confusing, when we have several controls causing events, which was causing to do the same databinding several times.
So a question arised - how to reduce the number of databindings - where to make the dtabinding. And here is my answer: Do all the data-related changes on PreRender stage. This have a lot of good pooints:

  • It runs only once per request

  • It runs after all control events, which ensures, that no more flow-changes will arise.


This was a good solution these projects and hope it will also help some of you.

Tuesday, October 6, 2009

LoadControl problem in ASP.Net

Today I met a problem and it made me angry enough to make some dumb checks and found an interesting thing conecrning the runtime control loading. I was trying to load controls using the LoadControl method in this way (using this overload of the method): LoadControl(type, object[]). This seemed ok - the control is being instanciated but when I was trying to access a property of it - found that all the inner controls were not loaded and I was getting NullReferenceException.
And here was the solution - seems that the LoadControl(string) overload works much more better and using this overload you can get the required control with all its properties initialized.
Good luck.

Saturday, October 3, 2009

Get all the best from VS.Net

After moving to a new company I have found that all the developers here have installed some addons on the VisualStudio.Net 2008 which make it load slower and found that they only use it for some features like outline.
This make me to write this post about the best and also most important feature in VS.Net, which is not expsed by default (only in VS.Net 2003). This feature is just Class View Synchronization with the class/struct, your cursor is in. So here how you can enable it.
1. In the toolbar area right click and select Customize... from the opened context menu.
2. Go to the Toolbars tab, and enable the ContextMenus item in the list. This will make a new toolbar appear in the toolbars.
3. Go to the Commands tab of the Customize window, and select the View item in the Categories list.
4. Now navigate to the Synchronize Class View item in the Commands part of the same tab. Drag it to the new added toolbars Editor Context Menus item (a menu will appear), and drop somewhere in the Code Window submenu.
Here it is. Now you can use that amazing feature from your context menu.

Tuesday, June 9, 2009

Looking for an invester

After two years of private work, I got several project ideas, which currently are waiting for their time to be implementd. But ... they are big, and they have a lot of investments. I tried to look into different websites, which were offering some kind of "find-an-invester" solutions. But all of them were somehow alike, and had some non-friendly points in the policy. So I have decied write this blog - post and ask someone, who will read it and will be interested in investing in really good projects, which will become into huge companies later.

Thanks a lot

Monday, May 11, 2009

Underhood of the "ref" keyword

One of the most interesting keywords in C# is the "ref" keyword. This is a argument parameter, which causes the method (indexer, constructor...) parameters to be passed by reference.
Let's take a look to the following code snippet:

public void TestFunction(int argValue)
{
      argValue++;
}

When we pass an argument to this function a new copy of the argument is being created and the function operates on the copy - not the real object. Because the copy of a reference again points to the sane location, we get no difference using the "ref" keyword with the reference-parameters (in most cases).
Now let's add a ref keyword before the parameter declaration:

public void TestFunction(ref int argValue)
{
      argValue++;
}

In the previous example we will get no result because the ++ operation was done on a copy of the object. But if we now pass the an integer to the function (when calling the function now, already, we have to add the "ref" keyword also here), we will get the increase of the value of the original memory, because this keyword makes the function to operate on the reference of the given parameter.

And here is the most important case. Many developers see no difference in "ref" keyword when using it with reference types. But... let's now review the following interesting example:

public void TestFunction(object argParam)
{
      argParam = null;
}

When entering this function the CLR will create a copy of the reference and will operated on it. But because the copied reference will refer to the same object on the heap, we will get the same result using the object's methods. And when we assign null to the argParam in the function body, we will just remove the copied reference of the parameter, which will take no modification on the real object.

And now, let's add the ref keyword before the param declaration:

public void TestFunction(ref object argParam)
{
      argParam = null;
}

Here comes the specific behavior of the keyword. No copy of the reference is being created during function call, so when we assign a null value to the argument, we remove the original reference from the memory, and ... in the calling context - lose the reference to the object.

Monday, April 13, 2009

Developing distributed software

After development of the first version of the "Legends Of Noobia" server (MMORPG game server, written on .Net) I thought about later versions of it where the distributed features should be surely included. And here comes the main question - how to extend an existing application to support distribution ?
Here what came on my mind first - distributed TCP messageing system. because this component will let me attach the same game server (for this special case, but can be any other application) instance on different servers, where the DTMS (Distributed TCP/IP Messaging Server) node will be configured and run in the same way as it was used to.
Now let's go deeper into details to understand the idea of the DTMS. Imagine a graph, where each node is a configured instance of the system. Let's call each node a Physical Node. Each physical node is simple TCP/IP Server which accepts client connection. But - here comes the main idea - it also accepts connections from neighbour nodes (Physical Nodes from other servers, or even from the same server but another instance) and becomes a node of the distriubuted messaging graph. When a client sends a message - the server distributes this messages through all the neighbour nodes. This let's two clients, being connected to different physical nodes send/receive messages with/to each other.
To support private messaging we can add the recepient identifier to the header of the message. Each node, receiving the message, checks whether the recepient is connected to the system through it and if so, it sends the message to the client, otherwise it distributes the message to all nodes, except the one, which from it received the message.
In the same - easy, way the system can also support group messaging simply adding a group identifier to each message.

Friday, March 6, 2009

Using previously indtroduced Tween

In my previous post I introduced a Tween class, which lets you to manipulate object Properties in timely manner.
Let's consider we have a simple button on client area, which has "btnButton" name. The following code will change the button's width from 50 to 300 pixels in 3 seconds (3000 milliseconds).

Tween tmpTween = new Tween();
tmpTween.MaxValue = 300;
tmpTween.MinValue = 50;
tmpTween.Period = 10000;
tmpTween.Target = this.btnTest;
tmpTween.Property = "Height";
tmpTween.Start();

Tween in Silverlight

During my Silverlight development I noticed that I need a feature which present in Adobe Flash - the tween. It's a class which lets you to increase a property for a given object from MinValue to MaxValue in a given time period.
So I have implemented it in C# for Silverlight 2. Here is the code for it.


///ITween.cs - Tween support for Silverlight 2
///The code is written by Artak Mkrtchyan
///Exposed under GNU license at http://mkartak.blogspot.com
///Contact me by: mkartak@gmail.com
public interface ITween
{
void Start();
void Stop();

double Period
{
get;
set;
}

object Target
{
get;
set;
}

string Property
{
get;
set;
}

double MinValue
{
get;
set;
}

double MaxValue
{
get;
set;
}
}


///Tween.cs - Tween support for Silverlight 2
///The code is written by Artak Mkrtchyan
///Exposed under GNU license at http://mkartak.blogspot.com
///Contact me by: mkartak@gmail.com
public class Tween : ITween
{
private object target;
private string property;

private double period;

private double minValue;
private double maxValue;

private bool started = false;
private PropertyInfo targetPropertyInfo;
private double currentValue;

private DispatcherTimer timer;

public Tween()
{

}

#region ITween Members

public void Start()
{
if (!this.started)
{
if (this.Period < 0)
{
throw new ArgumentOutOfRangeException("Period must be more than zero");
}

if (this.Target == null)
{
throw new ArgumentNullException("Target");
}

if (this.Property == null)
{
throw new ArgumentNullException("Property");
}

if (this.Property.Trim() == String.Empty)
{
throw new ArgumentException("Property must have a value");
}

this.started = true;

this.targetPropertyInfo = this.Target.GetType().GetProperty(this.Property);
this.currentValue = this.MinValue;
this.timer = new DispatcherTimer();
this.timer.Tick += new EventHandler(this.OnTimerEvent);
this.timer.Interval = TimeSpan.FromMilliseconds(this.MinPeriod);
this.timer.Start();
}
}

public void Stop()
{
if (this.started)
{
this.started = false;
this.timer.Stop();
}
}

public double Period
{
get
{
return this.period;
}
set
{
this.period = value;
}
}

public object Target
{
get
{
return this.target;
}
set
{
this.target = value;
}
}

public string Property
{
get
{
return this.property;
}
set
{
this.property = value;
}
}

public double MinValue
{
get
{
return this.minValue;
}
set
{
if (this.started)
{
throw new Exception("Unable to change value during progress");
}

if (value >= this.maxValue)
{
throw new ArgumentException("MinValue must be less than the maxValue");
}

this.minValue = value;
}
}

public double MaxValue
{
get
{
return this.maxValue;
}
set
{
if (this.started)
{
throw new Exception("Unable to change value during progress");
}

if (value <= this.minValue)
{
throw new ArgumentException("MaxValue must be more than the minValue");
}

this.maxValue = value;
}
}

#endregion

private double Step
{
get
{
return (this.MaxValue - this.MinValue) / StepCount;
}
}

private double Speed
{
get
{
return (this.MaxValue - this.MinValue) / this.Period;
}
}

private double StepCount
{
get
{
return Math.Ceiling(this.Period / this.MinPeriod);
}
}

private int MinPeriod
{
get
{
return 10;
}
}

private void OnTimerEvent(object sender, EventArgs e)
{
if (this.started)
{
lock (this)
{
this.currentValue = Math.Min(this.currentValue + this.Step, this.MaxValue);

this.targetPropertyInfo.SetValue(this.Target, this.currentValue, null);

if (this.currentValue == this.MaxValue)
{
this.Stop();
}
}
}
}
}

Friday, February 13, 2009

Introducing LegendsOfNoobia

Legends Of Noobia is an online multi-player role-playing game (MORPG).
After the quick registration of your character, you appear in a city center of mystical Noobia! You may communicate with other warriors, chat with those that are in the same location with you and also outside of it (such as shout which makes everyone hear your message).
You begin moving around different locations in the world where various NPCs (non-player characters) meet you. Some are kind - just polite and hospitable citizens of Noobia, and some ... awfully angry. You
suddenly find you in a pine forest where a green snail is crawling slowly over the leaves and a spider is looking at you from its web. And you decide to attack one of them. Started the fight and ...
Wooow! you killed him and gained a level ! The more you gain levels the stronger you become, and the harder it becomes to kill you. The more powerful NPCs you kill, the more experience you gain. At high levels, however, you definitely will not be able to struggle one alone against the 20 leveled Dracula sleeping in old graves, and may consider acting with a team in order to kill such a monster.
Your goal is not just killing and gaining levels, but also fighting together with other players in a team, helping each other, by hints, messages and most importantly - by healing.
Check out this flash game and try out your strength by just browsing: www.legendsofnoobia.com.