Sitecore - Assert.IsNotNull
Asked Answered
K

2

5

I've seen few sample sitecore applications which uses the code below within the business logics:

 Database database = Factory.GetDatabase(itemUri.DatabaseName);
 Assert.IsNotNull(database, itemUri.DatabaseName);
 return database.GetItem(attribute);

Could someone please clarify if this is a sitecore convention. I've only used Assert for unit testing scenarios but not within logic.

Thanks.

Kato answered 9/3, 2013 at 0:0 Comment(0)
A
5

It is a convention that can be seen heviliy used in the sitecore.dll's. It is used to throw an exception if that condition is not met.

For example if you look at Assert.IsTrue, if the condition is not met the system will throw a "InvalidOperationException"

De-compiling the a method from the Search API I found this.

  Assert.IsTrue(local_0 != null, "SearchConfiguration is missing");

Then if we de-compile IsTrue, it gives us

   [AssertionMethod]
public static void IsTrue([AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition, string message)
{
  if (!condition)
    throw new InvalidOperationException(message);
}

To Answer your other question you can use this in your application code, as you can see its just another way of throwing an exception if a condition is not met.

The confusion comes with the use of the word assert, which as you said is usually seen in context of unit tests in a traditional c# .NET solution. As long as you know what the Sitecore assert is doing behind the scenes, its up to you if you want to use it or not.

Atheistic answered 19/5, 2015 at 0:36 Comment(0)
C
8

I found this article which addresses most of your question. There are a couple of important points:

  • The .NET Assert is not the same as the Sitecore Assert. At a minimum, the Sitecore Assert is a good deal more verbose.
  • It is considered best practice in Sitecore to use Asserts to check method inputs. (And you will find these Asserts consistently in Sitecore's code and the code in the Sitecore community (I can't tell you how annoying Field is null can be.))

I think it is also important to note that many of the examples I seem to be finding in the Sitecore blogs are cases where an exception would happen anyway. So, if ArgumentIsNotNull were to be omitted, for example, that would result in a NullObjectException, so an Assert actually cleans things up a bit. In your case, if the Database is unavailable, that would also cause a problem. An Assert makes it so that cause of the error is clear.

Calfee answered 9/3, 2013 at 5:30 Comment(3)
It is worth pointing out the System.Diagnostics.Debug.Assert is conditional and will only fire in DEBUG builds of an application. Sitecore.Assert has no such restriction so will raise an InvalidOperationException whenever the test condition fails in both DEBUG and RELEASE builds.Roderickroderigo
Could you clarify "Assert cleans things up a bit". Does it mean exception is swollen. Still don't see reason why we need it.Kato
As Kevin says, you don't want to rely only on asserts. Assert is meant to cover cases that are likely to help during testing, like a resource not being present or a value being null that should not. You should still use validation and exceptions and exception handling for production code as the asserts do not function in a release build. They are helpful while testing as they are quick to add and can provide more feedback with little effort.Extant
A
5

It is a convention that can be seen heviliy used in the sitecore.dll's. It is used to throw an exception if that condition is not met.

For example if you look at Assert.IsTrue, if the condition is not met the system will throw a "InvalidOperationException"

De-compiling the a method from the Search API I found this.

  Assert.IsTrue(local_0 != null, "SearchConfiguration is missing");

Then if we de-compile IsTrue, it gives us

   [AssertionMethod]
public static void IsTrue([AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition, string message)
{
  if (!condition)
    throw new InvalidOperationException(message);
}

To Answer your other question you can use this in your application code, as you can see its just another way of throwing an exception if a condition is not met.

The confusion comes with the use of the word assert, which as you said is usually seen in context of unit tests in a traditional c# .NET solution. As long as you know what the Sitecore assert is doing behind the scenes, its up to you if you want to use it or not.

Atheistic answered 19/5, 2015 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.