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

  • Home
  • SEARCH
  • 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 5849519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:00:42+00:00 2026-05-22T13:00:42+00:00

I have a class that has in its constructor some primitive type arguments, such

  • 0

I have a class that has in its constructor some primitive type arguments, such as string etc.

How should I register the type with unity container?

public LoginManager(
  IRegionManager regionManager, 
  IEventAggregator eventAggregator, 
  string mainRegionName, 
  Uri login, 
  Uri target)
  {
    this.regionManager = regionManager;
    this.eventAggregator = eventAggregator;
    this.mainRegionName = mainRegionName;
    this.login = login;
    this.target = target;
  }
}

Update:
Remeber that the IRegionManager and the IEventAggregator are known types to the Prism UnityBootstrapper which is the container wrapper in my case. Do I have to re-register them?? I want to keep the type-registration as simple as possible.

Would this be considered a bad habit? Any better alternatives?

  • 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-22T13:00:43+00:00Added an answer on May 22, 2026 at 1:00 pm

    Try to prevent to have a class design that has primitive or hard to resolve types in the constructor. As you have already seen from Tavares’ answer, you’re configuration gets very brittle (update: Tavares seems to have removed his answer for reasons that are not clear to me). You loose compile time support, and every change to that constructor would make you change your DI configuration.

    There are multiple ways to change your design to prevent this. Which one is applicable for you is up to you, but here are a few ideas:

    Option 1: Use an immutable configuration DTO:

    private sealed class LoginManagerConfiguration
    {
        public Uri Login { get; private set; }
        public Uri Target { get; private set; }
        public string MainRegionName { get; private set; }
    
        public LoginManagerConfiguration(Uri login, Uri target, string mainRegionName)
        {
            this.Login = login;
            this.Target = target;
            this.MainRegionName = mainRegionName;
        }
    }
    

    Now you can let your LoginManager take a dependency on LoginManagerConfiguration:

    public LoginManager(IRegionManager regionManager,
        IEventAggregator eventAggregator,
        LoginManagerConfiguration configuration)
    {
        ...
    }
    

    The LoginManagerConfiguration can simply be registered like this:

    container.RegisterInstance<LoginManagerConfiguration>(
        new LoginManagerConfiguration(
            login: new Uri("Login"),
            target: new Uri("Target"),
            mainRegionName: ConfigurationManager.AppSettings["MainRegion"]));
    

    It might be tempting to specify an application-wide configuration object instead of this type-specific DTO, but that’s a trap. Such application-wide configuration object is the configuration equivalent of the Service Locator anti-pattern. It becomes unclear what configuration values a type requires, and makes classes harder to test.

    Option 2: Derive from that class

    Another option is to derive from that class, just for the purpose of DI configuration. This is especially useful when you can’t change the class signature (i.e. when it’s a third-party component):

    private sealed class DILoginManager : LoginManager
    {
        DILoginManager(IRegionManager regionManager,
            IEventAggregator eventAggregator)
            : base(regionManager, eventAggregator,
                ConfigurationManager.AppSettings["MainRegion"],
                new Uri("Login"),
                new Uri("Target"))
        {
            ...
        }
    }
    

    Define this class close to the composition root of your application. This class becomes an implementation detail of your DI configuration. Registration of your type will now be very simple:

    container.RegisterType<ILoginManager, DILoginManager>();
    

    Be very careful though with calls that lazy load configuration values, such as ConfigurationManager.AppSettings["MainRegion"]. This could easily lead to situations where configuration errors are not caught during application startup, which really is preferable.

    Option 3: Use a factory delegate

    The last option I would like to present is a factory. This will look much like Traveses’ answer, but is more type-safe:

    var mainRegion = ConfigurationManager.AppSettings["MainRegion"];
    
    container.Register<ILoginManager>(new InjectionFactory(c =>
    {
        return new LoginManager(
            c.Resolve<IRegionManager>(),
            c.Resolve<IEventAggregator>(),
            ConfigurationManager.AppSettings["MainRegion"],
            new Uri("Login"),
            new Uri("Target"));
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a Generic type G In my class model
I have a class that has some properties. And I want something that calculates
I have a class that has a vector of another class objects as a
Let's say I have a class that has a member called data which is
I have a base class that has a private static member: class Base {
I have an entity class that has a property with an underlying db column
Let's say I have one class Foo that has a bunch of logic in
I have a page base class that has no .aspx file and so I
I have a base class Foo that has an Update() function, which I want
You have a method on a class that has 2 parameters, one of which

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.