Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoFac: Inject NULL values

Tags:

c#

null

autofac

I want to use AutoFac to inject the current principal in the objects that need it. Suppose I have an object (AuthorizationValidator) that is performing security checks. It looks something like this:

public AuthorizationValidator : IAuthorizationValidator
{
  public AuthorizationValidator(IDataAccess dataAccess, IPrincipal principal)
  {
     // Save injected objects
     ...
  }

  public bool CheckPermission(Guid objectId, Action action)
  {
     // Check if we are authorized at all
     if (this.principal == null)
       return false;

     // Check the permission in the database
     ...
  }
}

For my web application the AuthorizationValidator is registered and I use the following registration to inject the principal:

builder.Register<IPrincipal>((c, p) => HttpContext.Current?.User);

Other type of applications use the thread's principal or something similar. All object that require the principal get the proper principal injected.

If a call is made without authorization, then AutoFac raises an exception telling me that it cannot provide the IPrincipal object, because the factory returned null. In this case, an empty principal is fine and shouldn't raise an exception.

like image 760
Ramon de Klein Avatar asked Jul 20 '16 09:07

Ramon de Klein


1 Answers

In the Autofac documentation they recommend to use the Null Object pattern for such scenarios. You could create a NullPrincipal class that inherits from the IPrincipal interface with a private constructor that only exposes a readonly static field which provides the default instance. Then you can return this instance instead of null:

builder.Register<IPrincipal>((c, p) => HttpContext.Current?.User ?? NullPrincipal.Default);

Of course you would have to update all places in your code where you are checking if the principal is null and check if it is equal to the NullPrincipal.Default instead.

like image 123
fknx Avatar answered Oct 24 '22 05:10

fknx