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 8768945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:12:03+00:00 2026-06-13T17:12:03+00:00

I’m using the Provider pattern and Unity. This is the typical provider implementation. I

  • 0

I’m using the Provider pattern and Unity.

This is the typical provider implementation. I have SomeProvider as abstract base provider with the abstract methods as well as the logic to instantiate the defaultProvider.

public abstract class SomeProvider : ProviderBase
{
    #region provider boilerplate

    private const string PROVIDER_NAME = "someProvider";
    private static volatile SomeProvider defaultProvider = null;
    public static SomeProvider Provider
    {
        get { return defaultProvider; }
    }
    private static object providerLockObject = new object();
    static SomeProvider()
    {
        LoadProvider();
    }
    private static void LoadProvider()
    {
        if (defaultProvider == null)
        {
            lock (providerLockObject)
            {
                if (defaultProvider == null)
                {
                    // exception handling omitted for brevity
                    var section = ConfigurationManager.GetSection(PROVIDER_NAME)
                        as BaseProviderConfigurationSection;
                    defaultProvider = ProvidersHelper.InstantiateProvider(
                        section.Providers[section.DefaultProvider], typeof(SomeProvider)) as SomeProvider;
                }
            }
        }
    }

    protected SomeProvider() { }

    #endregion

    #region abstract methods

    public abstract bool DoSomething();

    #endregion
}

Here is the ASomeProvider that implements SomeProvider. Note that ASomeProvider has dependancy ADependency but SomeProvider does not.

public class ASomeProvider : SomeProvider
{
    #region provider boilerplate

    private string name;
    public override string Name
    {
        get { return name; }
    }

    public override void Initialize(string name, NameValueCollection config)
    {
        this.name = name;
        base.Initialize(name, config);
    }

    #endregion

    // Provider pattern needs parameterless ctor and calls this
    public ASomeProvider() { }
    // constructor injection
    public ASomeProvider(ADependency aDependency)
    {
        this.ADependency = aDependency;
    }

    [Dependency]
    public SomeDependency ADependency { get; set; }

    #region methods

    public override void DoSomething()
    {
        // do something
    }

    #endregion
}

class SomeDependency {}

I use the ASomeProvider in a business layer as follows:

public class SomeBusinessLayer
{
    public SomeProvider someProvider;

    public SomeBusinessLayer(SomeProvider someProvider)
    {
        this.someProvider = someProvider;
    }

    #region methods
    public bool DoSomethingWrapper()
    {
        return someProvider.DoSomething();
    }
    #endregion
}

I have the BusinessLayerFactory factory for wiring up objects using Unity and returning objects as follows:

public static class BusinessLayerFactory
{
    private static UnityContainer container;

    private static void WireUp()
    {
        container = new UnityContainer();

        container.RegisterInstance(SomeProvider.Provider);
        container.RegisterInstance(new SomeDependency());
        container.RegisterType<SomeBusinessLayer>(new ContainerControlledLifetimeManager());
    }
    public static SomeBusinessLayer SomeBusinessLayer_Unity
    {
        get
        {
            return container.Resolve<SomeBusinessLayer>();
        }
    }
    public static SomeBusinessLayer SomeBusinessLayer_Self()
    {
        var asomeProvider = SomeProvider.Provider as ASomeProvider;
        if (asomeProvider != null && asomeProvider.ADependency == null)
            asomeProvider.ADependency = new ADependency();
        return new SomeBusinessLayer(SomeProvider.Provider);
    }
}

The problem that I have is that when I resolve to get SomeBusinessLayer, the someProvider has its dependency ADependency as null.

var someBusinessLayer = BusinessLayerFactory.SomeBusinessLayer_Unity;

((ASomeProvider)someBusinessLayer.someProvider).ADependency is null.

The reason for it is that the Provider pattern uses the helper method ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type type) to instantiate the default provider and not through Unity’s Resolve<T>() method.

I can think of doing this without Unity as shown in SomeBusinessLayer_Self where I new ADependency if null but just wondering if and how Unity handles this.

Method injection or Ctor injection is fine by me.

How do I fix this and have Unity and Provider pattern work together? My main reason for using Unity is to wire up objects in the factory.

I’m not using MVC.

UPDATE:

To fix this:. hat-tip to @seth-flowers

I could not figure out a way without an explicit cast container.BuildUp<ASomeProvider>(aSomeProvider).

container.RegisterInstance(new SomeDependency());

//this does not build up the object with dependency
container.BuildUp(SomeProvider.Provider);
//this works and does build up
var aSomeProvider = SomeProvider.Provider as ASomeProvider;
if(aSomeProvider != null)
    container.BuildUp<ASomeProvider>(aSomeProvider);
//compile error
container.BuildUp<ASomeProvider>(SomeProvider.Provider);

container.RegisterInstance(SomeProvider.Provider);

container.RegisterType<SomeBusinessService>(new ContainerControlledLifetimeManager());
  • 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-06-13T17:12:05+00:00Added an answer on June 13, 2026 at 5:12 pm

    Couldn’t you use UnityContainer.BuildUp in your BusinessLayerFactory.WireUp method, in order to flesh out the dependencies on your SomeProvider.Provider instance? Your object would still be constructed through your provider implementation, but would have dependency injection through unity.

    For instance:

    private static void WireUp()
    {
        container = new UnityContainer();
    
        container.RegisterInstance(SomeProvider.Provider);
        container.RegisterInstance(new SomeDependency());
        container.RegisterType<SomeBusinessLayer>(
            new ContainerControlledLifetimeManager());
    
        container.BuildUp<ASomeProvider>(SomeProvider.Provider as ASomeProvider);
    }
    

    The docs for BuildUp state the following:

    This method is useful when you don’t control the construction of an
    instance (ASP.NET pages or objects created via XAML, for instance) but
    you still want properties and other injection performed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.