Home > English > Gofer – Console

Gofer – Console

By now, I am sure that you are tired of reading and just want to play with whatever I have been talking about. Well, that is exactly what we are doing to do.

First, start by creating a new Console Application. Next we are going to access my libraries using NuGet. Right-click on your References folder and select Manage NuGet Packages. Next, type in “Gofer” as your search criteria. Select Gofer.Sample as your choice. This package comes with the Gofer library as well as with some helper files to make testing this easier.

Gofer.Sample has a dependency on SwitchBlade and ValueInjecter.

SwitchBlade is another package that I wrote that allows you to host Razor templates outside of ASP.NET and IIS. I will be covering SwitchBlade in a future post.

ValueInjecter is a package like AutoMapper but much more convention-based and easier.

You will also notice that you have two new template folders for your CRUD and DDL operations. You can modify these templates to shape how you want your SQL code to look when it is used by Gofer.

You will also notice two new files:

Domain.cs – This file represents a sample domain model. It is very similar to what you would see from a Northwind with some slight modifications.

TestDriver.cs – This file is a test driver class that allows us to test Gofer.

In your Program.cs file, add the following code snippet to your Main method:

TestDriver td = new TestDriver();
td.Run();

Here is what the TestDriver class looks like:

public class TestDriver
{
    public void Run()
    {
        SchemaRules rules = new SchemaRules();
        rules.AssemblyOf<Customers>()
            .ShouldMap(x => x.Namespace == "Domain")
            .GetSchema();

        rules.PerformMigration = true;
        rules.ForceNewMigration = true;

        var repo = new Repository<Customers>(rules);
        var cust = new Customers() {
                CompanyName = "Bubba's Repair",
                ContactName = "Billy Bob",
                ContactTitle = "Owner",
                Address = "100 Pecan Street",
                City = "Columbia",
                PostalCode = "29661",
                Country = "USA",
                Phone = "(803) 836-1212",
                Fax = "(803) 836-1213"
        };
        var id = repo.Insert(cust);
        var ds = repo.Get().ToList();

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
};

As you can see, we are using two main classes from Gofer: SchemaRules and Repository.

SchemaRules – This tells the Gofer engine what conventions to use for its data access. There is a ton that you can override with this class and we will take a look at that in a later post but this is the bare minimum that you need to get going. Also, you will see two properties that tell the engine whether or not to perform a migration as well as force the migration, meaning that it will drop the database and recreate it if necessary.

Repository – This is our data access class that facilitates getting data from the Gofer engine.

There is one last change that you need to put in place before you continue. The package will have also provided you with an App.config that you will need to complete. The following is a sample App.config that you can pattern against for yourself:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="DB_NAME" value="Example" />

    <add key="DDL_ConnectionString" value="Provider=SQLOLEDB;Server=(local);Database=master;Integrated Security=SSPI;" />
    <add key="DDL_DatabaseType" value="4" />

    <add key="ConnectionString" value="Data Source=(local);Initial Catalog=Example;Integrated Security=SSPI;" />
    <add key="DatabaseType" value="3" />

    <add key="TemplatePath" value="C:\Users\Matt\Documents\Visual Studio 2010\Projects\GoferConsole\GoferConsole\CRUD_Templates" />
    <add key="DDL_TemplatePath" value="C:\Users\Matt\Documents\Visual Studio 2010\Projects\GoferConsole\GoferConsole\DDL_Templates" />
  </appSettings>
</configuration>

I used the name “Example” for the name of the database we will be using for data access. There are two connection strings since we will have one for our data access as well as one for our creation statements. You will see two different DatabaseType values you can leave for now. We will go into how you can go against any back-end system in a future post. Finally, there are two paths for the CRUD and DDL templates. Make sure that you update the paths to the directory where these folders are located on your machine.

If you run your application and you left PerformMigration to true, Gofer will create the database for you. It will then try and insert a new record and the pull all records from the Customers table.

NOTE: I did need to change my project type from the Client Profile to the full .NET 4 Framework.

Here is one last tidbit, if you put a break point after your insert statement, you can access a SqlTrace property on the Repository instance. This will show you what was executed and any error messages coming back from SQL Server. This is really helpful especially when you are migrating changes over to the database. Gofer does this automatically when you have the PerformMigration property set to true.

In my next post, I will be basically doing the same thing but using Gofer over the web for Silverlight without any need for a proxy! My main goal for Gofer is simplicity and allowing us to get back to focusing on our business rules and domain models. Gofer has a lot of extensibility and we will be going into this in future posts as well.

If you start playing with this, remember that it is the tip of the iceberg and I will be going into more depth on multiple levels. Hope you like…

Advertisements
  1. Chui Tey
    January 3, 2012 at 2:05 pm

    Matt,

    I’ve just tried this. Very nice out of the box experience. This reminds me of the days when simple things were simple.

    Look forward to you next post.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: