3

I'm sure I'm being really stupid here - but I'm getting into Dapper and contrib. Sample code includes lines like this:

 using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            connection.Get<User>(3).IsNull();

            var id = connection.Insert(new User { Name = "Adam", Age = 10 });

IsNull() is never recognised. Nor is IsEqualTo in the same context.

I have googled - nothing close, searched in object browsers - am using Dapper - and using Dapper.Contrib.Extensions; But it sill can't find it.

thx

niico
  • 11,206
  • 23
  • 78
  • 161
  • What is the connection string? It is in the project configuration settings which is an xml file. The tag is "DefaultConnection". You are connecting to a SQL Server and the name of the server is in the connection string and must be valid and match the instance of the server you are using. – jdweng May 23 '16 at 00:23
  • the connection string is to a sql server instance - that works fine and isn't causing the error (I can insert data if i comment the isNull line) - I would just like to know where IsNull and IsEqualTo come from – niico May 23 '16 at 00:32
  • Right click on GET and then select 'go to definition'. – jdweng May 23 '16 at 00:42
  • It's in Dapper.Contrib.Extensions – niico May 23 '16 at 00:49
  • Boolean status = connection.Get(3).IsNull(); I suspect that IsNull will create an exception which is handled by the 'using' so the application doesn't halt when the connection isn't defined in the configuration file. – jdweng May 23 '16 at 00:53
  • OK but IsNull has a red line under it - the solution will not compile. – niico May 23 '16 at 00:57

1 Answers1

2

Those are actually methods from the testing framework that is being used. They are actually assertions; the IsNull() is asserting that the value on the left is null, and throwing an exception otherwise. The IsEqualTo is asserting that the value on the left is equal to the value passed to the method, and throwing an exception otherwise.

You don't need those methods for real code. I guess that the example has been lifted from a test method, where it is being used to confirm the state of the data before and after the insert.

It comes to mind that AssertNull and AssertEqualTo might be better names!

The code is in Assert.cs; they could also be invoked via:

Assert.IsNull(connection.Get<User>(3));
...
Assert.IsEqualTo(someObj.SomeProp, 42);

etc, in which case the intent would be more obvious. The fact that they are extension methods hides a little bit of detail in this case (specifically, the fact that the declaring type is Assert).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 We have similar methods in our internal framework that helps ReSharper flag code more correctly, that are compiled away in release builds, but under no circumstance should production code contain a reference to a unit-test framework... unless you're building code that executes tests. – Lasse V. Karlsen May 23 '16 at 07:54
  • @LasseV.Karlsen the methods in question aren't even part of the core library project; they are in the test project only. You can't reference the test project since we don't ship it. I agree with your point - it just doesn't really apply here :) – Marc Gravell May 23 '16 at 07:55
  • My point was that I agreed with you :) It got lost in translation from my head. More coffee I guess. – Lasse V. Karlsen May 23 '16 at 07:56
  • @LasseV.Karlsen gotta love that `[Conditional]` usage, though (I'm assuming that is what you are describing, although it could also be a `partial` method that lacks an implementation in release); very powerful for debug-only code - far prettier than filling the code with `#if DEBUG`. – Marc Gravell May 23 '16 at 08:00
  • No, it's conditional :) The entire method is empty save for `[Conditional("DEBUG")]` and `[ContractAnnotation("false => halt")]` or `[ContractAnnotation("null => halt")]`. – Lasse V. Karlsen May 23 '16 at 08:02
  • thx - so this is some arbitrary / custom testing framework - and to get this to work I need to just remove those lines as they are for demonstration purposes only? (or is there a common framework I can reference in a using statement to just make the examples run?). – niico May 23 '16 at 08:31
  • @niico you can copy/paste that `Assert.cs` to make the examples just run :) There's some back-story to the `Assert.cs` class - IIRC I ended up writing the test rig from scratch while stuck at an airport for a few hours without any network access, hence I couldn't pull down any of the usual suspects; as it happens, though, it has let us move between multiple frameworks without having to change the code. As you can see, the `Assert.cs` is currently implemented using xunit, but it has been nunit, and "other" at various points. It is essentially an accidental abstraction over the test API – Marc Gravell May 23 '16 at 09:16
  • Interesting story :D I've added Asserts.cs - I get this error on Xunit.Assert.Equal "Error CS0012 The type 'IEnumerable<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'." (i added that to web.config & tried adding a reference to "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll" - neither fixed it). I downloaded the entire solution - stuck building for 2 hours. – niico May 23 '16 at 11:27
  • 1
    @niico if it was me, I would just comment out the calls to these methods that aren't doing anything useful - anything that looks like an assertion probably isn't needed – Marc Gravell May 23 '16 at 11:59