none
HOw to get the class name implementin an interface RRS feed

  • Question

  • Dear all,

    I have a class which is implementing my interface IPrint

    IPrint is a printengine which print a crystal report file to PDF.
    The name of the crystal report file should be the name of the class which use the interface Iprint at runtime

    The way my class use the interface is by constructor injection using Prism Unity

    For instance if I have a class as bellow :

    myClass1(IPrint prt)

    {

      prt.PrintReport();

    }

    When the PrtReport is call I need to know nside the PrinReport method the name of the class "myClass" dynamically in order that O can output report file name myclass1.rpt

    Of course I could pass the class name hardcoded as a parameter to PrintReport metho but would like to avoid this

    Any idea how to do that ?

    regarsd

    Tuesday, May 22, 2018 7:19 AM

Answers

  • public abstract class PrintEngine : IPrintEngine
    { 
       public string ReportTitle 
       { 
          get => String.IsNullOrEmpty(_reportTitle) ? GetType().Name : _reportTitle;
          protected set => _reportTitle = value;
       }
    
       public bool PrintReportToPDF ( … ) { }
       public bool PrintReportToPrinter (…) { }
    
       public ISparkReportData BuildReportData ( … ) { }
    
       private string _reportTitle;
    }
    
    public class ExplicitTitle : PrintEngine
    {
       public ExplicitTitle ()
       {
          ReportTitle = "Special Title";
       }
    }
     
    
    public class ImplicitTitle : PrintEngine
    {
    }
    
    There are many variants as well. For example you could require any derived type to specify the title in the constructor (since it is unlikely to change) and the abstract class simply uses that or the type name if one isn't specified.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by wakefun Wednesday, May 23, 2018 2:32 PM
    Wednesday, May 23, 2018 1:41 PM
    Moderator

All replies

  • Try this:

       string name = prt.GetType().Name;


    Tuesday, May 22, 2018 7:56 AM
  • Why don't you want to define string ReportName {get; } in interface? Each class can define report name independently by class name.
    Tuesday, May 22, 2018 8:10 AM
  • Because it is not only a report name but a report file and to prevent user mistake I prefer to set the class name as file name  for report to avoid potential mistakes
    Tuesday, May 22, 2018 8:51 AM
  • I would advise against this route. Imagine that my report is called Bob's Monthly Invoice Report. You cannot represent that as a class name.

    If your own purpose is to prevent mistakes then there are other alternatives.

    1) Make the report name abstract such that derived types must define it. Are you preventing mistakes? No more or less than if I misname the class itself. Report20180405 and Report20180504 are both valid class/report names but only the author knows which one is correct.

    2) Have the default report name be the type name. Then allow a derived type to override it.

    We've gone this way in our report-heavy code and it is useful to have a strongly typed class to wrap a report but only for the parameters. The report is responsible for setting its own report name and it isn't changeable by the instances. Given the # of times we ever touch this code and the fact we test it when we write it the odds of something being typed wrong is all but non-existent.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 22, 2018 1:58 PM
    Moderator
  • I think if you do it there can be someone who do mistake in class name (i.e. rport.rpt). We don't have time to correct it now, we will correct it later. Ok. Then you found out it is not change class name only, you must change filename. As time go class can change that behavior is not in relation with class name. It is not class name change, you must change filename so. 

    As CoolDadTx mention you can create base abstract class with abstract properties which inherited class must implemented. Report name or report filename could be const so. I think if there will be mistake in filename it should be exposed by unit or integration test.

    Wednesday, May 23, 2018 3:35 AM
  • Hello CoolDadX,

    Thnaks for your advise.

    I would like to go to opetion 2 you mention but how do I implement abstract class in my interface ?

    Ad I am using dependency injection through my viewmodel constructor suing Prism unity, it gives me an error that I cannot implement abstract class implementing the interface .

    here is below my PrintEngine interface that is actually injected in my viewmodel constructor :

    public interface IPrintEngine
    	{
    		string ReportTitle { get; }
    		bool PrintReportToPDF(ISparkReportData Data);
    		bool PrintReportToPrinter(PrintingContext Ctx, ISparkReportData Data);
    
    		ISparkReportData BuildReportData(PrintingContext Ctx,Measure CurrentMeasure, ChartRenderInfo ChartInfo);
    	}

    Then it is passed as below :

    public MyViewModel(IPrintEngine PrintEngine)
    {
    .....
    }

    Any idea or did I miss something ?

    Wednesday, May 23, 2018 8:03 AM
  • public abstract class PrintEngine : IPrintEngine
    { 
       public string ReportTitle 
       { 
          get => String.IsNullOrEmpty(_reportTitle) ? GetType().Name : _reportTitle;
          protected set => _reportTitle = value;
       }
    
       public bool PrintReportToPDF ( … ) { }
       public bool PrintReportToPrinter (…) { }
    
       public ISparkReportData BuildReportData ( … ) { }
    
       private string _reportTitle;
    }
    
    public class ExplicitTitle : PrintEngine
    {
       public ExplicitTitle ()
       {
          ReportTitle = "Special Title";
       }
    }
     
    
    public class ImplicitTitle : PrintEngine
    {
    }
    
    There are many variants as well. For example you could require any derived type to specify the title in the constructor (since it is unlikely to change) and the abstract class simply uses that or the type name if one isn't specified.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by wakefun Wednesday, May 23, 2018 2:32 PM
    Wednesday, May 23, 2018 1:41 PM
    Moderator