Coding for Patterns

With the deployment of the gold release of ASP.NET MVC, the topic of patterns are hot and heavy again, and I am getting reader emails about “What pattern do I use for this or that”.  I answer when I can, but it needs to be understood that The Gang Of Four were writing for a pretty broad kind of software development, and a lot of their thoughts only peripherally apply to the world of business programming.

Most of us write programs that accept data from some source – usually a user – and then deliver it to some source – usually a report.  This is not what The GoF had in mind when they wrote Design Patterns.  In fact, you’ll notice that even Model View Controller – the pattern behind the ASP.NET MVC – is Martin Fowler’s; it isn’t even in Design Patterns.

Speaking of Fowler, he is a great author to check out when making decisions about a pattern for the data-driven applications usually built by VB.NET programmers.  I strongly recommend his Patterns of Enterprise Application Architecture.  The Object-relational structural patterns, especially, define a lot of the thought that needs to go into object-oriented applications persisting to a relational database.

What patterns do I use?  I believe in single abstraction of the domain.  I think that you need significant separation between the database and the domain and application layer, but to wrap ever single little thing in interfaces is a waste of time for 80% of stovepipe applications.  In general, using Fowler’s terminology, I use a lazy mapper model.  The domain objects (like person or organization) call data objects that relate directly to tables in order to gather or persist information.  If it is a really big application, I will go ahead and go to that next level of abstraction and implement that separated interface that I generally do not like.

A final note – design patterns are not ‘philosophical constructs of programming.’  They are real, important, significant and weighty contributors to software design.  If you are not designing your software, you should be and it is tough if not impossible to design without an approach and the approach is defined by the pattern.  Please don’t allow the hackers on a team to direct the flow away from a sensible approach.

Downloading a file from a remote server with network credentials

Man, was this a pain in the butt.  Seems like downloading a file from a remote PC would be easy, but 'taint.  This worked great for me,  your mileage may vary.

   1:  'First we set up all of the filenames and locations, making heavy use of app.config.
   2:  Dim filename As String = String.Format("{0}-{1}-{2}.txt", CDate(checkDate).Year.ToString, CDate(checkDate).Month.ToString, CDate(checkDate).Day.ToString + 1)
   3:  Dim RemoteFile As String = String.Format("\\{0}\{1}\{2}", My.Settings.IpNumber, My.Settings.LogDirectory, filename)
   4:  Dim LocalDirectory As String = String.Format("{0}logs\", My.Settings.ReportLocation)
   5:  Dim LocalFile As String = String.Format("{0}{1}", LocalDirectory, filename)
   6:  'Hey, check for the localdirectory
   7:  If Not Directory.Exists(LocalDirectory) Then
   8:    Directory.CreateDirectory(LocalDirectory)
   9:  End If
  10:  'Then we set up a little security
  11:  Dim myClient As New System.Net.WebClient
  12:  Dim myCreds As New System.Net.NetworkCredential(My.Settings.Username, My.Settings.Password)
  13:  'Log the fact that we are starting.
  14:  My.Application.Log.WriteEntry(String.Format("Starting download of file {0}", fileName))
  15:  'Set up the securit
  16:  myClient.Credentials = myCreds
  17:  'Now we actually try and get the file.
  18:  Try
  19:    myClient.DownloadFile(RemoteFile, LocalFile)
  20:  Catch uae As System.UnauthorizedAccessException
  21:    My.Computer.FileSystem.WriteAllText(LocalFile, String.Format("The file path to file {0} was not available at the time of this posting.", file), True)
  22:  Catch ex As Exception
  23:    My.Application.Log.WriteException(ex)
  24:  End Try
  25:  My.Application.Log.WriteEntry(String.Format("Finished download of file {0}", fileName)) 
  26:   

 

Did this as part of a project at Wendy's.  Part of the Classic Posts collection.  (heh)

Who the heck is Bill Sempf?

Bill Sempf

Husband. Father. Pentester. Secure software composer. Brewer. Lockpicker. Ninja. Insurrectionist. Lumberjack. All words that have been used to describe me recently. I help people write more secure software.

Paying the bills

 

profile for Bill Sempf on Stack Exchange, a network of free, community-driven Q&A sites

INETA Community Speakers Program

EFF Member 2013

Month List

I spoke at

I'm speaking at Black Hat Europe