Injection

Topics: Customizing Orchard, General, Troubleshooting, Writing modules
Aug 3, 2011 at 4:53 PM

Hello. Here example of code. SecurityTokenService - class in microsoft's assembly.

 
public class ICDSecurityTokenService : SecurityTokenService { protected IMembershipService MembershipService { get; private set; } protected IICDSTSSettingsService ICDSTSSettingsService { get; private set; } protected IRoleService RoleService { get; private set; } public ICDSecurityTokenService(SecurityTokenServiceConfiguration configuration) : base(configuration) { } public void InitProviders(IMembershipService membershipService, IICDSTSSettingsService icdstsSettingsService, IRoleService roleService) { this.MembershipService = membershipService; this.ICDSTSSettingsService = icdstsSettingsService; this.RoleService = roleService; } }
Is there way make injection of instances of IMembershipService IICDSTSSettingsService and IRoleService into this class through constructor or maybe another way?

Coordinator
Aug 3, 2011 at 6:45 PM

It looks like this question has absolutely nothing to do whatsoever with Orchard. You might want to ask this on a more relevant forum.

Aug 4, 2011 at 1:01 PM

Ok. Another question. How to get WorkContext or WorkContextAccessor in this class? Here piece of code from OrchardServiceHostFactory class

            // Create work context
            IRunningShellTable runningShellTable = HostContainer.Resolve<IRunningShellTable>();
            ShellSettings shellSettings = runningShellTable.Match(baseAddresses.First().Host, baseAddresses.First().LocalPath);

            IOrchardHost orchardHost = HostContainer.Resolve<IOrchardHost>();
            ShellContext shellContext = orchardHost.GetShellContext(shellSettings);
            IWorkContextAccessor workContextAccessor = shellContext.LifetimeScope.Resolve<IWorkContextAccessor>();
            WorkContext workContext = workContextAccessor.GetContext(); 

Can I do something like that in my custom classes? I need get IRoleService instance and IMembershipService instance. How can I do it?


Aug 4, 2011 at 5:21 PM
aandriichuk wrote:

Ok. Another question. How to get WorkContext or WorkContextAccessor in this class? Here piece of code from OrchardServiceHostFactory class

 

            // Create work context
            IRunningShellTable runningShellTable = HostContainer.Resolve<IRunningShellTable>();
            ShellSettings shellSettings = runningShellTable.Match(baseAddresses.First().Host, baseAddresses.First().LocalPath);

            IOrchardHost orchardHost = HostContainer.Resolve<IOrchardHost>();
            ShellContext shellContext = orchardHost.GetShellContext(shellSettings);
            IWorkContextAccessor workContextAccessor = shellContext.LifetimeScope.Resolve<IWorkContextAccessor>();
            WorkContext workContext = workContextAccessor.GetContext(); 

Can I do something like that in my custom classes? I need get IRoleService instance and IMembershipService instance. How can I do it?

 

 

 

If I will have WorkContext then I will able do following  IMembershipService membershipService = WorkContext.Resolve<IMembershipService>(); Is there another way to do it?

Coordinator
Aug 4, 2011 at 6:16 PM

It is much simpler than that. You can have a constructor parameter of type any interface that derives from IDependency, and it will get injected for you. There are literally hundreds of examples of that throughout the codebase.