Wednesday, October 29, 2008

Cool if statement.

I ran into this little if statement while fixing moving some code around today. It does something like 18 string functions and that is assuming that Path.GetExtension is just one string function call.

   30 if (Path.GetExtension(path).ToLower() == ".tif"
   31     || Path.GetExtension(path).ToLower() == ".pdf"
   32     || Path.GetExtension(path).ToLower() == ".jpg"
   33     || Path.GetExtension(path).ToLower() == ".png"
   34     || Path.GetExtension(path).ToLower() == ".gif"
   35     || Path.GetExtension(path).ToLower() == ".jpeg")

I decided that it needed to be removed and I replaced it with this.

   25 string ext = Path.GetExtension(path).ToLower();            
   26 List<string> validExt = new List<string> { ".tif", ".pdf", ".jpg", ".png", ".gif", ".jpeg" };
   27 if(validExt.Contains(ext)) 

But you could also do  this with LINQ

   25 var extensions = new List<string> { ".jpg", ".jpeg", ".tif", ".png", ".gif" };
   26 if ((from i in extensions where i == Path.GetExtension(dir).ToLower() select i).Count() > 0)

I like the second version the best but that is just personal preference.  Both alternatives are way better then the first option.

Tuesday, October 28, 2008

Fun with directory security

So for the last couple weeks we have been having a problem with our installer and it’s setting of access rights on directories. This was mostly a problem with vista and well I got made the “vista access rights expert” so that translates into you get to figure out what is wrong.

   15 static void Main(string[] args)
   16 {
   17     string aPath = @"C:\ProgramData\TestApp";
   18     string fPath = Path.Combine(aPath, "files");
   19     Directory.CreateDirectory(fPath);
   20     SetPermissions(aPath);            
   21 }
   22 public static void SetPermissions(string dir)
   23 {
   24     DirectoryInfo info = new DirectoryInfo(dir);
   25     DirectorySecurity ds = info.GetAccessControl();            
   26     ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users", 
   27                      FileSystemRights.FullControl,
   28                      InheritanceFlags.ContainerInherit,                            
   29                      PropagationFlags.None,
   30                      AccessControlType.Allow));
   31     info.SetAccessControl(ds);            
   32 }

The code started out looking something like this. It adds a security rule for all users to the folder allowing use users access to data on the local system. Problem is that if a program tries to access the files it throws an access denied error. Even though the user’s effective permission to the folder is FullControl. He doesn’t have any access to the files in the folder, and can’t access the files dir at all. After several hours of reading and trying different things all the smart people I drug into help me with the problem came up with three small changes that make it all work without a problem.

   15 static void Main(string[] args)
   16 {
   17     string aPath = @"C:\ProgramData\TestApp";
   18     string fPath = Path.Combine(aPath, "files");
   19     Directory.CreateDirectory(aPath);
   20     SetPermissions(aPath);
   21     Directory.CreateDirectory(fPath);
   22 }
   23 public static void SetPermissions(string dir)
   24 {
   25     DirectoryInfo info = new DirectoryInfo(dir);
   26     DirectorySecurity ds = info.GetAccessControl();
   27     ds.AddAccessRule(new FileSystemAccessRule(@"BUILTIN\Users",
   28                      FileSystemRights.FullControl,
   29                      InheritanceFlags.ObjectInherit |
   30                      InheritanceFlags.ContainerInherit,
   31                      PropagationFlags.None,
   32                      AccessControlType.Allow));
   33     info.SetAccessControl(ds);
   34 }

The changes that needed to be made wore creating the base directory and setting permissions before the sub folder was added. In the set permissions folder there was also a second flag added to the inheritance option of FileSystemAccessRule that changed it so that both files and folders inherited the settings.

Monday, October 6, 2008

The DAO Class

In the project I am currently working on we have lots of objects and each object has it's own DAO class.  This is fine every class should have a DAO object.  Or I should say that there should be a DAO object that every class can use to access it's data from it's storage location. The thing is that most systems I have worked with don't do it this way.  They have a DAO object that creates objects.  Not objects that use the DAO system to create them selves.

I will give you an example.  Say you have a class called foo and you want to get an instance of it from the database. With the DAO as create system you have to do the following.

   20 foo bar = fooDAO.GetFooByID(fooID);

bar gets created in the fooDAO object so basically any place where you want to work with foo you have to have fooDAO around and visible in scope as well.  No in the way I like to see things like this done a foo object has a reference to the fooDAO in his class so when you are creating the foo object you just say:

   19 foo bar = new foo(fooID);

So some people will say you haven't accomplished all that much by pushing it down into the class.  Which is true you still have the same amount of code, but when the guy that writes the business logic is working with foo now he doesn't have to know about fooDAO or care about it. He gets to make calls like.

   22 bar.Save();

rather then calls like this.

   23 fooDAO.UpdateFoo(bar);

Then when the day comes where it is time to stop using the cheep read free database you decided to use and switch over to a more powerful database. All of your updates can be done one place for each class type, and if you designed it right and use an well defined interface for your DAO object you will be able just to change the DAO object reference in each class.

Wednesday, October 1, 2008

Best varible name ever.

So today I was looking at a coworker’s code. I am doing CURD processes for the system I am working on this isn’t creative code or even variable code beyond you change the names of what you are keeping in the database. But while grabbing his code to use as a template for the new function I ran across this little pearl of code. My work went from boring to enjoyable in second all because of one line of code that made me laugh. Her it is.

  716 object of_my_affection = command.ExecuteScalar();

Sometimes having a bit of fun in your code makes it better than all the naming conventions you could ever use.