logo
down
shadow

Yii2 DI container: registering same class twice, but with different constructor parameters


Yii2 DI container: registering same class twice, but with different constructor parameters

By : Yosoy Soyyo
Date : November 21 2020, 04:01 AM
With these it helps You should use aliases for components in the Dependency Injection Container. For example:
Add a component to DI container:
code :
Yii::$container->set(
    'component1',
    [
        'class' => '\common\components\ElasticSearchClient'
    ],
    [$elasticSearchServer1Params]    
);
$component = Yii::container->get('component1');
$someObject = new SomeClass1($component);
// add the component to the container
Yii::$container->set(
    'common\components\ElasticSearchClient',
);

// get the component from container and configure it
Yii::$container->get('common\components\ElasticSearchClient', 
                     [], 
                     [$elasticSearchServer1Params]
);
$someObject = new SomeClass1($component);
$container->set('SomeClass1', function ($container, $params, $config) {
    $searchClient = $contaier->get('common\components\ElasticSearchClient',
                                   [], 
                                   [$elasticSearchServer1Params]
    );
    return new SomeClass1($searchClient);
});

$container->set('SomeClass2', function ($container, $params, $config) {
    $searchClient = $contaier->get('common\components\ElasticSearchClient',
                                   [], 
                                   [$elasticSearchServer2Params]
    );
    return new SomeClass2($searchClient);;
});


Share : facebook icon twitter icon
Registering Class to Ninject and injecting that class in Constructor

Registering Class to Ninject and injecting that class in Constructor


By : user2473736
Date : March 29 2020, 07:55 AM
like below fixes the issue you can bind ISession to a Provider. In this case you can change the BootStrapper class to inherit from Provider and implement the CreateInstance method.
That would look like that:
code :
public class BootStrapper : Provider<ISession>
    {
        .....
        protected override ISession CreateInstance(IContext context)
        {
            return GetSession();
        }
        ....
    }
public class DaisyBlossomsServices : NinjectModule
    {
        public override void Load()
        {
            Bind<IProductsRepository>().To<MySqlProductsRepository>();
            Bind<ISession>().ToProvider<BootStrapper>();
        }
    } 
Specify lifetime when registering services in LightInject while passing constructor parameters?

Specify lifetime when registering services in LightInject while passing constructor parameters?


By : Himanshu Verma
Date : March 29 2020, 07:55 AM
will be helpful for those in need Using LightInject, lets say that I have a service that I want registered with multiple constructor parameters, example: , You can register it something like this.
code :
container.Register<IFoo<String>>(factory => new Foo<String>(arg1, arg2, arg3), new PerRequestLifeTime());
Yii2: Registering Asset Bundle vs registering external Js file

Yii2: Registering Asset Bundle vs registering external Js file


By : user3077866
Date : March 29 2020, 07:55 AM
hope this fix your issue One of the main reasons for using an Asset Bundle is that your assets' paths will always be correct. Consider:
code :
$this->registerJsFile('js/myjsfile.js', ['position'=>$this::POS_READY]);
<script src="js/myjsfile.js"></script>
$this->registerJsFile(Yii::$app->request->baseUrl.'/js/myjsfile.js', ['position'=>$this::POS_READY]);
StructureMap: How to send the container to a class that has a Constructor that does not accept Parameters

StructureMap: How to send the container to a class that has a Constructor that does not accept Parameters


By : Bala Sudhakar
Date : March 29 2020, 07:55 AM
should help you out It turns out that this question has been asked before on MSDN, the answer of which was provided by Travis Spencer in 2 different posts.
code :
RequestSecurityTokenResponse Issue(IClaimsPrincipal principal, RequestSecurityToken request)
{
    SecurityTokenServiceConfiguration config = new MyGoodSecurityTokenServiceConfiguration();
    SecurityTokenService sts = config.CreateSecurityTokenService();
    RequestSecurityTokenResponse rstr = sts.Issue(principal, request);

    return rstr;
}

