DCSIMG
September 2009 - Posts - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

September 2009 - Posts

Do We Need Design Patterns?

Do We Need Design Patterns?

In the previous week I have been asked “do we really need design patterns
when we write code?”. Since one of the first things that I wrote in my blog
was a design patterns series you probably would think that I answered SURE!
or other absolute answer.
That isn’t the case.
The question was raised since I saw that a developer tried to enforce a
design pattern (abstract factory) that wasn’t needed in some place in the
code. What I really believe in is that design patterns are guidelines
of how to build better object oriented design but…
sometime it is being abused like every other aspect in programming.
You don’t need to enforce a pattern in code if what you only need is very
simple implementation.
Yes, reusability, scalability and other phrases that are connected to design
patterns
are very important but so does simplicity or other
object oriented design principals like S.O.L.I.D.

In conclusion, we need design patterns but there are other things to consider
when you develop an application. To write it in other words –
When the only tool you have is a hammer, it is tempting to treat everything as
if it were a nail
” - Abraham Maslow.

DotNetKicks Image

Quick Tip – Adding Business Validation Messages to ValidationSummary

Quick Tip – Adding Business Validation Messages to ValidationSummary

Two days ago I was askedAdding Business Validations to ValidationSummary
how to insert business
validation messages into a
ValidationSummary control.
The post is the answer I
gave to the person who asked
me.

The Problem

We have a web form that is doing input validation and output the errors
inside a ValidationSummary control. Since ValidationSummary shows a
summary of all validation errors from ASP.NET validators how can we
add business validations which are part of the business logic we have to
the output of the ValidationSummary ?
Also, how can we make the input not valid according to the business logic?

The Answer

We can use the CustomValidator control to our rescue. In code we will
create the CustomValidator and sets its IsValid property to false. We
also add our business error message to the ErrorMessage property.
Then we will add the created CustomValidator to the Page’s Validator
list. The following helper method will perform what I describe:

public static void AddBusinessErrorMessage(Page page, string message)
{
    var validator = new CustomValidator();
    validator.IsValid = false;
    validator.ErrorMessage = message;
    page.Validators.Add(validator);
}

Summary

In the post I showed an example of using a CustomValidator in order
to add business error messages to a ValidationSummary control.
There are more solutions out there that will do the same thing but this
solution is the easiest to implement.

DotNetKicks Image

Management From Hell

Management From Hell

This post is a result of a conversation I had with my wife.
My wife is also in the Hi-Tech world and she is a senior Java developer (yes
computers runs in the family ;-)). Last week we talked about what is
going on in the project that she is assigned to and I couldn’t believe what
I’m hearing.  Apparently, the managers there just lost their mind…
Sentences like:

  • “I don’t want/care about quality code I want to see that you work a
    lot of hours”
  • “Do you have a family? kids? if not you don’t have excuses not to work
    above 11 hours a day”
  • “Developers that don’t do more then 11 hours a day will be transferred
    to the QA team” – until now three developers were “punished” and felt
    the wrath of the managers
  • “You must do at least 11 hours a day!” (in Israel the average working
    hours is 9 hours a day)
  • And many many more “good” sentences like that.

Since the project’s status is on schedule then it is very foolish of managers
to act like that. Instead of encouraging the workers to do their job, these
managers are ”throwing very big stones” at their workers (even though it
isn’t necessary). From what my wife tells me the workers are very embittered.
Some of them are looking for new jobs. Others just turned to be very apathetic
in regard to their job or developed antagonism for their job.

What do you think about this situation?
What will you do if you were in my wife’s place?
Would you eat that cake?

DotNetKicks Image

If it is Working Don’t Touch it?

If it is Working Don’t Touch it?

Occasionally I hear this sentence from here and there.
Every time I hear it I get angry again. IMHO, when someone says that sentence
it’s probably indicating that there is a lot of pain coming at us in the future.
When I always hear that sentence?

  • When there is a spaghetti code that nobody knows how to deal with (because
    the code is so tangled).
  • When the developer is lazy and don’t want to change something that is
    working even though it is badly written.
  • When there are no unit tests that will imply that changes you made broke
    the application.
  • When managers/developers want fast delivery and don’t care about code quality.
  • When no one knows what is going on in some piece of code (or don’t care).
  • And more. You name it.

Even though it is probably implies that I’m always touching the code and change it,
this is not the case. What I want to say is that code changes during the development
and maintenance cycles. You need to refactor smelling code even though it’s working and
not keep bad code. If you have poor design or spaghetti code it will come at you in the
future and then to refactor the code will be horror. So if you see something that isn’t
right it’s your responsibility to fix it or at least to report about it.
For example, last week I showed a piece of code that I wrote to other colleague.
During our conversation I saw a method that was bad written and fixed it.
Since I had unit tests for my code I could see that my changes didn’t break the application
and now it’s running faster.
So the lesson here is – if it is working and smells touch it!

DotNetKicks Image

Quick Tip – Using the ShouldSerializeXXX methods

Quick Tip – Using the ShouldSerializeXXX methods

Something that I Quick Tip – Using the ShouldSerializeXXX methods
encountered last week.
The ShouldSerialize methods
are optional methods that
you can provide for
a class property.
These methods are built as ShouldSerializePropertyName
and inside of them you can provide a check that will determine whether the
property should be serialized or not. Of course this can be achieved only in
serializable classes.
An example of use:

public string Text { get; set; }
 
public bool ShouldSerializeText()
{
    return string.IsNullOrEmpty(Text);
}

Very helpful if I don’t what to serialize data that is not available.
For further reading - ShouldSerialize and Reset Methods.
Enjoy!

DotNetKicks Image

Back to Basics – Calculating MD5 Hashing

Back to Basics – Calculating MD5 Hashing

Yesterday I needed toBack to Basics – Calculating MD5 Hashing
use a hashing algorithm
for a given task.
I chose to use the MD5 
hashing algorithm and
in this post I’ll show how
to calculate MD5 hash from a given string.

What is MD5 Hashing Algorithm?

MD5 is a very widely used hashing function that commonly used to
check the integrity of files or strings. When using MD5 There is a
small possibility of getting two identical hashes to two different strings.
It is very useful for storing passwords in databases.

Calculating MD5 Hash from a String

MD5 hash function is provided in the BCL of .NET inside
System.Security.Cryptography namespace.
The following code shows how to use the MD5 function to hash a given
string:

public string CalculateMD5Hash(string input)
{
    var md5Function = MD5.Create();
    var inputBytes = Encoding.ASCII.GetBytes(input);
    var hash = md5Function.ComputeHash(inputBytes);
 
    var sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}

The code will creates MD5 hash algorithm class which is used to compute
the hash for the input string. After I’m computing the hash, I convert the 
128 bit value to Hexadecimal upper case representation. If you want to use
lower case just replace the appending of the StringBuilder to the following
line of code:

sb.Append(hash[i].ToString("x2"));

After running this method on a given "Some String" string the result will be:
83BEB8C4FA4596C8F7B565D390F494E2

Summary

Computing hash using the MD5 hash algorithm class is very simple and straight 
forward. In the post I showed how to do that using the BCL MD5 class.

DotNetKicks Image