Coding Adventure
The adventure of writing meaningful code.
The adventure of writing meaningful code.
Aug 20th
When working with asynch operations on WP7 one of the easiest gotchas is accessing UI elements from different threads. The typical error looks sorta like this [InvalidCrossThreadAccess].
It is very easy to solve. The easiest way to solve the problem is using BeginInvoke :
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
textBox1.Text = responseString;
});
Jul 25th
Linq to SQL is great. I love it because it adds a simple abstraction layer that can greatly speed up building a data access layer.
If not used properly, LINQ to SQL can also create performance issues. Here are my general LINQ to SQL guidelines when I work in projects:
This is mostly a general C# programming guideline but there have been several times when I see programmers missing this step. Here is more information from MSDN.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object’s resources.
A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
Here is an example:
using (NorthwindDataContext context = new NorthwindDataContext())
{
//do stuff here
}
To query something with LINQ to SQL there are several “startup” procedures. This procedures are not too bad when queries are not used too often. If the same query is done several times, its heavy and it is the core of the product then it is VERY important to make it a compiled query.
I will not go into too many details about this because there are several posts about the subject:
Contexts are meant to keep track of the objects in the database. By having small contexts with a single purpose then the burden of tracking is lessen and therefore there is less memory consumption.
There are two good ways to improve the performance of queries that do not involve concurrency issues:
For object tracking, is super easy to turn off:
context.ObjectTrackingEnabled = false;
Combining queries is a good idea when working with databases, just grab what you need and aggregate the data into a POCO model or anonymous type. Finally, if extreme fine control is needed, there is always custom expressions.
Jul 24th
Recently I was setting up Teamcity and the build agent kept going down. It was starting and stopping. Sometimes it was “starting…” for a long time.
After doing a bit of research I came across this. The agents would appear only for seconds under team city and then go down as inactive with the message “Agent has unregistered (will upgrade)”. The culprit was my antivirus. Apparently this is a common issue but I have not seen many people blog about it.
Jul 16th
Many developers forget that in our industry almost everything we do is a service. We are performing work for others at some level. In many cases there will errors in the service provided and then it is when we can measure the quality of the service.
In my opinion, it is an error to try to rate quality of a service as a relation between bugs and release version (this measures how effective are processes). I like to measure quality as a fixed bugs, release version and days to release correlation. This way, we can measure how good is the service by measuring how fast the bugs can be fixed (and properly fixed).
I am not a fan of the even if version 1 sucks, ship it anyway theory but it has some good points. Most of the clients that love my work are happy because any bugs that appeared on the systems have been fixed fast (and remain fixed).
Even google services go down but we all keep using them because google fixes them right away. How can one measure how good is a service? It can be measured by how fast it can respond to change.
Jul 3rd
If you use Sophos Antivirus you might be getting Tortoise SVN related erros. It took me a while to figure it out. The best way to fix it is to exclude your projects directory from Sophos active scan.
Jul 3rd
One of the common questions about IOC is how to pass parameters. This question is specially common with StructureMap since a lot of the old methods have been deprecated.
Here is a quick Example:
public class DataSource : IDataSource
{
public DataSource(string URL, string account, string password)
{
//your code here
}
}
ObjectFactory.Initialize(x =>
{
x.For<IDataSource>().Use<DataSource>()
.Ctor<string>("URL").Is(URL)
.Ctor<string>("account").Is(account)
.Ctor<string>("password").Is(password)
;
}
and you can call it like this:
private IDataSource ds = ObjectFactory.GetInstance<IDataSource>();
There is a huge amount of benefits as I mentioned in my previous post, including saving a lot of typing since all parameters are preconfigured.
Jul 2nd
I was not a big fan of VLC. It felt a bit slow and it did not seem to render as smothly as MPC.
Now that with the latest release of VLC that has truly changed. VLC 1.1.0 meets all my expectations. They did an excelent job with the GPU and DSP decoding.
Here is the developers log from GIT: http://git.videolan.org/?p=vlc/vlc-1.1.git;a=tag;h=1.1.0
VLC 1.1.0 - ‘The Luggage’
The first release of the 1.1.x branch of VLCThis is a major release, adding major features, notably:
- GPU and DSP decoding on selected platforms
- New support for codecs, demuxers and muxers
- Lua extensions and Lua content extensions (luaSD)
- Improved interfaces
- Video Output rework
- Removal of lots of modules, rewrite of many
- Improved performances, in CPU, RAM and I/O
- New libVLC and bindings
- New or improved ports on misc platforms
And so many bugs and other features…
Jun 29th
I am very happy Microsoft updated the Azure Tools to have full integration with Visual Studio. It is extremely easy to set up, just a few clicks to create a certificate and upload it. The process is all guided and it took me around 30 seconds. After the certificate has been uploaded you can deploy from Visual Studio with a neat status bar.
There are some other neat features like IntelliTrace and others that are now available.
Here is a link to the latest release: Windows Azure Tools for Microsoft Visual Studio 1.2 (June 2010)
Jun 26th
Sometimes it is needed to have complete control over how WCF manipulates the data being returned. By default, WCF serializes objects and returns them as XML. Sadly, there is not much control on how to create templates over how objects will be serialized (flat structure, hierarchical, etc). In many cases this does not matter. A few weeks ago I stumbled for the first time when I need 100% control over how the XML was formated and being sent.
To send custom formated XML use the message envelope and not the string datatype. If the envelope is not used, it will add extra meta content.
Here is how it can be done:
public Message GetMessage(string xml)
{
XmlDocument x = new XmlDocument();
//This is very important as it will VALIDATE the XML. Saved my butt a few times.
x.LoadXml(xml);
XmlElementBodyWriter writer = new XmlElementBodyWriter(x.DocumentElement);
Message msg = Message.CreateMessage(MessageVersion.None,
OperationContext.Current.OutgoingMessageHeaders.Action, writer);
return msg;
}
public class XmlElementBodyWriter : BodyWriter
{
XmlElement xmlElement;
public XmlElementBodyWriter(XmlElement xmlElement)
: base(true)
{
this.xmlElement = xmlElement;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
xmlElement.WriteTo(writer);
}
}
Also a little warning, the string passed in should be formated and already encoded in the format you want. This can make a huge difference specially when internationalization is involved.
Jun 23rd
Value Objects are objects that can be shared across different parts of a program. This can have great benefits in performance. Because the objects are shared it is very important for value objects to be immutable. If value objects are not immutable then any part of the program can change the values and all the other parts can do calculation with erroneous data.
One of my goals was to make immutable objects in F# to take advantage of parallelization and automatic compiler optimizations:
type City(Name:string, X: float, Y:float) = member t.Name = Name member t.X = X member t.Y = Y type NeighborCities(city1:string, city2:string) = member x.fromCity = city1 member x.toCity = city2
Objects created in this manner in F# are immutable. Even when they are accessed in other projects outside their definition, their members are read-only. This was specially helpful on the distributed traveling salesman problem I built last year.