Search code examples
c#aspnetboilerplateabp-framework

ASP Boilerplate - How does ITransient work


From what I understand, in abp, when a class implements, ITransient interface, it is automatically registered in the dependency injection system.

When I create a new project in ASPNetZero, and a class implements the ITransient, I cannot inject the said class in other projects e.g Application

Using the following snippet does not allow me to use constructor injection.

public interface ITrackAppService : ITransientDependency

public class TrackAppService : ITrackAppService

But when I register it (Even if the class does not implements ITransient), then I can use constructor injection.

IocManager.RegisterIfNot<ITrack, Track>();

Did I mistakenly understood how ITransient works? How do I use Itransient so I can use constructor dependency injection?

Note: The class I'm trying to inject to the Application project is in a different project I created.


Solution

  • If you are injecting an interface to a new project, you cannot use it that way out of the box. Because your new project doesn't know your dependencies. Each new project that uses DI must to be set as an AbpModule.

    See a sample module declaration.

    [DependsOn(typeof(MyBlogCoreModule))]
    public class MyBlogApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
    

    Look out the [DependsOn] attribute on the class. This helps to register the project to the DI.

    So what you need to do is,

    1. Create a new class in the new project like I showed you above.
    2. Add the [DependsOn(typeof(YourApplicationServiceModule))] attribute to this new module.