How do I get access to the ISettingService in my plugin's DependencyRegistrar class?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
I have a couple of related questions about retrieving data from the nop database inside the my plugin's DependencyRegistrar class's Register method.

Firstly...

In my plugin, in DependencyRegistrar.Register, I need access to the SettingsService to retrieve a couple of configuration values to then pass into the constructor of a dependency I'm registering, for example:

builder.Register(d => new MyClass(configValue1, configValue2)).InstancePerLifetimeScope();


Where configValue1 and configValue2 would be values stored in the nop Settings table (and thus retrived via the SettingsService).

Secondly...

My plugin has its own database tables. Another dependency that I need to register will need some values from these tables supplied in its constructor. Is it possible in DependencyRegistrar.Register to get access to my plugin's own DbContext or IRepository objects to query these plugin specific tables.


Thanks,
Kevin
5 years ago
kevsterer wrote:
I have a couple of related questions about retrieving data from the nop database inside the my plugin's DependencyRegistrar class's Register method.

Firstly...

In my plugin, in DependencyRegistrar.Register, I need access to the SettingsService to retrieve a couple of configuration values to then pass into the constructor of a dependency I'm registering, for example:

builder.Register(d => new MyClass(configValue1, configValue2)).InstancePerLifetimeScope();


Where configValue1 and configValue2 would be values stored in the nop Settings table (and thus retrived via the SettingsService).

Secondly...

My plugin has its own database tables. Another dependency that I need to register will need some values from these tables supplied in its constructor. Is it possible in DependencyRegistrar.Register to get access to my plugin's own DbContext or IRepository objects to query these plugin specific tables.


Thanks,
Kevin


for your first question you can follow this.

for second part you can look at the developers documentation, plugin with data access part.

You can either use your dBContext or make queries from your service.

Keep us updated if you have any further questions abt it. It can be frustrating at the beginning :)
5 years ago

            builder.Register<YourService>(c =>
            {

                var settingService = c.Resolve<ISettingService>();

                return new YourService(blablabla);

            });
5 years ago
timmit wrote:

            builder.Register<YourService>(c =>
            {

                var settingService = c.Resolve<ISettingService>();

                return new YourService(blablabla);

            });


Thanks Timmit that's exactly what I was looking for.
5 years ago
dianoche wrote:


for your first question you can follow this.

for second part you can look at the developers documentation, plugin with data access part.

You can either use your dBContext or make queries from your service.

Keep us updated if you have any further questions abt it. It can be frustrating at the beginning :)


Hi dianoche,

Thanks for taking the time to answer.

For my first question @timmit's answer was spot on the money. Though the nugget of info about setting the Order property to influence the running order was definitely useful (see below).

For my second question...as I mentioned, my plugin already has its own dBContext etc (created as per the article you linked to). I'm just trying to access an instance of that object (or the IRepository's I'm registering - after they've been registered of course) inside my DependencyRegistrar.Register() method so I can read data from my plugin's own custom tables.

Thinking about this a bit more I might try creating two IDependencyRegistrar classes. The first one where I set the Order property 1 and in there I register my IRepository's. In the second I set the Order property to 1000 and in that one I register the classes that are dependent on values from my custom tables, perhaps using the c.Resolve<MyDbTable>() technique as per timmit's answer.


> It can be frustrating at the beginning

Yes, there's a definitely a bit of a steep learning curve at the beginning, but I feel I'm winning now!  :)

Thanks,
Kevin
5 years ago
Why don't you use like;



public partial class YourService : IYourService
    {
        private readonly IRepository<YourTable> _yourTableRepository;


        public YourService(IRepository<YourTable> yourTableRepository)
        {
            _yourTableRepository = yourTableRepository;

        }
        //Add whatever method you want to use here
    }
5 years ago
(Apologies kevsterer here, I have a separate work and personal accounts on nop and signed in with the wrong one)

dianoche wrote:
Why don't you use like;


public partial class YourService : IYourService
    {
        private readonly IRepository<YourTable> _yourTableRepository;


        public YourService(IRepository<YourTable> yourTableRepository)
        {
            _yourTableRepository = yourTableRepository;

        }
        //Add whatever method you want to use here
    }


Because as I need access to an IRepository<MyTable> inside the DependencyRegistrar.Register() method, just after its been registered, I know how to inject into the other classes in my plugin. After DependencyRegistrar.Register() has run.
5 years ago
dianoche hasn't the faintest clue what you're asking. And you're approaching this problem the wrong way. This is what you need to do -



public class ConfigurationProvider
{
    IDbContext dbContext;

    public ConfigurationProvider(IDbConext dbContext)
    {
        this.dbContext = dbContext;
    }

    public MyConfig GetConfigurationValues()
    {
        //get required stuff from database
        var xyz = dbContext.SqlQuery(whatever);

        return new MyConfig(xyz);

    }

}

...

builder.Register<ConfigurationProvider>().WithParameter(ResolvedParameter.ForNamed<IDbContext>("your_context_name"));

builder.Register<SomeServiceWhichNeedsConfigurationValuesFromDatabase>(c =>
{

    var configurationProvider = c.Resolve<ConfigurationProvider>();
    
    var config = configurationProvider.GetConfigurationValues();

    return new YourClass(config);

});
5 years ago
timmit wrote:
dianoche hasn't the faintest clue what you're asking. And you're approaching this problem the wrong way. This is what you need to do -



public class ConfigurationProvider
{
    IDbContext dbContext;

    public ConfigurationProvider(IDbConext dbContext)
    {
        this.dbContext = dbContext;
    }

    public MyConfig GetConfigurationValues()
    {
        //get required stuff from database
        var xyz = dbContext.SqlQuery(whatever);

        return new MyConfig(xyz);

    }

}

...

builder.Register<ConfigurationProvider>().WithParameter(ResolvedParameter.ForNamed<IDbContext>("your_context_name"));

builder.Register<SomeServiceWhichNeedsConfigurationValuesFromDatabase>(c =>
{

    var configurationProvider = c.Resolve<ConfigurationProvider>();
    
    var config = configurationProvider.GetConfigurationValues();

    return new YourClass(config);

});



Yet again you've been tremendously helpful Timmit. I'm still getting my head around some of the Autofac DI stuff, however both your answers have lit up some bulbs and explained away some "magic".

Much appreciated,
Kevin
4 years ago
timmit wrote:

            builder.Register<YourService>(c =>
            {

                var settingService = c.Resolve<ISettingService>();

                return new YourService(blablabla);

            });


God bless you man, you solved my serious problem.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.