public class MyGoodSecurityTokenServiceConfiguration : SecurityTokenServiceConfiguration
{
    public override SecurityTokenService CreateSecurityTokenService()
    {
        IssuerNameRegistry = IoC.Resolve<IssuerNameRegistry>();

        var sts = IoC.Reslove<SecurityTokenService>();

        return sts;
    }
}
Registering a class with ioc with constructor with param (concrete class no interface)

Registering a class with ioc with constructor with param (concrete class no interface)


By : user2253287
Date : March 29 2020, 07:55 AM
this will help I have looked at the dryIoc documentation and for whatever the reason.
code :
container.RegisterDelegate<ISampleClass>(
    _ => new SampleClass(mainActivity),
    Reuse.Singleton);
container.Register<ISampleClass, SampleClass>(
    Reuse.Singleton,
    made: Parameters.Of.Type<Activity>(_ => mainActivity));
Related Posts Related Posts :
  • change the link color if a post has been read wordpress
  • How do you customize variables in Laravel default emails?
  • Extract content of specific div class php dom
  • Showing results of a database query inside Navigation Tab
  • Return many object in my json response using resource
  • How to upload an image to Azure storage through PHP?
  • PHP - DOMDocument scrap divs dont remove images
  • Adding to functions.php in child theme doesn't work
  • Regular Expression to match markdown and regular href sources from specific domain(s)
  • How to manipulate and collate data in an associate array
  • How do I check an array for an entry that contains HTML characters?
  • Select current ( week or any given week ) records excluding week ends and holidays
  • How do I find the shipping class id in WooCommerce?
  • Make a select field disabled with a preselected value in Woocommerce
  • How to use the querybuilder in doctrine to test whether or not atleast one of two given values is already set in the dat
  • Laravel File Privacy
  • PHP read TXT file from a special character
  • Select first 10 rows and display into a slideshow
  • PHPMailer autoload.php not able to open
  • PHP - Create GUID from ID
  • PHP variable causing my SQL query to fail, but hard-coding the same values into the query works Why is this?.
  • generate a random password and update the database
  • DELETE to /oauth/personal-access-tokens/token not supported
  • Forget not modifying the original collection Laravel 5.7
  • Print a sequence of letters
  • Is there any alternative for asset and URL in Laravel?
  • PHP - how to link other pages in view in ci
  • Cannot Access Laravel Storage Directory
  • Find and replace value in system.cfg php
  • Laravel : How to retrieve data from inside an nested array
  • Codeigniter search with comma-separated values
  • upload file on laravel 5.7
  • Wordpress - How to display 10 users only?
  • Display the results in the HTML
  • PHP - mysqli_fetch_assoc, 2 results then into an array
  • mysqli update database receiving email and password reset token
  • PHP mailer not creating new lines
  • IF / OR statement causes Call to undefined function() error. Why?
  • How to read Protocol Buffers delimited I/O functions
  • Creating a link from Google Chart timeline item
  • Rewrite PHP variables from URL with .htaccess
  • Undesired output for php array creation
  • Symfony 4 - custom authentication FosUserBundle
  • laravel: cant push object property to array
  • When sharing on facebook, showing angular brackets in title and decription
  • Undefined function: 7 ERROR: operator does not exist: public.ltree public.< public.ltree[]
  • Generate a CSV in php from Array or Object
  • LibreOffice convert PDF to Word as textbox instead normal document
  • php laravel swift email sending problems
  • Reload PHP file in command line PHP-CLI
  • how do I fetch a certain part out of an database?
  • CodeIgniter URL Route with ID and title
  • Add a header to each column
  • How can I loop stdClass in PHP?
  • Four arrays in for each loop
  • bootstrap 4 cards with php foreach loop
  • Codeigniter return not found on the server
  • PHP Codeigniter Do Upload Function Does Not Work
  • Implement REST API Symfony 3
  • how to add show data button in Yajrabox
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org