Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# design question

Tags:

c#

I am just confused on the approach. Pls suggest me which is best. I will be creating multiple reports. SalesReport, ProfitReport etc.

Approach - 1:

class Report
{
  ReportType type;
}

Subclass ReportType as SalesType, ProfitType and assign it to report instances

SalesReport:

Report sales = new Report();
sales.type = new SalesType();

ProfitReport:

Report profit = new Report();
profit.type = new ProfitType();

Approach 2:

class Report
{
}

class SalesReport : Report
{
SalesType type;
}

class ProfitReport : Report
{
ProfitType type;
}

Which approach is best? and best design? Thanks a lot.

Every report will have different criterias, different output options like Email, print etc.

class Report
{
  ReportType type;
  Criteria criteria;
  Output output;
}

These classes are used like a Entity classes. For e.g from a browser/client we'll get the xml <Report><Type>Sales</Type><Criteria>...</Criteria></Report>. Based on the XML I need to form the Report classes and pass it for processing. Based on report type it will be executed.

like image 628
jaks Avatar asked Feb 02 '26 14:02

jaks


1 Answers

Are you going to be treating SalesReports and ProfitReports as Reports in any polymorphic ways?

If not, don't even have them inherit from a base class.

Is there much/any shared logic between a Sales report vs. a Profit report?

Inheritance for code reuse is an anti-pattern. If you have shared logic, consider composition.

Composition vs. Inheritance

Skip to the "When to use Inheritance and when to use Composition?" part first. It's a Java based article, but the concepts are pretty universal for static OO languages.

like image 105
Andy_Vulhop Avatar answered Feb 05 '26 03:02

Andy_Vulhop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!