Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 988779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:41:12+00:00 2026-05-16T05:41:12+00:00

I have an interface to resolve and one of the mapped object’s dependencies has

  • 0

I have an interface to resolve and one of the mapped object’s dependencies has a property on it which I would like to set with a value that I only have available when I resolve the top level object.

There’s no valid default value for the property. If its not set it should be null and it should only be set if the value that I have available at resolve time is not null.

Is this conditional property injection possible?

I tried this…

container.RegisterType<ProductInstanceValidatorBase, CartItemPurchaseTypeValidator>("CartItemPurchaseTypeValidator", new InjectionProperty("AccountEntity", null);

… but it said I couldn’t use a null value!

I also tried this on the resolve…

container.Resolve<ProductInstanceValidatorBase>(new PropertyOverride("AccountEntity", value));

…but this throws an exception when the value is null. It says,

Parameter type inference does not work for null values. Indicate the parameter type explicitly using a properly configured instance of the InjectionParameter or InjectionParameter classes.
Parameter name: parameterValue

Basically I’m looking to register a property that is only set with an override and then only if the override value is non-null. Any ideas? Surely from a semantic point of view, property injection should be optional.

Cheers, Ian.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:41:12+00:00Added an answer on May 16, 2026 at 5:41 am

    One potential fix to this problem is to implement a Null Object Pattern and pass Account.Empty when you haven’t got a valid account. Here is what a class could look like:

    public class Account {
       public static readonly Account Empty = new Account();
    }
    
    //at resolution time
    Account account = null;
    if (HasAccount) 
      account = GetAccount();
    else 
      account = Account.Empty;
    
    container.Resolve<ProductInstanceValidatorBase>(new PropertyOverride("AccountEntity", account));
    

    This way the account is never null and Entity framework wouldn’t complain.

    However I sense that you might have a larger problem with the design of the system. IoC container allows a more loosely coupled system, where the wiring of the components is defined before the program is run, not while it’s run. Judging by the name, AccountEnyity is an entity class, not a service class, and you normally do not register entities with the IoC container, only services.

    In the light of the above I would suggest that AccountEntity shouldn’t be an injectable property, but instead a normal property. You then externalize the ProductInstanceValidatorBase creation into a factory and let it take care of setting AccountEntity property

    public interface IProductInstanceValidatorFactory{
      ProductInstanceValidatorBase Create(Account account);
    }
    public class ProductInstanceValidatorFactory : IProductInstanceValidatorFactory{
      public ProductInstanceValidatorBase Create(Account account){
         var validator = new ProductInstanceValidator();
         validator.AccountEnity = account;
         return validator;
      }
    }
    

    You don’t even need to register ProductInstanceValidatorBase with Unity, but instead register the factory. This is how using the factory would look like:

    Account account = null;
    container.Resolve<IProductInstanceValidatorFactory>().Create(account);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.