简体   繁体   中英

C# IsNull() and IsEqualTo() - Visual Studio doesn't recognise them in sample Dapper.net / Contrib code

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

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM