question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Checking my use of IoC in ABP with an ITransientDependency ....

See original GitHub issue
  • Abp 2.3.0

I want to make sure I have understood the use of ITransientDependency with how I have things implemented below:

public interface IOzCpShortCodeProcessorBase<out TProcessOutput> : ITransientDependency
{
}

public abstract class OzCpShortCodeProcessorBase<TProcessOutput> : IOzCpShortCodeProcessorBase<TProcessOutput> where TProcessOutput : new()
{
}

 public interface IOzCpShortCodeCruiseListProcessor : IOzCpShortCodeProcessorBase<OzCpProcessOutput>
{

} 

public class OzCpShortCodeCruiseListProcessor : OzCpShortCodeProcessorBase<OzCpProcessOutput>, IOzCpShortCodeCruiseListProcessor
{
}

public static class OzCpShortCodeHelper
{
    /// <summary>
    ///     Factory to spin up the appropriate processor class to deal with the short code in question.
    /// </summary>
    /// <param name="aShortCode">Short code to be processed.</param>
    /// <returns>Returns a concrete of OzCpShortCodeProcessorBase that can be used to process the short code.</returns>
    public static dynamic ProcessorFactory(OzCpShortCodeParseItem aShortCode)
    {
        if (!aShortCode.IsValid)
            throw new Exception("Short code was not correctly parsed.");

        if (aShortCode.Identifier.Trim().ToUpper() == "CRUISELIST")
            return IocManager.Instance.Resolve<IOzCpShortCodeCruiseListProcessor>();

        throw new Exception($"I don't know how to create a processor for {aShortCode.Identifier}");
    }
}

Ultimately from within an action method in an MVC application I make the following call to the factory method:

public virtual ActionResult Process()
{
    //Get a short code processor from the factory and then cast to a concrete
    dynamic shortCodeProcessor = OzCpShortCodeHelper.ProcessorFactory(shortCode);

    //-- Cruise List ---------------------------------------------------------------------
    if (shortCodeProcessor is OzCpShortCodeCruiseListProcessor)
    {
        OzCpShortCodeCruiseListProcessor shortCodeCruiseListProcessor = (OzCpShortCodeCruiseListProcessor) shortCodeProcessor;
        OzCpProcessOutput shortCodeCruiseListProcessOutput = shortCodeCruiseListProcessor.Process(aShortCode);
        TokenContentQueryBuilderCruiseListViewModel viewModel = PrepareTokenContentQueryBuilderCruiseListViewModel(new OzCpQueryBuilderKindDataCruiseList
                                                                                                                   {
                                                                                                                       CruiseCodes = shortCodeCruiseListProcessOutput.CruiseList.CruiseCodes
                                                                                                                   }, 1, 9999999);

        result = OzCpCmsHelper.RenderViewToString(_ControllerContext, MVC.Cms.Views._TokenContentQueryBuilderCruiseList, viewModel);
    }
    //-- /Cruise List ---------------------------------------------------------------------
}

As shortCodeProcessor is marked with ITransientDependency am I correct in assuming that it will be destroyed at the conclusion of the Process() method call? (I don’t explicitly have to dispose of it).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
acjhcommented, Nov 17, 2017

ASP.NET MVC 5

This is handled by an IControllerFactory.

ABP sets IControllerFactory in AbpWebMvcModule.

MVC system calls IControllerFactory.ReleaseController.

  • WindsorControllerFactory releases the controller.

ASP.NET Core

This is handled by an IControllerActivator.

ABP provides AddAbp extension to be called in Startup.

MVC system disposes IServiceProvider.

  • ScopedWindsorServiceProvider disposes IMsLifetimeScope .
  • MsLifeTimeScope releases _resolvedInstances.
1reaction
hikalkancommented, Nov 17, 2017

Thank you @acjh for your great explanation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency Injection | Documentation Center | ABP.IO
If you implement these interfaces, your class is registered to dependency injection automatically: ITransientDependency to register with transient lifetime.
Read more >
Dependency Injection
Registration informs the IOC (Inversion of Control) Container (a.k.a. the DI framework) about your classes, their dependencies and lifetimes. Somewhere in your ......
Read more >
Dependency Injection
With such a dependency, it's very hard (or impossible) to unit test the PersonAppService. ... You can use IocManager to register dependencies (generally...
Read more >
Basic configuration of ABP framework and Dependency ...
When your application needs to create objects using the IOC container, ASP. NET provides a number of ways to resolve dependencies. Constructors ...
Read more >
IShouldInitialize interface equivalent in ABP Framework
I search for an equivalent of the IShouldInitialize interface in ABP.IO framework. In ASP.NET Boilerplate, the interface marked the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found