Kris Krause .NET Meister

"If it is fast and ugly, they will use it and curse you; if it is slow, they will not use it."
- David Cheriton, The Art of Computer Systems Performance Analysis

Thursday, August 06, 2009

.NET mySql PerformanceMonitor Class

The 6.x mySql Connector for .NET comes with two performance counters that seem to be working rather well. If I call the same stored procedure 1,000 times... the metadata is only retrieved from the server the first time. All subsequent calls utilize the client-side cache. You can see this using the Performance Monitor graph in Control Panel -> Administrative Tools -> Performance.

Also, you have set the following parameter in your connection string:

Use Performance Monitor=true;

HardProcedureQueries - The number of times a procedures metadata had to be queried from the server.

SoftProcedureQueries - The number of times a procedures metadata was retrieved from the client-side cache.

Labels: ,

Monday, June 29, 2009

Concrete Example - Mocking .NET Objects w/NUnit

Geoff Lane does a very good job of explaining .NET Mock objects.

People were requesting a "concrete" example so I implemented an example C# console application (although this can easily be made into an ASP.NET web application).

Geoff hints to using Spring .NET to wire things up, but that might be too confusing for some people. And maybe a little over board for a simple example (open closed principle).

So instead I created a PersonServiceManager that creates a "concrete" PersonService via IPersonRepository injection. The GUI communicates to the PersonServiceManager through an IPersonService interface.

Instead of "mocking" an IPersonRepository, we are actually implementing it then injecting it into PersonService (just like our NUnit test). The PersonServiceManager handles passing the appropriate database connection string down to the data layer. In my example, the data layer object PersonDb (which implements IPersonRepository and used for injection) utilizes the DbProviderFactory.

