Archive for February, 2010

Using the Linq IQueryable Toolkit

The IQToolkit provides you with a framework for building your own LINQ providers. The toolkit comes with providers for Microsoft SQL, Microsoft SQL CE, Access, MySQL, and SQLite.

This post will show you how to use the toolkit to talk to Microsoft SQL. Later, I’ll be attempting to write my own providers for DB2 and Interbase 7.1.

Download the toolkit
First thing to do is download the IQToolkit (version 0.16a as of this posting).

Reference the toolkit
I setup a console project to test the toolkit. Within your project, create a reference to the IQToolkit.Data.dll and the IQToolkit.dll. Then, add a reference to the dll for the provider you wish to use. In this case, we are using the IQToolkit.Data.SqlClient.dll for Microsoft SQL.

Table objects
Next, you will need to create a class object for each table you need to work with in the database. I have a table that has two fields: PLCID and SummaryProdMode. Create a class object to hold these properties and then use the Column attribute to bind the property to the table column.

Imports IQToolkit.Data.Mapping

Public Class Machine

    Private _id As Integer
    Private _mode As Boolean

    <Column(IsPrimaryKey:=True)> _
    Public Property PLCID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    <Column()> _
    Public Property SummaryProdMode() As Boolean
        Get
            Return _mode
        End Get
        Set(ByVal value As Boolean)
            _mode = value
        End Set
    End Property

End Class

DataContext
The data context contains read only properties for each database table being used. In this case, I’m dealing with the DCM_Machines table.

Imports IQToolkit
Imports IQToolkit.Data.Mapping
Imports System.Linq

Public Class MachineDataContext

    Private _provider As IEntityProvider

    Public Property EntityProvider() As IEntityProvider
        Get
            Return _provider
        End Get
        Set(ByVal value As IEntityProvider)
            _provider = value
        End Set
    End Property

    <Table()> _
    Public ReadOnly Property Machines() As IEntityTable(Of Machine)
        Get
            Return EntityProvider.GetTable(Of Machine)("DCM_Machines")
        End Get
    End Property

    Public Sub New(ByVal provider As IEntityProvider)

        _provider = provider

    End Sub

End Class

Using the provider
Use the following code to setup and use the provider with LINQ.

Imports IQToolkit.Data

Module Module1

    Sub Main()

        Dim provider = DbEntityProvider.From( _
            "IQToolkit.Data.SqlClient", _
            "YOUR_CONNECTION_STRING", _
            "Machine")
        Dim db = New MachineDataContext(provider)

        Dim machs = (From m In db.Machines _
                    Select m).ToList()

        For Each m In machs

            Console.WriteLine("id=" & m.PLCID)

        Next

        Console.ReadLine()

    End Sub

End Module

ASP.NET MVC – Organizing your solution

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:

  1. Use multiple projects to separate sections of the program.
  2. Use folders to group interfaces, classes, and factories that deal with an object.
  3. 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

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

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.