-
Designs, testing secured code with nUnit and a book review
-
My current project is finishing the design phase this week. Our leads are insisting that we design to a low level, including method signatures for business objects, the database layer and the stored procedures. We have been iterating the designs extensively and they are becoming very detailed. I've learned a ton about my functional area and will now be able to easily write the code. As I understand it, the rationale behind extensive upfront design is that changing the code is much more expensive than changing the designs. With modern tools this is becoming less true. I can change a method signature and callers in VStudio as quickly as I can update a Word document and fix up the Interaction Diagrams in Visio. My recent projects have been 2 or 3 developers using an Agile process, this one is 10+ developers.
If you need to use nUnit to test code that is protected by .NET security, you need to provide a Principal to the nUnit thread. For example, // This principal will be used throughout the tests
Thread.CurrentPrincipal = new GenericPrincipal( new GenericIdentity("TedsTester"), new string[] { "DeleteEverything" } );
[What I'm reading: Effective Java Modeled after Scott Meyer's incredible Effective C++ books, this manual of Java best practices should be required reading for C# developers. Joshua Bloch is an architect on the Java class libraries and this book is filled with the knowledge that comes from years of use. C# is quite close to Java and most of the tips in this book apply beautifully. Especially good for C++ developers moving to C#. In 5 years someone will know C# well enough to write an equivalent book, but this one will help you code better while we wait. (I'm an Amazon affiliate, all monies will be matched and donated to a homeless shelter in southern Colorado)]
-
Output params from stored procs using MS Data Access Application Block
-
As my current client is early in the architecture phase, I’ve been reviewing the MS Data Access Application Block. I did a brief experiment, and I’m having problems retrieving output parameters from stored procedures. I created a simple stored proc (with CodeSmith):
[Edit: I just debugged this with one of the MS Consulting Services Architects on this project. There is a bug in the Data Access App Block that causes this problem when you call methods that take an array of objects that hold your parameter values. Calling the overload that takes a SqlParameter array works fine. He wasn't real suprised that there are problems with the DAAB.]
[Edit2: Bill Selznick's comment is right on. Most of the people that mail me about this problem are calling the wrong overload of ExecuteNonQuery. Step into the DAAB and make sure you are calling the overload that you intend to.]
CREATE PROCEDURE dbo.InsertValidationPolicy @Severity int, @Message nvarchar(50), @Required bit, @ValidationPolicyID uniqueidentifier OUTPUT AS SET @ValidationPolicyID = NEWID() INSERT INTO [ValidationPolicies] ( [ValidationPolicyID], [Severity], [Message], [Required] ) VALUES ( @ValidationPolicyID, @Severity, @Message, @Required ) GO
And created a simple data access object with static methods (doing the prototype before I build the CodeSmith template). The below method calls the stored procedure to create a new object. Notice the fourth parameter is an output parameter, and that the stored procedure is executed using SqlHelper (the main component in the Data Access App Block). static public Guid CreateValidationPolicy(int severity, string message, bool required) { SqlParameter paramSeverity = new SqlParameter("Severity", SqlDbType.Int); paramSeverity.Value=severity; SqlParameter paramMessage = new SqlParameter("Message", SqlDbType.NVarChar); paramMessage.Value=message; SqlParameter paramRequired = new SqlParameter("Required", SqlDbType.Bit); paramRequired.Value=required; SqlParameter paramID = new SqlParameter("PolicyID",SqlDbType.UniqueIdentifier); paramID.Direction = ParameterDirection.Output; SqlParameter[] parameters = { paramSeverity, paramMessage, paramRequired, paramID }; SqlHelper.ExecuteNonQuery(ConnectionString, "InsertValidationPolicy", parameters); return (Guid)paramID.Value; }
This method throws an Exception complaining that, “Object must implement IConvertible”. It works fine if I don’t use output parameters. There are several threads discussing this problem in the newsgroup dedicated to the MS Building Blocks. The best discussion was started by Jim Bentley on 2/27/03 (sorry, but I don’t know how to link to news threads) and includes some responses from Microsoft, but no solutions.
Ted
-
Generating data layers and testing the results
-
From initial discussions of the technical architecture I think we are planning on handwriting all the stored procedures and the data objects that wrapper them. I'd much rather use LLBLGen or another data layer tool to automate this, but it may not be my decision. I'd love to hear anyone's experience using data layer tools. Ideally I'd like round-trip generation, but could consider generating the initial objects and maintaining them by hand. I read the Great Data Generation Layer Debate with interest, but it was a discussion of different tool styles instead of first hand reports on using the tools. Regardless of how we build the data layer, I'd like to use nUnit to test it. I've had great experiences with nUnit and many of the consultants agree, but the MCS architect I discussed it with thinks writing the tests will slow us down.
[What I'm reading: The Things They Carried A series of brilliantly written short stories about the adventures of a squad in Vietnam. I read it in a day, but it would make a great bedside book if you want to read 15 minutes a night.]