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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:28:13+00:00 2026-06-01T03:28:13+00:00

We are planning to use Unity in our project to dynamically load objects/types residing

  • 0

We are planning to use Unity in our project to dynamically load objects/types residing in different assemblies. To register the types we are using the .config file. I have written a small sample app to test this out and all works well. Following is the sample code:

/* App.config
...
<container name="xyz">
      <register type="IOperations" mapTo="COperations">
        <lifetime type="external" />
      </register>

      <register type="ITest" mapTo="CTest">
        <lifetime type="external" />
      </register>
</container>
...
*/
public class CA
{ }
public class CB
{ }

public interface IMyOperations : IDisposable
{
    void DoSomething(string str);
}
public interface ITest : IDisposable
{
    void DoSomethingElse(string str);
}

class Program
{
    static void Main(string[] args)
    {
        string containerName = "xyz";
        using ( IUnityContainer container = new UnityContainer() )
        {
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(container, containerName);

            IMyOperations myOps = container.Resolve<IMyOperations>();
            myOps.DoSomething("Hello");
            myOps.Dispose();

            ITest test = container.Resolve<ITest>();
            test.DoSomethingElse("World!");
            test.Dispose();

        }
    }
}

public class COperations
{
    private IUnityContainer container;
    public COperations(IUnityContainer container)
    {
        this.container = container;
    }

    public void DoSomethingElse(string str)
    {
        // based on runtime values create an instance of class CA or CB and register them with the container
        if (str.Length > 10)
            container.RegisterInstance<CA>("InstanceCA", new CA());
        else
            container.RegisterInstance<CB>("InstanceCB", new CB());
    }
    public void Dispose()
    { }
}


public class CTest
{
    private IUnityContainer container;
    public CTest(IUnityContainer container)
    {
        this.container = container;
    }

    public void DoSomethingElse(string str)
    {
        // based on runtime values get the registered instance of class CA or CB
        if (str.Length > 100)
            container.Resolve<CA>("InstanceCA");
        else
            container.Resolve<CB>("InstanceCB");
    }
    public void Dispose()
    { }
}

I am also creating new objects (of class CA and CB in the code above) in the container-resolved-type and registering those with the container using ResisterInstance(). To do this I am changing the the signature of ctor of my container-resolved-type looks like the following:

public COperations(IUnityContainer container)

I am not sure if this is the right approach. Is there a better way to use the container to register instance from the container-resolved-type object? I am trying to share a few objects among each other, which I can know only at run time. Also this makes my components hardwired with Unity and not helpful in case we decide to move to another container at a later point of time.

  • 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-01T03:28:14+00:00Added an answer on June 1, 2026 at 3:28 am

    I’d suggest creating a Registration Factory if you want to register things in code. You’d then be passing the Registration Factory via the constructor and it would register your objects. This would allow you to decouple from Unity.

    So:

    public interface IRegistrationFactory
    {
        void RegisterInstance<TI,TO>();
        void RegisterType<TI,TO>();
        void RegisterInstance<TI,TO>(string name, TO objectToRegister);
    }
    
    public class COperations
    {
        private IRegistrationFactory _factory;
        public COperations(IRegistrationFactory factory)
        {
            _factory= factory;
        }
    
        public void DoSomethingElse(string str)
        {
            // based on runtime values create an instance of class CA or CB and register them with the container
            if (str.Length > 10)
                _factory.RegisterInstance<CA,CA>("InstanceCA", new CA());
            else
                _factory.RegisterInstance<CB,CB>("InstanceCB", new CB());
        }
    

    you’d obviously need to implement IRegistrationFactory. All this would be is a class that takes a IUnityContainer in its constructor and then implemented the interface.

    If you decide to change from Unity to another DI container all you need to do is tweak IRegistrationFactory to handle the new DI container.

    Also, if you’re registering instances you can do it like this:

    _container.RegisterType(new ContainerControlledLifetimeManager());

    this will always return the same instance of the object so it like a RegisterInstance. The advantage of this approach is that Unity will resolve any of CAs dependencies for you, whereas if you use RegisterInstance you have to manually do this.

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

Sidebar

Related Questions

We're planning to use standard ASP.NET user authentication for our application. However, by default
I am planning to use P6Spy to intercept database statements within our architecture. However,
I am planning to use SCTP for a embedded Linux project. Anybody has some
I am planning to use libcurl in my project. I had downloaded the library
I am planning to use facebook authentication for my application. I thought of using
I'm planning to use Mustache templates along with Kohana in my next project. So
We are planning to use AdControl in our windows phone app to show advertisement.
I'm planning to use openCMIS (apache chemistry project) with my JCR repository via JCR
We are planning to use Qtwebkit for Help viewer in our application, we need
I'm planning to use the Multiple apk approach to my current project, since it

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.