Programming
Stop! Or I am writing another framework!
There is a small dwarf in my head that whispers about persistent binary trees and RESTful services, views using DSL and JSON data interchange. In other words, I really want to write NCouchDB. I had the exact same thing happening with queuing, and it didn't stop until I wrote Rhino Queues (five or six times!) I keep trying to justify to myself that this is needed. But I already busy over my head. Please, help me stop this, before I lose all control.
Get that queue out of my head
For some reason, I am having a lot of trouble lately getting Rhino Queues out of my head. I don't actually have a project to use it on, but it keep popping up in my mind. Before committing to the fifth or sixth rewrite of the project, I decided to take a stub at testing its performance. The scenario is sending a hundred thousand (100,000) messages, with sizes ranging from 4Kb to 16Kb, which are the most common sizes for most messages. On my local machine, I can push that amount of data in 02:02.02. So just above two...
What I am working on...
I am just going to post that, and watch what happens. I will note that this is code that I just wrote, from scratch. public class TaxCalculator
{
private string conStr;
private DataSet rates;
public TaxCalculator(string conStr)
{
this.conStr = conStr;
using (SqlConnection con = new SqlConnection(conStr))
{
con.Open();
...
Legacy Driven Development
Here is an interesting problem that I run into. I needed to produce an XML document for an external system to consume. This is a fairly complex document format, and there are a lot of scenarios to support. I began to test drive the creation of the XML document, but it turn out that I kept having to make changes as I run into more scenarios that invalidated previous assumptions that I made. Now, we are talking about a very short iteration cycle, I might write a test to validate an assumption (attempting to put two items in the...
re: MVC Storefront Part 19: Processing Orders With Windows Workflow
Rob Conery has another MVC Storefront post, this time focusing on using Windows Workflow. Those are my random impressions: You probably do want to test your work flow. In the same way you want to have an integration test for the system. The sequence work flow seems to be a very heavy weight approach to just orchestrating actions in the application. I wonder what the perf implications of creating a workflow here would be. My gut feeling is that this is not good, but I don't really have data...
3 Levels Of Failures
You have the no-failure, everything completed successfully and everyone is happy. You have complete-failure, someone just pull the power cord off the machine. And in the middle, you have partial-failure, something bad happened, (network is down, someone pulled the USB key out) but the system can (and should) keep running. Most people give up on the complete failure scenario, leaving that up to transactional storage to handle this. That is usually the smartest solution. The problem occur with partial failure, where you need to recover in...
Useless Java
This article (The 'Anti-Java' Professor and the Jobless Programmers) has really annoyed me. Not because of its content, but because the professor being interview is so narrowly focused that he is completely out of it. Let us take this quote: “Furthermore, Java is mainly used in Web applications that are mostly fairly trivial,” Dewar says, with his characteristic candor. “If all we do is train students to be able to do simple Web programming in Java, they won't get jobs, since those are the jobs that can be easily outsourced. What we need are software...
Not all objects are created equals
I found something extremely surprising while profiling a project. Take a look at this piece of code: Stopwatch stop = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
new WeakReference(null);
}
stop.Stop();
Console.WriteLine("WeakRef: " + stop.ElapsedMilliseconds);
stop = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
new string('a', 5);
}
stop.Stop();
Console.WriteLine("'aaaaa': " + stop.ElapsedMilliseconds);
On my machine, this has the following output:
WeakRef: 980
'aaaaa': 35
Creating a WeakReference is much more costly than creating a normal object. Not surprising, when you think of it, WeakReference has deep ties to the CLR, but I couldn't really believe it when I...
Testing interleaved async file writing
Here is another interesting aspect that I run into when thinking about writing to files. What will happen if I start two async write operations? Will they get interleaved? Will they get interleaved? Will I get errors when using this? Here is using a single stream: var path = "test.txt";
if (File.Exists(path))
File.Delete(path);
var handles = new List<WaitHandle>();
using (
var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None, 0x1000,
FileOptions.Asynchronous))
{
for (int i = 0; i < 64; i++)
{
var handle = new ManualResetEvent(false);
var bytes = Encoding.UTF8.GetBytes( i + Environment.NewLine);
stream.BeginWrite(bytes, 0, bytes.Length, delegate(IAsyncResult ar)
{
stream.EndWrite(ar);
handle.Set();
}, stream);
handles.Add(handle);
}
WaitHandle.WaitAll(handles.ToArray());
stream.Flush();
}
This worked perfectly, the writes are written in...
In search of an embedded DB
I need to use an embedded DB in a project. Naturally, I turned to my old friend, SQLite. It has served me well in the past, and I am well versed in all its tricks. Except... SQLite really fall apart when you are talking about even moderately multi threaded applications. By that I mean, it works just as advertised, for sure. It just lock the entire DB when used, which tend to kill other threads who want to access the poor DB. I then turned to FireBird, but a short spike told me that it is... inadequate. I managed...
Production TDD
I'll leave you the conclusions. The test pass now :-)
Premature optimizations
I just finished doing a second de-optimization for NMemcached. This is the second such change I do, turning the code from using the async read pattern to using a simple serial read from a stream. Both those changes together takes the time to complete my simple (and trivial) benchmark from 5768.5 ms to 2768.6 ms. That is less than 50% of the original time! In both cases, I started with high use of BeginXyz, in order to get as much parallelism as much as possible, but it actually turned out to be a bad decision, since it meant that...
Scratching an itch: NMemcached
Last night I found myself up at 1 AM, feeling restless. I decided that I need a new project, something that is very simple and will allow me to channel some energy. I decided to pursue something that I have long been curious about, build a Memcached server in .Net. The Memcached server is a distributed cache server that is very popular in the Ruby, PHP & Perl worlds, with some following on both Java & .Net side. It is written in C, and it is has some fairly interesting characteristics. Chief among them, it is a straightforward technical...
Verifying assumptions with tests
Here are a few interesting tests that I just wrote: [TestFixture]
public class SystemWebCacheTests : CacheMixin
{
[SetUp]
public void Setup()
{
ClearCache();
}
[Test]
public void IterationWithModifications()
{
Cache["foo"] = new object();
Cache["bar"] = new object();
foreach (DictionaryEntry de in Cache)
{
Cache.Remove(de.Key.ToString());
}
Assert.AreEqual(0, Cache.Count);
}
[Test]
public void IterationWithAdditions_ShouldGet_ConsistentSnapshot()
{
Cache["foo"] = new object();
Cache["bar"] = new object();
bool first = true;
int count = 0;
foreach (DictionaryEntry de in Cache)
{
count += 1;
if(first==false)
Cache["baz"] = new object();
first = false;
}
Assert.AreEqual(2, count);
Assert.AreEqual(3, Cache.Count);
}
[Test]
public void IterationWithRemoval_ShouldGet_ConsistentSnapshot()
{
Cache["foo"] = new object();
Cache["bar"] = new object();
bool first = true;
int count = 0;
foreach (DictionaryEntry de in Cache)
{
count += 1;
if (first)
Cache.Remove("bar");
Assert.IsNotNull(de.Value);
Assert.IsNotNull(de.Key);
first = false;
}
Assert.AreEqual(2, count);
Assert.AreEqual(1, Cache.Count);
}
}
I have no idea if this is just a coincidental evidence, but I like the...
Challenge: Striving for better syntax
Or, as I like to call them, yes another stupid language limitation. I did some work today on Rhino Mocks, and after being immersed for so long in DSL land, I had a rude awakening when I remembered just how much inflexible the C# language is. Case in point, I have the following interface: public interface IDuckPond
{
Duck GetDuckById(int id);
Duck GetDuckByNameAndQuack(string name, Quack q);
}
I want to get to a situation where the following will compile successfully:
IDuckPond pond = null;
pond.Stub( x => x.GetDuckById );
pond.Stub( x => x.GetDuckByNameAndQuack );
Any ideas?
Note that unlike my other challenges, I...
Resharper 4.0 in now in beta
Just got the email about it. ReSharper 4.0 moved to a beta status. You can find the details here. Personally, I have been using a fairly old build, with relatively few problems.
Matching SVK revision numbers to the SVN revision number
I am using SVK as a way to learn Distributed SCM, and it has been fine so far, except for one big issue. I am trying to apply SVN patches against SVK repositories. Actually, I setup SVK so I work against a local mirror, so the workflow is exactly the same, but the revision numbers are different. Trying to apply a patch just doesn't work because of that. You can go into the patch and edit the root revision, so it is not hard to make this work, except... SVK doesn't give me any mapping between the local...
Critical Mass
When a software project is started, there is a lot of new functionality to be written. Most of the time you are dealing with new code. After a while, if it is done right, you have a solid foundation that you can use to keep building the application.
Whenever I recognize this moment, it feels me with deep sense of satisfaction, this is how it should be. Rhino Mocks has reached that point some years ago, and I noticed that SvnBridge has crossed that point as well recently.
As telltales for this, I use the amount of effort it takes to build...
A Messaging Saga
One of the core concepts in NServiceBus is the idea of a Saga. A saga is a stateful class, responsible for handling a set of related messages. It is important to understand that the saga is not kept in memory between messages, but persisted to storage. This means that we can have large amount of concurrent sagas, without worrying about about memory consumption. Let us take a real example, from my previous post, and see how a saga can take care of it. [Serializable, Transactional]
public class CandidateVerificationSaga
: ISaga<VerifyCandidateMessage>,
ISaga<ReferenceVerificationMessage>,
...
Hibernating Torrent: A simple torrent server for Windows
I think that I already mentioned that I started to run into scaling limits with my screen casts. More specifically, the latest screen cast, was already downloaded 7,300 times! It is a 160Mb file, it also means that I have to pay for obscene amount of traffic. Since I didn't want to pay for all that bandwidth, and since in principal I don't believe in having to pay to give something (which cost a lot to make) for free, I was in somewhat of a problem. The idea of torrents was very attractive, except... There are...
How to get good people: Two phase code tests
It is fairly common in job interviews to request some sort of a code sample. Generally a solution for a problem that the prospective employer gives the candidate. An interesting twist on that is to go over the code, give a single additional requirement, then see what the result it.
After writing my own "XML Parser", SOAP Stack was the obvious next target
Okay, this left really bad taste in my mouth. I had to do SOAP based called today using: WebRequest.Create() Handle the correct SOAP Action. Construct the request using string concatenation. Manually parse the result using XmlDocument and xpath. I thought that the whole point of using web services was to avoid this bloody nonsense stuff. Urgh!
The mysterious life of mutexes
Last night I was talking with Ron Grabowski (of log4net fame) and the subject of mutexes came up. Specifically, the subject of abandoned mutexes across threads, AppDomains, and Processes. I was pretty sure what the behavior should be, but we wrote a set of test cases for that, with... surprising results. My expectation that after I get a hold on a mutex, I have to explicitly let it go. If I am killed or interrupted somehow, then I would expect the next person to acquire the mutex to get an AbandonedMutexException. The first code sample does exactly that:...
Continuous Environment Validation
For some reason, it seems like I am talking about Release It a lot lately, to a lot of people. As I said when I reviewed it, that book literally changed the way that I approach problems. It also made me much more aware of the failure scenarios that I need to deal with. A while ago I sat down in one of Jeremy Miller's talks and he mentioned that he had added the ability to do Environment Validation to StructureMap, so when the application is starting up, it can verify that all its dependencies are in a valid...
MsBuild vs. NAnt
A long while ago I moved the Rhino Tools repository to using MSBuild. I am not quite sure what the reason for that was. I think it was related to the ability to build it without any external dependencies. That is not a real consideration anymore, but I kept that up because it didn't cause any pain. Now, it does. More specifically, I can't build the rhino tools project on a machine that only have 3.5 on it. The reason is that I have a hard coded path with 2.0, which worked well enough, until I tried to...
Performance, threading and double checked locks
A very common pattern for lazy initialization of expensive things is this: GetDescriptionsHelper.Delegate getDescription;
if (GetDescriptionsHelper.Cache.TryGetValue(entityType, out getDescription) == false)
{
MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
typeof(GetDescriptionsHelper.Delegate),
getDescriptionInternalGeneric);
GetDescriptionsHelper.Cache.Add(entityType, getDescription);
}
This tends to break down when we are talking about code that can run in multiply threads. So we start writing this:
lock(GetDescriptionsHelper.Cache)
{
GetDescriptionsHelper.Delegate getDescription;
if (GetDescriptionsHelper.Cache.TryGetValue(entityType, out getDescription) == false)
{
MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
typeof(GetDescriptionsHelper.Delegate),
getDescriptionInternalGeneric);
GetDescriptionsHelper.Cache.Add(entityType, getDescription);
}
}
Except, this is really bad for performance, because we always lock when we try to get the value out. So we start playing with it and get this:
GetDescriptionsHelper.Delegate getDescription;
if (GetDescriptionsHelper.Cache.TryGetValue(entityType, out getDescription) == false)
{
lock(GetDescriptionsHelper.Cache)
{
MethodInfo getDescriptionInternalGeneric = getDescriptionInternalMethodInfo.MakeGenericMethod(entityType);
getDescription = (GetDescriptionsHelper.Delegate)Delegate.CreateDelegate(
typeof(GetDescriptionsHelper.Delegate),
getDescriptionInternalGeneric);
GetDescriptionsHelper.Cache.Add(entityType, getDescription);
}
}
This code has a...
Task Scheduling Improvements
I took some time to see how I can improve the sample implementation of the task scheduling that I posted yesterday. You can checkout the code here. What I wanted to check is the ability to scale the solution to many tasks and to IO bound tasks. In a stress test that I made, the library held its own while scheduling 9,864,100 tasks, at one point we had ~3,500,000 concurrently queued tasks. Memory usage hovered at the ~500Mb range. The next stage was to see what happens when we have tasks that takes a long amount of time to execute,...
Erlang processes on the CLR
I mentioned that getting to Erlang like processes is something that I really want to get to. I started thinking about how I can make it work with the IEnumerable<T> implementation. As it turned out, that was fairly simple, once I got the idea. Take a look at this class diagram: We have tasks, and we register them in the scheduler. We can also wait for them without blocking the thread. Check out this trivial example. We have the following long running task: public class MultiplicationTask : AbstractTask
{
private readonly int value;
...
It's the future now
For various reasons, I had to implement the Future pattern in two completely different settings in three different ways today. A future is a place-holder for the undetermined result of a (concurrent) computation. Once the computation delivers a result, the associated future is eliminated by globally replacing it with the result value. That value may be a future on its own. The last implementation was the explicit one, it was simply: public class Future<T>
{
private T value;
private bool hasValue;
private string error;
public void SetValue(T value)
{
this.value = value;
hasValue = true;
}
public void SetError(string error)
{
this.error = error;
}
public bool HasError
{
get { return error != null;...
Import/Export Business, Est. Rube Goldberg and Co.
I want to get local access to my blog data. The problem is that it is somewhere on a server, and while I can probably open a connection to it, it is not really something that I would want to deal with on a regular basis. So, I decided to export the data locally, and work on it from there. After some failed restarts, I had come to the realization that trying to copy that much data over a WAN is probably not a good idea. So I started to look at local export solutions, which I could then...
Silverlight RTL
And on the heels of the Blame Microsoft post, here is a perfect example. Silverlight doesn't support Right to Left languages. This means that if I want to display Hebrew or Arabic content, it is flat out. This is usually the place where you sharpen a steak knife and go to meeting with a lot of red faced people and you need to wear ear muffs. Until an angel appears... Justin Angel went ahead and built RTL support to Silverlight. Check the links for details, but there a CodePlex project, a webcast and an online demo. Rendering Comparison ...
More generics gotchas, argh!
I have been working on .NET 2.0 for a long time now, generics still manage to trip me. Consider this piece of code: public interface IFoo<TFromInterface> { }
public class Foo<TFromClass> : IFoo<T> { }
[Test]
public void More_Generics_Gotcha()
{
Assert.AreEqual(typeof(IFoo<>),
typeof(Foo<>).GetInterfaces()[0]);
}
This test fails. the returned System.Type instance is a IFoo<TFromClass>, which is an unbounded generic parameter, but not the same as IFoo<> itself. Now I need to apologize for Windsor, it wasn't its fault all along.
D.O.A Software
From Don Demsak on the ALT.Net list: D.O.A. - Designer Oriented Architecture. Which is a good start for this post, which talks about Business Process Management tools, and manage to express some of the pain that I feel very clearly: And here is where it breaks down. All these unusable drag and drop tools, and “easy” XML programming languages aren’t targeted at programmers. They are targeted to suits who can buy into the idea that some non-techy is going to orchestrate these services and modify business rules. These products are unworkable because they are based on the idea that...
On Rails, Ruby and the CLR
Rob Conery has an excellent post about Imploding Rails, which manage to hit a lot of my own thoughts about Rails. I believe that I have stated it before, I am thrilled by what is going on there, but I don't see a good reason to move, since all the nice stuff are either already here or can be brought here. And yes, that include the Ruby's magic. James Avery has a broader look at this, which I agree to, but doesn't negate's Rob's points. Rob's comment on James' post had me LOLing: Ruby is slow, I need to...
From demo to production: Handling the edge cases that aren't there
There is a difference between getting a scenario to working and actually finishing it. The first part is just making sure that the tech stuff work, that is generally easy, the hard part is getting all the stuff that you need to take care of taken care of. The biggest problem is that often you don't really see those issues. Let us start with an example from today. Let us have this user story: As part of the nightly maintenance routines, two days before the event, the system will remind all the participants about the event using SMS....
The CRM Horror
It is rare that I get to the foint where I am just flat out speechless from seeing something. Today I went beyond that, I was flat out speechless and aghast. The image that you see here is a small part of the FilteredAccount view in Micorosft CRM. Yes, you got that right, a small part. I got to this from experimenting with the DB model, trying to figure out how things work. I was strongly adviced not to make any use of any sort of view that started with Filtered. "It would make you cry", they said,...
Trying to mimic Erlang style tasks in C#
After I lamented about missing key features for Erlang style tasks, Avish has told me about ThreadPool.RegisterWaitForSingleObject, which I was unaware of. I bang out this implementation of parallel sort as an example. Hacking code, proof of concept, written at 3AM in the morning, etc. public static WaitHandle ParQuickSort<T>(T[] domain, int lo, int hi)
where T : IComparable<T>
{
if (hi - lo <= 1)
{
return new ManualResetEvent(true); //already signalled, no wait
}
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
Semaphore semaphore = new Semaphore(0, 2);
int pivot = Partition(domain, lo, hi);
WaitOrTimerCallback releaseSemapore = delegate
{
int release = semaphore.Release();
if (release == 1)
{
manualResetEvent.Set();
}
};
ThreadPool.QueueUserWorkItem(
delegate
{
WaitHandle leftWaitHandle = ParQuickSort(domain, lo, pivot - 1);
ThreadPool.RegisterWaitForSingleObject(leftWaitHandle,
...
Refactoring threading code
Let us start by saying that threading is complex, hard and complicated. A while ago I was asked to do a code review of a JoinAll() method, it looked somewhat like this: public void JoinAll(IEnumerable<Thread> threads)
{
foreach (Thread thread in threads)
{
if(thread.Join(500)==false)
thread.Abort();
}
}
I was properly horrified by the use of thread.Abort(), but as you know, all rules have exceptions, so instead of commenting on the implementation details of the method, I asked to understand the entire scenario. This piece of code will probably easier than me trying to explain:
List<Thread> threads = new List<Thread>();
List<string> results = new List<string>();
foreach (string urlParam in GetUrlsToQuery())
{
string url =...
Yet one more reason to like ReSharper
Sometimes it feels like it reads my mind, but in a good way.
Generated Code Quality
Jeremy Miller made the mistake of opening the DataSet.Designer.cs file and got: You know the orange bars that ReSharper puts into the vertical scroll bar to denote warnings in the code? In the DataSet code it looked like a solid bar of orange sherbert. The discussion in the comments is about the quality of generated code, and it is fairly interesting. As someone who does quite a bit with code generation, I wanted to note two things: The quality of the generated code is the result of the generator, with the qualification that the generated code may be used to...
A most applicable approach to the Fertilizer pattern
I recently had a chance to go through several legacy applications, bug spotting in one and evaluating the possibilities of code reuse in another. Stuff like "a most applicable approach to the Fertilizer pattern" began to run on my head as I read it, and at one point I had to go away to be able to curse in private (there were ladies present). Some of the things that I run into (across all the applications): Goto inside a grid implementation, after a little fact finding, it was discovered that they lost the original source, and just copy /...
Development Platforms, again
Jeffery Pallermo commented about the applicability of SharePoint as a development platform, the conversation in the comments is fantastic. Sahil Malik replied to that in a post that says that SharePoint is a terrific development platform. I want to go through Sahil's points for a moment: I haven't ever walked into a shop with a home grown ASP.NET app that was any easier to install than sharepoint is. Here are the steps to install (on a bare bone server) my current project: Prerequisites: IIS .Net 2.0 + 3.0 SQL Server DB Steps: Open cmd.exe \\storage\tools\subversion\svn co svn://subversion/MyProject/trunk MyProject msbuild default.build...
Fluent Refactoring
I have just made the following refactoring, starting from here: Owner owner = GetAccountOwner();
Lookup to = new Lookup(EntityName.queue, Settings.Default.ControlQueueID);
Lookup from = new Lookup(EntityName.systemuser, owner.Value);
Guid regarding = PostState.new_parentpolicy.Value;
Picklist priority = PickLists.EmailPriority.Type.High;
SendMail(from, to, title, body, owner, regarding, priority);
I moved here:
Owner owner = GetAccountOwner();
Email
.Owner(GetAccountOwner())
.From(EntityName.systemuser, owner.Value)
.To(EntityName.queue, Settings.Default.ControlQueueID)
.Regarding(PostState.new_parentpolicy.Value)
.Priority(PickLists.EmailPriority.Type.High)
.Title(title)
.Body(body)
.Send();
Not much in terms of lines of code, but my sense of aesthetics is pleased, at least. And at least it ensures that we won't have this:
SendMail(
new Lookup(EntityName.systemuser, GetAccountOwner().Value),
new Lookup(EntityName.queue, Settings.Default.ControlQueueID),
title,
body,
GetAccountOwner(),
PostState.new_parentpolicy.Value,
PickLists.EmailPriority.Type.High);
Putting things in perspective
Josh Robb is talking about the sense of accomplishment of doing anything non trivial in WebForms, and concludes with: When this happens I experience this strange sense of achievement and even pride. The feeling that I have really done some “hardcore” programming today. Then I remember that I just managed to inject a string into the middle of another string and write the result to a stream. :-)
Mixed Mode Authentication
I just finished spending half a day implementing support for mixed mode authentication in my application. I am putting it here mostly to remind me how it is done, since it was a pretty involved process. As usual, the requirement was that most of the users would use Windows Authentication in a single sign on fashion, and some users would get a login screen. I am using Forms Authentication, and I want to keep is as simple as possible. After some searching, it seems that most of the advice on the web seems to include building two sites, and...
In the authentication maze
Well, it looks like I have once again managed to put myself in a tricky spot. I have the following scenario: Domains: A1 & A2 - no trust or any association between the two. A user access a Website on A1, using windows authentication, which makes a web service call to a machine on domain A2 (anonymous security, at the moment). As a result of the web service call, the machine on A2 needs to make another web service call to A1, and it needs to do it with windows authentication, with the credentials of the original...
Ergonomic Hardware
Like many people who work with computers a lot, I suffer from wrist pain. I recently switched to using a Microsoft Natural Keyboard 4000, and it reduced those issues very quickly. The main benefit of the keyboard, aside from the ergonomic, split, design, is that it is raised, which mean that my wrists doesn't have to bend in order to use the keyboard. I have found that this to be a great product, with the single complaint that the wrist rest hand doesn't stretch enough to the left, so I have no easy way to access the left most keys...
Useful lies about JavaScript Prototype Model
Note: This is not how it works, but it is close enough that it is a valid mental model for most scenarios. When I think about JavaScript and its type system, I usually envision objects as hash tables that can carry values or functions. The prototype idea is just an associated hash table, and so on. The moment that I settled on this mental model, it was much easier to grok javascript. Now, that is a vague explanation if I ever heard one, so let us speak in code: public class Object
{
private readonly Dictionary<string, Procedure> functions
= new Dictionary<string,...
How to test for SQL Injections
Not only does this snippet effective in finding simple SQL Injection attacks: The side effects are highly reduced ratio of second offences, and a sudden improvement in backup practices. Now if I could fix the weeping issue...
I want to see it, live!
Smalltalk had it right from the start, when I want to explore a system, I want to explore a live system, once I can interact with and get immediate results. REPL systems are very good (those are live interpreters, such as boo, perl, python, ruby, etc has) for exploring, but today the requirement for Liveness was brought home very clearly when I needed to work with some web service I knew nothing about. Now, I could have looked at the WSDL, and tried to figure out what it means (either by looking at the WSDL itself or looking at the...
Abstract Syntax Trees
Consider this a very high level explanation, I just wrote this in an email, and I felt that it is a shame to waste the info. AST is the most common way for computers to work with structured text. They turn it into a graph, and then they can do operation on the graph, instead of manual text parsing. Again, this is a rough description of how it works, but it is an essential part of any text parsing routine of any complexity. Most often, compiler, interpreter and the like are based around AST and operations on AST. Let us...
You can't estimate a month
Udi is talking about not trusting a developer's estimate: Developers don’t know how to estimate. Or more specifically, the variance in the actual completion time of a feature from the estimate given by a developer increases probably exponentially with time. For example, if the estimate is a day, you can expect it to be finished in around a day. If the estimate is a week (5 works days), it will probably vary between 4-10 work days. If the estimate is a month, in all actuality the developer probably doesn’t know enough to say but will answer when pressed. I...
Those untrustworthy developers...
From Jeremy Miller's blog: But Jeremy, that might be fine for you, but I can't trust my developers to just do the right thing. I need governance. First, what are you doing to change their behavior? Secondly, why did you hire them in the first place if they can't be trusted, or can you tighten up the hiring process. Three, is that really the case? I've heard this line about not being able to trust the developers far too many times. Why, or how, is the manager responsible, but the developers aren't? Allow me to spin the question...
First Class Functions: Having fun with delegates & design patterns
When writing Rhino ETL, I am getting more and more attached to the idea of first class functions, and their usage. The ability to just send a method (with context!) to be executed elsewhere is amazing. The really nice thing is the ability to express design patterns with focus on the intent vs. focus on the how. As an aside, this post should make your head hurt. Let us take a factory, for example: public delegate WaitHandle WaitHandleFactory();
public WaitHandleFactory CreateWaitHandleForCommand(ICommand command)
{
return delegate
{
ManualResetEvent resetEvent = new ManualResetEvent(false);
command.Completed += delegate { resetEvent.Set(); };
return resetEvent;
};
}
And a decorator:
public delegate void Command();
public Command DecorateWithPerformanceLogging(Command...
Evaluating a Business Platform
A business platform, as far as I care, is an application that I develop on top of. SAP, Oracle Applications, CRM, ERP, etc. Those big applications are usually sold with a hefty price tag, and a promise that if can be modified to the specific organization needs as required. That is often true, actually, but the question is how. This often requires development, and that is where this post comes in. I am a developer, and I evaluate such things with an eye to best practices I use for "normal" development. In a word, I care for Maintainability. Breaking it...
Application Types
At first, there was the Utility, it was written quickly, for doing just this one small thing, and no one cared much about it. Then came the Project, which took a few weeks, and saved some work for people to do. And on the third day the Application, which had users and did useful work. It was both more complex and more valuable. From the trenches, the Batch Process appeared, to make order in the chaos. Over the horizon the Framework came into place, and all was orderly and there was order in the DAL and the BAL....
Map, Reduce, Filter
Dustin Campbell has a couple of posts about Map/Reduce/Filter: Basic Concepts Using it in C# 3.0 He makes it easy to understand, but he forgot one thing that is important, usage of the Map/Reduce/Filter pattern make it very easy to parallelize your code, since you have already separated everything into an action on a set, which can be performed in parallel safely (in most cases).
The value of a feature
How do you value a feature? My own estimation is based on the amount of effort that it took, but the client has a totally different metric. The value of a feature for a client is directional proportional to the amount of pain it removes, with complete and utter disregard to the effort it took to implement. I have been reminded of this when the customer completely failed to appreciate my JavaScript multi threading capabilities (that enabled a drop down list), and was supremely impressed with the user impersonation feature, which took about an hour to write. Amusingly enough, a...
Adding to list from multiple threads?
I just read an email from someone that assume that it is safe to access a collection from multiple threads, if this is done for adding items to the collection only. My gut feeling said that this is a bad idea, but I set out to verify it anyway. Here is the test case: static void Main(string[] args)
{
List<int> ints = new List<int>();
ManualResetEvent resetEvent = new ManualResetEvent(false);
int additions = 0;
int totalCount = 1000000;
for (int i = 0; i < totalCount; i++)
...
7 Approaches for AOP in .Net
Here are all the ways that I can think of to add AOP to your application. This mostly focus on the interception side of things, because once you have that, everything else it just details.
Approach
Advantages
Disadvantages
...
Using partials in Web Forms
Partial is a MonoRail term to a piece of UI that you extract outside, so you can call it again, often with Ajax. This is something that is harder to do in WebForms. Yesterday I found an elegant solution to the problem. ASPX code:<div id="UserDetailsDiv">
<ayende:UserDetails runt="server" ID="TheUserDetails"/>
</div>
User Control:<div>
Name: <asp:label ID="Name" runat="server"/> <br/>
Email: <asp:label ID="Email" runat="server"/>
</div>
Client Side Code:function changeUser(newUserId, div)
{
var srv = new MyApp.Services.UserDetails();
srv.GetUserDetailsView(newUserId, onSucccessGetUserDetailsView, null, div);
}
function onSucccessGetUserDetailsView(response, userContext)
{
var div = $(userContext);
div.innerHTML = response;
new Effect.Highlight(div);
}
Web Service Code:[WebMethod(EnableSession = true)]
public string GetUserDetailsView(int userId)
{
User user = Controller.GetUser(userId);
//there may be a better way to do this, I haven't bothered looking
UserDetails...
ReSharper 3.0 Beta
I am using R# 3.0 Beta, and I am loving it, check out the suggestions: And this one: Or this one: The really great thing about it is that it is three key strokes from suggestions to having better code!
MVC in WebForms: The impossible fight to get rid of the views centric world
Let me start out by saying that I know of a lot of people that are doing MVC with WebForms, I am doing it myself right now, which put me in the position to talk about the pain that it brings. Let us consider the traditional MVC pattern: Now, in such a pattern, who is supposed to be in control? To drive the application? If you have guessed that the "Controller" is the one that is supposed to be in control, you got the jackpot, but missed...
Contrary to popular belief...
Patrick Cauldwell has a post about a set of guiding principal, which I mostly agree on, except: Buy, not build Take full advantage of the platform, even if it only solves the 80% case ...
What I don't like the Patterns & Practices efforts
The P&P groups has a lot of stuff that they release to the world, often with a lot of fanfare and people talking about that. I have reached the point that I no longer care about what the P&P guys are doing. Just having them release something, practically guaratees that this is not something that I would like to use. This has nothing to do with OSS vs. MS or something stupid like that. This has to do with the following reasons: The...
Reflectoring && Learning
I had a few discussions recently with Roy Osherove, about learning WPF. Roy is a fun of Petzold book about it, mainly because it goes through all the API and explain their usage in depth. I haven't read that book, but I am currently going through Adam Nathan's book about WPF, and I am enjoying it very much.
From Roy's perspective, Adam's book is too high level, not giving all the details that Roy feels he should know in order to be a great WPF developer. I don't agree with that approach, personally....
Beware of the technical solution
Imagine this fictitious scenario, you are working on some functionality, and you are pretty pleased with what you have so far. Then, a new requirement come in, and it require something that is plain out not possible with the approach that you have taken so far. Then you start to investigate it a little further, and you find that if you do this, and push that, and maybe not look too closely at the end result, you can get what you want, without major changes to your code. I have done this. I...
A developer so retarded
I just run into this post, that talks about a presentation about mocking, and included this statement (about using Rhino Mocks):I once heard a story about a developer so retarded that he and his team spent a good amount of time trying to debug a mock only to find out that he forgot about ReplayAll()*. Who hires guys like that anyway? That developer, it would be me, and it can happens quite often. And I wrote the tool, so I should know what I am doing when I am using it. ...
Maintainability as a first level concern
I have no idea how I missed this post from Anders Norås. It is talking about some of the problems in using traditional software factories (code-gen) vs. using smarter frameworks. It is a long read, but it is excellent. Maintainability is the enabler of all the other "itities" of an architecture. Experience shows that typical EJB applications often was hard to maintain even if code generation made them easy to develop. The IDE driven code generation for EJB paved way for vendor lock-in. ...
"Read The Code" is not a valid answer
Scott Bellware has a post titled Just Read the Code (or, Let them Eat Cake)That open source software can be opened and read is a great quality of open source. But reading code and understanding code are two different things. Here is a simple example, from my own code With.Cache, it is a small class, but in order to understand what it is doing you have to grok Disposable Actions, Semi Statics and the Local Data concept. And then you need to find out where the properties on this class are used, to understand what...
A good definition of scripting languages
From Avi Bryant:You extend Java by writing Java, you extend C++ by writing C++, you extend C# by writing C#, but you extend TCL and Perl and Ruby, as often as not, by writing C.
Business Objects vs. Entities: What is the diff?
Recently I have noticed several discussions about the differences between entities and business objects. I have my own opinions in this matter, naturally, but before I get to them, let us try to define what each term mean, and I can't quite put this in words. An entity is something that has a valid domain meaning, and is usually persisted to durable storage. I am not so clear on the definition of a business objects, and I am not sure that my entity definition is very clear, either. Options?...
Give me the code!
Here is an amusing thought. I am explicitly a code guy (vs. designers guy). My persistance format is code My configuration is code My database is generated from code
Development stats
A single page. 34 integration tests. 432 lines of test code (excluding data setup and stuff move to test infrastructure). 66 lines of code behind 210 lines of Javascript 641 lines of in the ASPX file (include the javascript above) ...
More on change tracking...
Frans Bouma has an excellent post about Why change-tracking has to be part of an entity object. I like the level that he has gotten into, but I don't agree with his conclusion. The main issue is about a disconnected scenario, where you object went somewhere, and then came back, and you don't know what hapepned to it in the middle. Frans gives an excellent description about how it works in the infrastructure level using the ASP.Net data source, which I will continue to use. The main problem here is not...
Limitations of Declerative Coding
David Hayden is talking about AOP, and he mentioned Caching declaratively, which sturk a chord, and I had to write a post about it. I want to talk about a completely different aspect of declerative coding, how far you should go with it. Just to be clear, this is fake code, and it has little to do with David's post other than using the scenario to make a point. Let us take the code example that David has:public class NewsController { private INewsService _service; public NewsController(INewsService service) { _service = service; } [CachingCallHandler] public...
.Net Communit Process
Here are some interesting thoughts in the subject. And some very interesting comments and replies from ScottGu replying the open letter that David posted.
How not to go about "Building user interfaces for object-oriented systems"
Update: This article is fron 1999, which I somehow missed, not news by far. Still wrong, even for its time, though. This really annoys me, the author of the article tries to preach an OO methodology for writing UI. In general, I don't have an issue with that, but his arguments contains this:An object-oriented solution tries to encapsulate those things [adding a field to a screen] that are likely to change in such a way that a change to one part of the program won't impact the rest of the program at all. For example,...
Working with Code, not Executable XML
A couple of commenters on my last post said that XML is easier to work with tool-wise. This is true in a very strict sense. Code is incredibly expressive, and you can easily turn it into an object graph by simply executing it. I did something very similar a year ago, Saving object graphs to file: CodeGen and Executable Graphs.
How would I build MsBuild...
In the Open Letter to Scott Guthrie David talks about copying the features from OSS projects, and bring MsBuild as an example. MsBuild is a project that reached feature parity with Nant, there is basically no technical reason that I know to prefer on over the other. NAnt has more tasks, but MsBuild has quite a few as well. The problem is that feature parity is just that, not enough. Both Nant and MsBuild are xml based programming languages. Read the last sentence again, and you can probably tell what I don't like in...
WebForms and lies
I made the comment several times that I feel that WebForms lies to me, Joe Young asks what do I mean by that. Here is a small reproduction that I just created, that demonstrate the problem. Put this in an ASP.Net page, and run, select another value, and see what you get. <script runat="server"> protected void...
Reporting business logic
Here is an interesting problem. I didn't give it any though whatsoever yet, I am just presenting the problem. A bit about the model: This is a temporal model, meani