Here is the sample source (C# Console App).

Also remember that the sample source could be broken down into separate assemblies in a real world project.

Labels: ,

Wednesday, April 08, 2009

MySql Connector .NET MySqlBulkLoader Example

Here is a MySqlBulkLoader example I quickly wrote to import 65,000 records into a mySql 5.1 database using the mySql Connector 5.2.5. And yes, its fast as snot (the lead programmer at my first programming job out of college invented the term).

.NET Reflector helped me figure out the follow details:

- the default FieldTerminator is "\t"
- the default LineTerminator is "\n"
- the default FieldQuotationCharacter is '\0'
- the .Load() method both opens and closes the connection

Also, the mySql documentation obviously helped to explain a lot including the "\r\n" Win32 line terminator in my example (I used Excel to export the CSV). The database user in this example needs "insert" permission.

Download the VB.NET, Sql, and CSV files here.

Labels: ,

Wednesday, February 25, 2009

Unit Testing ASP.NET App_Code

I learn something new every day. Today, its the ability to Unit Test code in the App_Code folder using Visual Studio 2008 Professional.

Most of the ASP.NET projects that I work on have their business logic contained in a separate assembly. However, a recent smaller project has some business classes residing in the App_Code directory.

Tip: Just make sure you watch your dynamic port assignments.

Labels: ,

Thursday, October 02, 2008

Using Microsoft ADO.NET Entity Framework with MySQL

Unfortunately, this week's Using Microsoft ADO.NET Entity Framework with MySQL Webinar is being postponed to Tuesday November 4, 2008. Myself and many others are looking forward to some (if not all) entity framework support. It seems that I check the mySQL dev forum daily for updates.

The myDirect.NET product offers support for the ADO.NET Entity Framework with a commercial license fee.

Labels: ,

Thursday, August 07, 2008

Visual Studio 2008 Justification

There are two primary justifications for my company's latest purchase of Visual Studio 2008 Professional -

1. The ability to "target" previous .NET versions.
2. The integrated unit testing.

While we still have not setup the automatic builds (command line)... and we still utilize NAnt... the integrated unit testing feature of Visual Studio 2008 rocks. Sure there are alternatives (we still have some projects with NUnit testing)... and various mocking frameworks, however the ability to "right click" create unit test/private accessor is very productive for us.

Labels: ,

Wednesday, July 23, 2008

VB.NET InternalsVisibleTo

Unfortunately, the InternalsVisibleTo attribute does not work with the .NET 2.0 Framework for VB.NET. However, I just installed Visual Web Developer 2008 Express SP1 Beta and it works! Thanks Microsoft for making my NUnit testing a little easier (and cleaner).

To reproduce:

Create a VB.NET class library named ClassLibrary1. Add a single class with only a friend constructor. Add the following attribute to AssemblyInfo.vb:

Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("ClassLibrary2")

Add another VB.NET class library named ClassLibrary2. Add a project reference to ClassLibrary1. Verify that you are able to construct an instance of the above class.

Labels: ,

Tuesday, June 03, 2008

mySql ASP.NET Membership and Role Provider

The mySql.Web.dll is a great addition to the mySql Connector for .NET. In order to utilize one must create (and use) a mySql user (localhost) with select, insert, update, delete, create, drop, and alter privileges.

Quickly running ASP.NET Configuration Web Site Administration Tool helps to set up and initialize all of the tables.

Labels: ,

Tuesday, January 08, 2008

Efficient Role-Based Authorization in ASP.NET

Efficient Role-Based Authorization in ASP.NET by Fritz Onion and Craig Andera is a good article. I know its a bit "old", but its still relevant and worth the read for any serious ASP.NET developer.

Labels:

Tuesday, July 24, 2007

Visual Studio 2005 on Mac OS X

Utilizing Parallels 3.0 http://www.parallels.com/en/products/desktop/ I am able to run Visual Studio 2005 on my Macbook (Mac OS X). Parallels Coherence makes it seem that I am running Visual Studio right inside my Mac desktop window instead of a separate virtualized window.

Here are some larger screenshots:

http://www.kriskrause.com/images/mac-vs-net-2005.jpg

http://www.kriskrause.com/images/mac-vs-net-2005-widgets.jpg (Widgets Overlay)

Labels: ,

Tuesday, April 12, 2005

Default ASP.NET Submit Button

Here are two ways to solve the problem of having multiple textboxes and multiple buttons on a form. Each button corresponds to a textbox. When the user enters something in the textbox and presses the enter key... we want the appropriate button to fire.

http://www.metabuilders.com/Tools/DefaultButtons.aspx
http://weblogs.asp.net/rajbk/archive/2003/12/11/43023.aspx

Labels:

Tuesday, February 08, 2005

Printing a Remote Document or Url Using C#

The following code enables a local machine to print a remote document or Url. One example would be a desktop application that needs to print a remote PDF or a Html producing web page.

The simple example downloads the file locally in order to print using the default printer. Sure there might be a better way to do this... and the code needs to be modified to handle "file not found", "url not found", and "unable to connect" exceptions in case a cable is loose. Cable? Who uses cable? It's all wireless nowadays!

using System;
using System.Diagnostics;
using System.Net;

namespace ShellPrint
{
class CMain
{
[STAThread]
static void Main(string[] args)
{
string lfile, file, ext;
Process proc = null;
WebClient client = null;

try
{
if (args.Length < 2) throw new ArgumentException("Invalid arguments passed.");

file = args[1].Trim();
ext = args[0].Trim();

client = new WebClient();

lfile = @"c:\" + DateTime.Now.Ticks + "." + ext;

Console.WriteLine("Downloading: {0}", file);

client.DownloadFile(file, lfile);

Console.WriteLine("Saving: {0}", file);

proc = new Process();

proc.StartInfo.FileName = lfile; 
proc.StartInfo.Verb = "Print";
proc.StartInfo.CreateNoWindow = true;

Console.WriteLine("Printing: {0}", lfile);

proc.Start();
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("Done and done.");
Console.ReadLine();
}
}
}
}

Labels:

Wednesday, February 02, 2005

Accessing iTunes With C#

The following example dumps your entire iTunes library to the console. You need to reference the COM iTunes 1.2 Type library. Currently I am running iTunes version 4.7.1.30.

using System;

namespace PlayListExample
{
class CMain
{
[STAThread]
static void Main(string[] args)
{
iTunesLib.IiTunes app = null;

try
{

app = new iTunesLib.iTunesAppClass();

foreach (iTunesLib.IITTrack track in app.LibraryPlaylist.Tracks)
{
Console.WriteLine("Artist: {0}\nSong: {1}\nGenre: {2}\n", track.Artist, track.Name, track.Genre);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
app.Quit();

app = null;

Console.ReadLine();
}
}
}
}

Labels: ,

Parsing dBase .DBF to .CSV Using C# and VB.NET

Per a New Jersey Microsoft Developer's Group message board request I created a sample console application that parses a .DBF file and creates an Excel usable .CSV file. Zero magic if you take advantage of the System.Data.Odbc namespace.

The command line syntax is:

FPParse.exe [table name] [output dir]
FPParse.exe canada c:

The generated .CSV file is just as good as an .XLS file. For fancy .XLS features and formatting I suggest modifying my code to utilize Excel COM Interop.

The C# source code is found here.

The VB.NET source code is found here.

Labels:

Monday, January 31, 2005

DataView vs. DataTable

Yes, I still pull my hair out over DataView versus DataTable every once in a while.

using System;
using System.Collections;
using System.Data;

public class MyTestClass
{
public static void Main()
{
DataTable dt = new DataTable();

try
{
DataColumn col1 = new DataColumn();
col1.DataType = System.Type.GetType("System.Int32");
col1.ColumnName = "EmployeeID";

dt.Columns.Add(col1);

DataColumn col2 = new DataColumn();
col2.DataType = System.Type.GetType("System.String");
col2.ColumnName = "LastName";

dt.Columns.Add(col2);

dt.Rows.Add(new Object[] {1, "Covell"});
dt.Rows.Add(new Object[] {2, "Schaefer"});
dt.Rows.Add(new Object[] {3, "Krause"});
dt.Rows.Add(new Object[] {4, "Meloskie"});
dt.Rows.Add(new Object[] {5, "Vader"});

MyDataView myView = new MyDataView(dt, "EmployeeID < 3");

Console.WriteLine("Filter row count: {0}", myView.CurrentFilterCount);
Console.WriteLine("Row count: {0}", myView.CurrentCount);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.WriteLine("Done and done.");
Console.ReadLine();
}
}
}

public class MyDataView : DataView
{
public MyDataView(DataTable dt, string filter) : base(dt)
{
this.RowFilter = filter;
}

public int CurrentFilterCount
{
get { return this.Count; }
}

public int CurrentCount
{
get { return this.Table.Rows.Count; }
}
}

Labels:

Wednesday, January 19, 2005

Visual Source Safe 2005 Beta

As soon as I have time to download Visual Source Safe 2005 Beta I am going to install it ASAP. Some of the new features include: Web Service Configuration, Remote Access via HTTP, Performance Improvements, Globalization, Visual Studio Integration: Rename/Deletes, File Dialogs, Asynchronous open.

The "big" and most desirable feature is Remote Access via HTTP. We telecommuters have to be patience using today's version of Source Safe 6 over a VNP. Using Visual Studio .NET over the VPN is not too painful... but for those of us who still do some VB6 coding in Visual Studio 6... its extremely painful.

Labels:

Tuesday, January 18, 2005

.NET Remoting Events: Server to Client(s)

I ported Sathish's code from VB.NET to C#. Pretty straight forward. His article is located here. Download the updated source code here.

If you leave the application running for ten minutes... come back... and try to send a message via the EventPublisher... the client(s) do not receive the message. In order to prevent timeouts I forced an "infinite lease" in both Hoster.cs and EventsManager.cs:


public override object InitializeLifetimeService()
{
return null;
}

Labels:

Friday, January 14, 2005

ImageGlue & ABCpdf

At my current consulting gig we are heavily using ImageGlue .NET and ABCpdf .NET developed by webSupergoo.

Zero complaints... aside from the usual... "I wish it was faster, smaller, and does everything that I want it to."

Labels: