Base solution for your next web application
Open Closed

Ioc and Inherit form <T> #1873


User avatar
0
andmattia created

Hi

I try to create a class T where I need to use a type T

public class ExcelExporter<T> : EpPlusExcelExporterBase, ITransientDependency
{
...
}

 public abstract class EpPlusExcelExporterBase
    {
        private ILocalizationSource _localizationSource;
        public IAppFolders AppFolders { get; set; }
        public ILocalizationManager LocalizationManager { get; set; }
        ...
}

 public class CustomerExcelExporter : ITransientDependency, ICustomerExcelExporter
    {
        private ILocalizationSource _localizationSource;
        public IAppFolders AppFolders { get; set; }
        public ILocalizationManager LocalizationManager { get; set; }
        public CustomerExcelExporter()
        {
            
        }
...
}

If I try to access to IAppFolder in base class it's empty I need to fill "manualy" in this way

var a = new ExcelExporter<Customer>(proj.LocalizationSourceName,
                appFolders:this.AppFolders,localizationManager:this.LocalizationManager)
            {
                OutputFileName = "Excel",
                OutputFileExtension = "xlsx",
                WorkSheetName = "Customer"
            };

It's work but I think i miss something or something is wrong is it correct?


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You are right, you shouldn't pass those variables. It must be done via dependency injection if it is possible. If you create a new instance of ExcelExporter, then dependency injection cannot pass those variables.

    Where do you create ExcelExporter ?

    var a = new ExcelExporter<Customer>(proj.LocalizationSourceName,
        appFolders:this.AppFolders,localizationManager:this.LocalizationManager)
    {
        OutputFileName = "Excel",
        OutputFileExtension = "xlsx",
        WorkSheetName = "Customer"
    };
    

    is it in CustomerExcelExporter class ?