Web Design, Programming, Tutorials
ASP.NET MVC – Organizing your solution
Feb 4th
Keeping your project files organized will help you navigate through your projects and help you find what you are looking for to make changes faster.
Here are some tips that I use to help keep my ASP.NET MVC solutions organized:
- Use multiple projects to separate sections of the program.
- Use folders to group interfaces, classes, and factories that deal with an object.
- If objects can be used by other programs, put them in their own class library project.
Option 1
When beginning a new program, I usually start by creating an ASP.NET MVC solution with 4 projects:
- The main project – which contains the MVC components of the application such as Controllers, Views, CSS, and JavaScript files.
- The unit testing project – which contains all of the requirements for the application along with any fake repositories used for testing.
- The data project – which contains the repositories and domain objects used by the application.
- The services project – which contains the service objects that are the middle-man between the Controller and the Repository.
Option 2
Folders are used within the projects to further organize the code files within. The data project could have a domain object called Operation. Domain objects are placed in a folder with the same name as the object. Interfaces, Repositories, and Factories that deal with the object are also placed in the domain object folder.
The service project is organized similar to the data project.
The unit test project is slightly modified from the default to create a folder which contains all fake repository objects and another folder to contain requirement tests. Depending on the detail of unit testing you wish to utilize, you may have unit tests that verify application requirements and/or unit tests that further verify other parts your code.
In my opinion, you can write application requirements that detail exactly what the program should do and as long as you unit test these requirements, you don’t need to waste time trying to unit test 100% of your program. If a bug is found later, it will most likely be because it was not an application requirement. Then, add a new requirement, write a unit test, and make the application pass the new test.
Option 3
Where I work, we have various databases that we need to communicate with. We wanted a way to use these databases the same way every time without having to remember all of the different objects and names that needed to be used by each. So, I developed a parent wrapper class around the basic database functionality and then individual wrappers around each database object that inherited from the parent wrapper. Now, we can call up any of the database connections and work with them in exactly the same way, the only difference between them is the call to the factory when creating the instance.
These database wrappers were added to their own separate project so that in future applications, we could continue to reuse them.
Balloon rocket
Feb 3rd
Yesterday, I spent the day with my daughter at her preschool class helping out with the kids. She was excited to bring daddy to school.
The day had a space theme and the kids had fun learning about rockets and space shuttles. On of the activities the teacher had setup for them was a balloon rocket that the kids could blow up and shoot from one side of the room to the other.
Each kid also received a balloon rocket kit to take home. My daughter loved this. We had to go home and setup her rocket in the living room.
If you have a preschool aged child, they’ll love the balloon rocket.
Music Digital DNA Search for your PC
Feb 1st
In the past few years, we’ve had the ability to search for music by letting our cell phones scan the sound of a song and use its digital DNA to tell you the song title and direct you to where you can buy that song online.
Now you can do the same from your PC with Tunatic.
Tunatic uses your computer’s microphone to listen to the song and then checks their song database for a match. You can click a link that directs you to the Tunatic website where you are provided with links where you can buy the song.
Selecting the last item of a collection using Linq-to-sql
Jan 29th
Today I had some problems with Linq-To-SQL. I needed to get the last item from a collection and only return items where the date was older than two weeks from now.
Data Tables
The main items that I want to return from Linq are Revision records. A Revision has a collection of Document_History records that keep track of updates to the revision. The Document_History record has a Date field that is the date and time when the history entry was inserted, or when the revision was modified.
The Problem
When using the data objects that Linq generates, you are allowed to perform some functions, such as Last and Reverse on the collection of items.
Revision.Document_History.Last.Date
The above code would return the most recent date, or the last time a revision was updated.
But, when you try to use the Last method within a Linq statement, Visual Studio throws an error.
The Fix
To get around this error, I figured out that you can select the latest date using a sub query and the Max method. Below is an example of the Linq-To-SQL code that worked for me.
Dim revs As List(Of Revision) = From r In dbcontext.Revisions _
Where (From h In r.Document_History _
Select h.Date).Max <= cutoffDate _
Select r
Configuring IIS 6 and ASP.NET MVC
Jan 28th
By default, IIS 6 does not work with ASP.NET MVC and needs to be configured to use wild-card mapping to get MVC’s routing and clean URLs to work correctly. Unfortunately, IIS 6 does take a performance hit because all requests are processed by ASP.NET. Static files, such as images, CSS, and JavaScript are processed as a dynamic page instead of a static one.
Install ASP.NET MVC
Follow the first two steps of the instructions for installing ASP.NET MVC. The third step can be ignored since Visual Studio will not be installed on the server.
Configuring ASP.NET 2.0
First thing to do is open the IIS Manager ->expand your server ->Web Service Extensions folder. Right-click on the white space under the list of Web Service Extensions and select to Add a new Web service extension…
The New Web Service Extension window will pop open. Enter “ASP.NET v2.0.50727″ as the Extension Name and click the Add button. Browse to the C:\Windows\Microsoft.Net\Framework\v2.0.50727 folder and select the aspnet_isapi.dll file. Make sure to check the “Set extension status is Allowed” button before clicking OK.
This will allow your programs to run ASP.NET 2.0, 3.0, and 3.5 versions of the .NET framework.
Configuring Your Web Application
The next thing to do is open the properties of the Web Site where your web application is. Select the ASP.NET tab and select 2.0.50717 in the ASP.NET version drop down box.
Next, click on the Home Directory tab. Click on the Configuration button. Under the Wild-card application maps area, click the Insert button. Browse to the C:\Windows\Microsoft.Net\Framework\v2.0.50727 folder and select the aspnet_isapi.dll file. Uncheck the “Verify that file exists” before clicking OK. Save your changes and your site should now be able to run ASP.NET MVC and use the routes setup by your application.
Dependency Injection
Jan 27th
Dependency Injection is a design pattern that is used in Object Oriented Programming to create a relationship between two objects without object A depending on object B. Instead, through the use of an Interface, a place-holder for object B can be referenced in object A. Then, object B can be passed into object A where it is utilized. If there is an object C that uses the same interface as object B, it may also be passed into object A.
A Tightly Coupled Relationship
Below is an example of two classes that are tightly coupled, meaning that if we want to replace object B with another class, we must change the code of object A. Object A is dependent on object B to execute.
Public Class ObjectA
Private _obj As ObjectB
Public Sub New(ByVal obj As ObjectB)
_obj = obj
End Sub
Public Sub RunB()
_obj.Run()
End Sub
End Class
Public Class ObjectB
Public Sub Run()
Console.WriteLine("Run from objectB")
End Sub
End Class
A Loosely Coupled Relationship
Now, let’s look at a better approach that will allow us to replace ObjectB with another class using the same interface or replace it with a child class of ObjectB.
Below, the ObjectB class now implements the IObjectB interface and the references to ObjectB in ObjectA have been replaced with a reference to the interface IObjectB instead.
Public Interface IObjectB Sub Run() End Interface
Public Class ObjectA
Private _obj As IObjectB
Public Sub New(ByVal obj As IObjectB)
_obj = obj
End Sub
Public Sub RunB()
_obj.Run()
End Sub
End Class
Public Class ObjectB
Implements IObjectB
Public Sub Run() Implements IObjectB.Run
Console.WriteLine("Run from objectB")
End Sub
End Class
Now, ObjectA can execute no matter if we pass into it a copy of ObjectB or another class that implements the IObjectB interface. The Interface ensures that all objects that implement it will have a subroutine called Run. That is all that ObjectA needs to know. This breaks ObjectA’s dependency on ObjectB.
The Repository Pattern
Jan 26th
The repository pattern is a design pattern that creates repositories of data that talk to a data source. These repositories are used to segregate the code that talks to a data source from the rest of your program. For example, your code that communicates to a database connection is stored within a repository class. Communication between your program and the database is all funnelled through the repository class.
In Domain Driven Design, you create data objects that are logical representations of the data stored within a data source; such as a database. These data objects may not mimic the design of a database, but instead be a more logical grouping of properties and methods. The repository pattern can be used to create a repository class for each data object, where that object can then be created, read, updated, or deleted (CRUD) from the data source.
Using the repository pattern with Dependency Injection allows for the repository class to be mocked during testing, where a more controlled and faster repository can be created. Also, if the data source changes to a XML file or another database, the repository class can be replaced.
The Repository pattern is one of the design patterns that I use to build a more maintainable and flexible application.
Vector class for Lotus Script
Jan 25th
The following is a script library written in Lotus Script to allow programmers to use a vector in their Lotus Script code.
Option Declare Option Base 0 Public Class Vector 'Private data members Private vArray As Variant Private vCapacity As Integer Private vSize As Integer Public Sub New() 'Initialize variables Call SetCapacity(0) Call SetSize(0) Call Reserve(10) End Sub Public Sub Delete() Call EraseAll() 'Clear the array Set vArray = Nothing End Sub Private Sub SetCapacity(newCapacity As Integer) vCapacity = newCapacity End Sub Public Function Capacity() As Integer Capacity = vCapacity End Function Private Sub SetSize(newSize As Integer) vSize = newSize End Sub Public Function Size() As Integer Size = vSize End Function Public Function Empty() As Boolean 'Declare variables Dim valid As Boolean 'Get the size If (Size() = 0) Then valid = True Else valid = False End If 'Return if the vector is empty Empty = valid End Function Public Sub Reserve(minCapacity As Integer) 'Declare variables Dim openElements As Integer Dim newCapacity As Integer 'Initialize variables openElements = Capacity() - Size() newCapacity = Capacity() 'Check if there are any open slots remaining If (openElements = 0) Then newCapacity = (Capacity() * 1.5) End If 'Make sure that the minimum capacity is stored If (newCapacity < minCapacity) Then newCapacity = minCapacity End If 'Check if a change to the capacity is being made If (Not(Capacity() = newCapacity)) Then 'Check the size to determine if the array's contents need to be 'saved while redimensioning the array If (Size() = 0) Then 'Nothing stored yet, perform redim Redim vArray(newCapacity) Else 'Keep the array's contents while extending its boundaries Redim Preserve array(newCapacity) End If 'Update the capacity Call SetCapacity(newCapacity) End If End Sub Public Sub PushBack(newItem As Variant) 'Check if the passed item/items is actually an array or a list If ((Not(Isarray(newItem))) And (Not(Islist(newItem)))) Then 'Add the single item Call AddItem(newItem) Else 'Add multiple items Forall item In newItem Call AddItem(item) End Forall End If End Sub Private Sub AddItem(newItem As Variant) 'Declare variables Dim newSize As Integer 'Initialize variables newSize = Size() + 1 'Make sure there is enough capacity in the vector Call Reserve(newSize) 'Insert the item into the vector If (Isobject(newItem)) Then Set vArray(Size()) = newItem Else vArray(Size()) = newItem End If 'Increment the vector size Call SetSize(newSize) End Sub Public Function At(index As Integer) As Variant 'Check the vector's index boundaries If ((index < 0) Or (index => Size())) Then Error 2000, _ "[Vector Class]:(Function: At): Index out of bounds." 'Check if the item is an object If (Isobject(vArray(index))) Then Set At = vArray(index) Else At = vArray(index) End If End Function Public Sub Erase(index As Integer) 'Check if the index is out of bounds If ((index < 0) Or (index => Size())) Then Error 2000, _ "[Vector Class]:(Function: Erase): Index out of bounds." 'Declare variables Dim newArray As Variant Dim x As Integer Dim y As Integer 'Initialize variables y = 0 'Make a new array with the same capacity Redim newArray(Capacity()) 'Loop through each element of the vector For x = 0 To Size() - 1 'Check if the current index is the one to remove If ((x <> index) And (Not(y > Size()))) Then 'Check if the item is an object If (Isobject(vArray(x))) Then Set newArray(y) = vArray(x) Else newArray(y) = vArray(x) End If 'Increment the index counter y = y + 1 End If Next x 'Subtract one from the vector size Call SetSize(Size() - 1) 'Set the new array as the vector vArray = newArray End Sub Public Function BeginIndex() As Integer BeginIndex = 0 End Function Public Function EndIndex() As Integer EndIndex = Size() - 1 End Function Public Sub EraseAll() 'Declare variables Dim x As Integer 'Loop through each element of the array For x = BeginIndex() To EndIndex() 'Delete each element of the arra 'Check if the item is an object If (Isobject(vArray(x))) Then Set vArray(x) = Nothing End If Next 'Reset the size. Call SetSize(0) End Sub End Class
Copy the above code and paste into a script library for the database where you would like to use a vector. Then, you can use the example below to use the vector class.
Use "libVector" Dim vector As New Vector() dim obj As String obj = "hello" 'Load any object into the vector. Call vector.PushBack(obj) Call vector.PushBack(obj) 'Loop through the vector to retrieve objects. For i = vector.BeginIndex() To vector.EndIndex() Print "Item: " & i & " " & vector.At(i) Next
Adding social media icons to the Mystique theme
Jan 22nd
If you are using Wordpress and the Mystique theme, such as this site, then the following instructions will help you with adding additional social icons above the menu bar.
Creating the images
The number of image files you need to create depends on the number of icons you wish to have. You can combine three icons per image file and use CSS to pull the desired icon from the file to display.
For example, below are the two image files used by this site. Since I only have six icons, I can make them all fit into two files. This is important because the browser only has to download two images to display these six icons. Each HTTP request back to the server can slow down a website. Using CSS to display the images also helps by allowing the browser to cache the images and reduce text in the HTML file.
These images were created in Photoshop using a transparent background. You can download various free social media icons. Search the internet and find some that you like that are 64 x 64 pixels. To create the tilted look, rotate the icon 30° and then resize so the height is 64 pixels. The total width of the image should be 64 x the number of icons, but remember to only store three per image file.
Changes to the CSS
Inside the Mystique theme, open the style.css file. Around 100 lines down into the file, you will see some code similar this:
#header .nav-extra
{width:64px;height:36px;display:block;position:absolute;bottom:18px;z-index:10;}
#header .nav-extra span
{display:none;}
#header .nav-extra.rss
{background:transparent url(images/nav-icons.png) no-repeat right top;right:20px;}
#header a.twitter
{background:transparent url(images/nav-icons.png) no-repeat left top;right:85px;}
Just below this block of CSS is where we will add our additions, which will be very similar to the last line of CSS implementing the twitter class on the “a” tag.
Add the following CSS to use the same image files and icons that I have used. You can change the name following the “a.” to anything you wish, but it should be something descriptive.
#header a.facebook
{background:transparent url(images/nav-icons.png) no-repeat center top;right:147px;}
#header a.myspace
{background:transparent url(images/nav-icons2.png) no-repeat right top;right:209px;}
#header a.flickr
{background:transparent url(images/nav-icons2.png) no-repeat center top;right:273px;}
#header a.linkedin
{background:transparent url(images/nav-icons2.png) no-repeat left top;right:338px;}
Here I have added additional icons for Facebook, MySpace, Flickr, and LinkedIn.
To reference the correct icon, using the background: CSS property, you can call the image file (nav-icons.png and nav-icons2.png). Using the top right, left and center keywords, you can select the icon file to use from the image. In the above example, the nav-icons.png file has three icons…by selecting the center one, I’m pulling out the Facebook icon from the first image above.
The “right” property positions the icon however many pixels from the right side of the screen. You can adjust this number to increase or reduce the amount of space between the icons.
Changes to the header
The last thing we need to do is add the HTML code to the header template. This will tell your browser to display the icons. Open the header.php file within the mystique theme, then look for the following block of code around line 55.
if ($twituser): ?>
<a href="http://www.twitter.com/<?php echo $twituser; ?>" class="nav-extra twitter"
title="<?php _e("Follow me on Twitter!","mystique"); ?>">
<span><?php _e("Follow me on Twitter!","mystique"); ?></span>
</a>
<?php endif; ?>
Next, we’ll add in our new icon HTML below the HTML shown above.
<a href="http://www.facebook.com/YOURPROFILE" class="nav-extra facebook"
title="<?php _e("My Facebook Profile.", "mystique"); ?>">
<span><?php _e("My Facebook Profile.", "mystique"); ?></span>
</a>
<a href="http://www.myspace.com/YOURPROFILE" class="nav-extra myspace"
title="<?php _e("My MySpace Profile.", "mystique"); ?>">
<span><?php _e("My MySpace Profile.", "mystique"); ?></span>
</a>
<a href="http://www.flickr.com/photos/YOURPROFILE/" class="nav-extra flickr"
title="<?php _e("My Flickr Photos.", "mystique"); ?>">
<span><?php _e("My Flickr Photos.", "mystique"); ?></span>
</a>
<a href="http://www.linkedin.com/in/YOURPROFILE" class="nav-extra linkedin"
title="<?php _e("My LinkedIn Profile.", "mystique"); ?>">
<span><?php _e("My LinkedIn Profile.", "mystique"); ?></span>
</a>
Make sure that you replace YOURPROFILE with the correct link information to your profile for these social media sites.
One thing that is important to notice is the CLASS attribute. There are two classes added to each “a” tag: nav-extra and the name of whatever social media icon you are displaying. This name must match the name specified in the CSS above that follows the “a.”.
The PS3 Media Server
Jan 21st
If you have a Playstation 3, or even an Xbox 360, you should take a look at the PS3 Media Server. I have tried using Windows Media Center to serve video content, and while it does work, the video formats seem very limited. The PS3 Media Server seems to support just about any type of video that I would want to use; such as MPG, MP4, MKV, M2TS, and VOB.
I was most impressed by how backing up a movie DVD at 100% quality (around 7 GB), I was still able to stream to my PS3 and the video quality looked as if the DVD were playing in the PS3. In testing, I was able to play Blu-Ray video as well, although the video did jump around a bit on some clips; it could have been a bad backup. Both my computer and the PS3 were wired on a 1GB network.
There are some problems:
- Seeking does not work the best, specifically from the Xbox 360.
- DVDs are typically split into roughly 1GB VOB files. If there are many of these, it may be difficult to find the desired VOB that contains the movie. Usually, the movie is the largest one. You can use the command below to combine the VOBs into one file…which then can be renamed to something.mpg.
copy /b file1.vob + file2.vob + file3.vob new.vob
So, if you would like to keep your DVD collection in good condition and have the space to store your movies on your computer, give the PS3 Media Server a try. You can backup your DVDs to your computer using a tool like DVDShrink, then play them over your network. No more searching for the disc to watch a movie.
Disclaimer: You should only backup media that you own. Do not illegally copy movies or music.
You can download the PS3 Media Server from google.