My question is regarding correct coding and encapsulation:
I have structure of nested object lists. On the lowest level there are objects containing currency information (currency code such as “USD”, amount and due date).
In order to do calculations with those values I need exchange rates for the currency and for different due dates.
Here is what I have done (it is working):
Another object is initializing the nested structure and preparing a local (private) dataset containing the needed exchange rate table. Private function for exchange rate calculation that makes use of the private dataset is “injected” in every nested object that needs it and thus also “passing” the dataset into this object…
I have checked the memory usage and it seems that there is only one function / dataset in use.
See sample code down below (simplified and reduced).
So my question is this how it should/can be done or am I making some bad mistake that can cause vulnerability etc.?
Many thanks in advance for your kind advice
E.T.
/// <summary>
/// Provides report group value in target currency for particular date.
/// </summary>
public class ReportInstantValue
{
/// <summary>
/// Value calculation function passed via constructor on object creation.
/// </summary>
private Func<List<CurrencyValue>, double> _calculate { get; set; }
public DateTime Date { get; private set; }
public double Value
{
get
{
// Use the injected function.
return this._calculate(this.CurrencyValues);
}
}
public List<CurrencyValue> CurrencyValues { get; private set; }
public ReportInstantValue(Func<List<CurrencyValue>, double> calculate, DateTime date)
{
this._calculate = calculate;
this.Date = date;
this.CurrencyValues = new List<CurrencyValue>();
// Fill the currency values etc.
}
}
public class PrepareReportData
{
private DataSet _ds;
private NestedStructure _nested;
public PrepareReportData()
{
// Initialize dataset.
this._ds = new DataSet();
// ...
// Initialize nested structure
this._nested = new NestedStructure();
this._nested.ValuesList = new List<ReportInstantValue>();
foreach(//... Loop to fill the values list)
{
this._nested.ValuesList.Add
(
new ReportInstantValue
(
this.Calculate, // <= "inject" the local private calculation function...
// and so on ...
)
);
}
CreateReport report = new CreateReport();
report.ReportResult(this._nested);
}
/// <summary>
/// Value calculation routine for currency value.
/// <para>Will be injected in every ReportInstantValue object.</para>
/// </summary>
private double Calculate(List<ReportInstantValue> currencyValues)
{
double result = 0;
// Make usage of the local dataset.
foreach(ReportInstantValue value in currencyValues)
{
this._ds.Tables["ExchangeRate"].Select("...");
// ... and so on.
}
// ...
return result;
}
}
public class CreateReport
{
/// <summary>
/// Creates report from report data nested structure.
/// </summary>
public void ReportResult(NestedStructure structure)
{
foreach(ReportInstantValue current in structure.ValuesList)
{
double reportValue = current.Value; // <= call the injected function, uses the dataset defined private in the other class.
// add value to report...
}
}
}
Aucun commentaire:
Enregistrer un commentaire