logo
down
shadow

How can a static class be resolved by the Unity Framework?


How can a static class be resolved by the Unity Framework?

By : scarp1a
Date : January 02 2021, 06:48 AM
this one helps. Unity (or any IoC container framework) is basically a super-factory, creating objects and handing them to you. How is it supposed to create an instance of a static class?
Refactor your design to use a non-static class. If necessary create a class that wraps the static class and delegates to it.
code :


Share : facebook icon twitter icon
Unity Framework - passing integer's and strings into resolved objects

Unity Framework - passing integer's and strings into resolved objects


By : user1947423
Date : March 29 2020, 07:55 AM
hope this fix your issue is there a way using the Unity framework to pass an integer as an argument into the constructor or a resolved object? , Hope I understood you right
code :
   public class ConcreteService {

        public int Val { get; set; }

        public ConcreteService(int val) {
            Val = val;
        }
    }
        var container = new UnityContainer();

        container.RegisterType<ConcreteService>();
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>(new InjectionConstructor(1));

        container.RegisterType<ConcreteService>("for42");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for42",
                                                                                      new InjectionConstructor(42));
        container.RegisterType<ConcreteService>("for31");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for31",
                                                                                      new InjectionConstructor(31));

        Debug.WriteLine(container.Resolve<ConcreteService>().Val); //1
        Debug.WriteLine(container.Resolve<ConcreteService>("for42").Val); //42
        Debug.WriteLine(container.Resolve<ConcreteService>("for31").Val); //31
<type type="ConcreteService"  name="for42">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                     Microsoft.Practices.Unity.Configuration">
            <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
          </typeConfig>
        </type>
<type type="ConcreteService"  name="for42">
        <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
        </type>
Unity DI for static member (for example in static class program) / when do members get populated

Unity DI for static member (for example in static class program) / when do members get populated


By : Jbahngoura
Date : March 29 2020, 07:55 AM
wish of those help A few points:
You currently seem to completely configure the container in XML. Don't do that! You should only configure the dependencies that can actually change after building (deployment configuration) in XML. For the rest, use code based configuration, because XML based configuration is very brittle, lacks compile time support, lacks intellisense support, and lacks expressiveness. Don't use method injection to initialize components. This leads to Temporal Coupling. The main way of injecting dependencies is using constructor injection. Define all your dependencies on constructor parameters. All types that have dependencies should have a single public constructor that contain all the needed dependencies. Unity will automatically find that constructor and know how to invoke that constructor and inject the dependencies. Don't use attributes at all! This couples your code to the container used, while application code should be oblivious to the existence of the DI Container. Again, stick with constructor injection.
Implicitly injecting dependency in Base class while derived class is resolved through Unity

Implicitly injecting dependency in Base class while derived class is resolved through Unity


By : Josh Wiseman
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further No, unity is unable to do so. Actually, there's not a single container who can do so. A constructor is there to instantiate a class. If you call two constructors, you'll end up with two instances. If the base class is abstract, you couldn't even call its constructor (except derived constructors as you know).
So by limitations of C#.net, if you want to use constructor injection, it will only work if you explicitly inject the value into the Derived constructor which calls the non-default Base constructor.
code :
class Base
{
    [Dependency]
    public IDep Dep { get; set; }
} 
class Base
{
     private IDep dep;

     [InjectionMethod]
     public void Initialize(IDep dep)
     {
         this.dep = dep;
     }
}
Prism / Unity update resolved/injected references with new concrete class

Prism / Unity update resolved/injected references with new concrete class


By : Emiliano Capoccia
Date : March 29 2020, 07:55 AM
around this issue If you'll implement Proxy-class for IMyService which'll retrieve the actual implementation from ServiceLocator per each call the problem will be solved:
code :
public class MyServiceProxy : IMyService
{
    public int DoWork(string someParameter)
    {
        return ServiceLocator.Resolve<IMyService>().DoWork(someParameter);
    }
}
public class MyServiceChangedEvent : CompositeWpfEvent<IMyService>
{
}
var newMyService = new MyServiceB();
container.RegisterInstance<IMyService>(newMyService, new ContainerControlledLifetimeManager());
eventAggregator.GetEvent<MyServiceChangedEvent>().Publish(newMyService);
IUserStore[Models.ApplicationUser] is not resolved by Unity Interface to Class, but resolved with InjectionConstructor

IUserStore[Models.ApplicationUser] is not resolved by Unity Interface to Class, but resolved with InjectionConstructor


By : Vamjr Jun Jr.
Date : March 29 2020, 07:55 AM
will help you The reason for the InvalidOperationException is that Unity, by default, will select the constructor with the most number of arguments as the constructor to use to instantiate the object.
In this case that would be
code :
public AccountController(ApplicationUserManager userManager,
                         ApplicationSignInManager signInManager)
container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(); 
container.RegisterType(typeof(IUserStore<>), typeof(UserStore<>));
// Will resolve both concrete types
var userStore1 = container.Resolve<IUserStore<ApplicationUser>>();
var userStore2 = container.Resolve<IUserStore<OtherApplicationUser>>();
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk