Skip to content

Commit

Permalink
Revision of Dependency Injection patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Meyrelles committed Sep 7, 2012
1 parent 4235e92 commit 9c7855a
Show file tree
Hide file tree
Showing 18 changed files with 184 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public decimal? ExpectedValue
/// Campo opcional que indica o valor pago após juros e/ou descontos.
/// </summary>
[LocalizableColumnName]
[Ignore("Future Use")]
public decimal? ActualValue { get; set; }

public abstract decimal Value { get; }
Expand Down
2 changes: 0 additions & 2 deletions Solution2010/ModernCashFlow.Domain/Entities/EditStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ public enum EditStatus
/// </summary>
Complete,


//todo: maybe it's necessary to create a "partially complete" status
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ModernCashFlow.Excel2010.ApplicationCore
public class CommandHandler
{

internal static void Send<T>(Commands.CommandArgs commandArgs) where T : ICommand
internal static void Send<T>(CommandArgs commandArgs = null) where T : ICommand
{
NinjectContainer.Kernel.Get<T>().Execute(commandArgs);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using ModernCashFlow.Excel2010.WorksheetLogic;

namespace ModernCashFlow.Excel2010.ApplicationCore.Factories
{

/// <summary>
/// Abstract Factory for creating Worksheet logic objects. Meant to be used with Ninject Factory extension.
/// </summary>
public interface IExpenseWorksheetFactory
{
ExpenseWorksheet CreateWorksheet();
ExpenseWorksheet.ContextMenus CreateContextMenu();
ExpenseWorksheet.Events CreateEventHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ModernCashFlow.Excel2010.WorksheetLogic;

namespace ModernCashFlow.Excel2010.ApplicationCore.Factories
{
/// <summary>
/// Abstract Factory for creating Worksheet logic objects. Meant to be used with Ninject Factory extension.
/// </summary>
public interface IIncomeWorksheetFactory
{
ExpenseWorksheet CreateWorksheet();
ExpenseWorksheet.ContextMenus CreateContextMenu();
ExpenseWorksheet.Events CreateEventHandlers();
}
}
Binary file modified Solution2010/ModernCashFlow.Excel2010/CashFlow.xlsm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@

namespace ModernCashFlow.Excel2010.Commands
{
//todo: pass reference via constructor.
/// <summary>
/// Responsible to read the configuration stored in configuration worksheets. With this data,
/// the main application build validation lists, environment configuration and so on.
/// </summary>
public class InitializeBasicBusinessDependenciesCommand : ICommand
{
private readonly BaseController<Account> _accountController;
private readonly AccountWorksheet _accountWorksheet;


public class InitializeBasicDependenciesCommand : ICommand
{
[Inject]
public BaseController<Account> AccountController { get; set; }
public InitializeBasicBusinessDependenciesCommand(AccountWorksheet accountWorksheet, BaseController<Account> accountController)
{
_accountWorksheet = accountWorksheet;
_accountController = accountController;
}

public void Execute(CommandArgs args)
{
NinjectContainer.Kernel.Get<AccountWorksheet>().Start();
AccountController.GetLocalDataAndSyncronizeSession();
_accountWorksheet.Start();
_accountController.GetLocalDataAndSyncronizeSession();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ namespace ModernCashFlow.Excel2010.Commands
public class InitializeBusinessRulesCommand : ICommand
{


[Inject]
public BaseController<Expense> ExpenseController { get; set; }

[Inject]
public BaseController<Income> IncomeController { get; set; }

[Inject]
public BaseController<Account> AccountController { get; set; }
private readonly BaseController<Expense> _expenseController;
private readonly BaseController<Income> _incomeController;
private readonly BaseController<Account> _accountController;
private readonly ExpenseStatusService _paymentSvc;

public InitializeBusinessRulesCommand(BaseController<Expense> expenseController,
BaseController<Income> incomeController, BaseController<Account> accountController,
ExpenseStatusService paymentStatusService)
{
_expenseController = expenseController;
_incomeController = incomeController;
_accountController = accountController;
_paymentSvc = paymentStatusService;
}

public void Execute(CommandArgs args)
{
Expand All @@ -32,22 +37,21 @@ public void Execute(CommandArgs args)

private void LoadAllTransactions()
{
ExpenseController.GetLocalDataAndSyncronizeSession();
IncomeController.GetLocalDataAndSyncronizeSession();
_expenseController.GetLocalDataAndSyncronizeSession();
_incomeController.GetLocalDataAndSyncronizeSession();
}


private void ConvertTodayPaymentsToPending()
{
var paymentSvc = NinjectContainer.Kernel.Get<ExpenseStatusService>();
var todayPayments = paymentSvc.GetTodayPayments(ExpenseController.CurrentSessionData).ToList();
var todayPayments = _paymentSvc.GetTodayPayments(_expenseController.CurrentSessionData).ToList();
todayPayments.ForEach(x => x.Expense.TransactionStatus = TransactionStatus.Pending);
}

private void WriteAllTransactionsToWorsheets()
{
ExpenseController.RefreshAllLocalData();
IncomeController.RefreshAllLocalData();
_expenseController.RefreshAllLocalData();
_incomeController.RefreshAllLocalData();
}

private void ShowSplashWindow()
Expand All @@ -57,10 +61,10 @@ private void ShowSplashWindow()

private void ProcessTodayPayments()
{
var paymentSvc = NinjectContainer.Kernel.Get<ExpenseStatusService>();
var todayPayments = paymentSvc.GetTodayPayments(ExpenseController.CurrentSessionData).ToList();
var comingPayments = paymentSvc.GetComingPayments(ExpenseController.CurrentSessionData).ToList();
var latePayments = paymentSvc.GetLatePayments(ExpenseController.CurrentSessionData).ToList();

var todayPayments = _paymentSvc.GetTodayPayments(_expenseController.CurrentSessionData).ToList();
var comingPayments = _paymentSvc.GetComingPayments(_expenseController.CurrentSessionData).ToList();
var latePayments = _paymentSvc.GetLatePayments(_expenseController.CurrentSessionData).ToList();

var form = new FormPendingExpensesViewModel { TodayPayments = todayPayments, ComingPayments = comingPayments, LatePayments = latePayments };
form.ShowDialog();
Expand All @@ -71,7 +75,7 @@ private void ProcessTodayPayments()
processedPayments.AddRange(EditPendingExpenseDto.ToList(form.LatePayments, w => w.IsPaid == true));
processedPayments.AddRange(EditPendingExpenseDto.ToList(form.ComingPayments, w => w.IsPaid == true));

ExpenseController.AcceptDataCollection(processedPayments, true);
_expenseController.AcceptDataCollection(processedPayments, true);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@

namespace ModernCashFlow.Excel2010.Commands
{
public class InitializeMainWorkooksCommand : ICommand

/// <summary>
/// Responsible to initalize the main worksheets after their dependencies initialization.
/// </summary>
public class InitializeMainWorksheetsCommand : ICommand
{
[Inject]
public BaseController<Account> AccountController { get; set; }
private readonly IncomeWorksheet _incomeWorksheet;
private readonly ExpenseWorksheet _expenseWorksheet;

public InitializeMainWorksheetsCommand(IncomeWorksheet incomeWorksheet, ExpenseWorksheet expenseWorksheet)
{
_expenseWorksheet = expenseWorksheet;
_incomeWorksheet = incomeWorksheet;
}

public void Execute(CommandArgs args)
{
//initalize other worksheet helpers
NinjectContainer.Kernel.Get<IncomeWorksheet>().Start();
NinjectContainer.Kernel.Get<ExpenseWorksheet>().Start();
_incomeWorksheet.Start();
_expenseWorksheet.Start();
}
}

Expand Down
39 changes: 21 additions & 18 deletions Solution2010/ModernCashFlow.Excel2010/Expenses.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Office.Tools.Excel;
using ModernCashFlow.Excel2010.ApplicationCore.Factories;
using ModernCashFlow.Excel2010.WorksheetLogic;
using Ninject;
using Excel = Microsoft.Office.Interop.Excel;
Expand All @@ -8,50 +9,51 @@ namespace ModernCashFlow.Excel2010
{
public partial class Expenses
{
private ExpenseWorksheet _wksHelper;

private IExpenseWorksheetFactory _factory;


private void Expenses_Startup(object sender, System.EventArgs e)

private void ExpensesStartup(object sender, System.EventArgs e)
{
this.tblExpenses.Change += Expenses_Change;
this.tblExpenses.BeforeRightClick += Expenses_BeforeRightClick;
this.tblExpenses.SelectionChange += Expenses_SelectionChange;
this.ActivateEvent += Expenses_ActivateEvent;
this.tblExpenses.Change += ExpensesChange;
this.tblExpenses.BeforeRightClick += ExpensesBeforeRightClick;
this.tblExpenses.SelectionChange += ExpensesSelectionChange;


ThisWorkbook.NotifySheetLoaded(this);

//todo: review DI process for the factory itself.
_factory = NinjectContainer.Kernel.Get<IExpenseWorksheetFactory>();
}

void Expenses_SelectionChange(Excel.Range target)
void ExpensesSelectionChange(Excel.Range target)
{
var eventHandlers = NinjectContainer.Kernel.Get<ExpenseWorksheet.Events>();
var eventHandlers = _factory.CreateEventHandlers();
eventHandlers.OnSelectionChange(target);
}

void Expenses_BeforeRightClick(Excel.Range target, ref bool cancel)
void ExpensesBeforeRightClick(Excel.Range target, ref bool cancel)
{
Application.EnableEvents = false;

var popup = NinjectContainer.Kernel.Get<ExpenseWorksheet.ContextMenus>();
var popup = _factory.CreateContextMenu();
popup.ShowContextMenu(target, ref cancel);

Application.EnableEvents = true;
}

private void Expenses_Change(Excel.Range target, ListRanges changedRanges)
private void ExpensesChange(Excel.Range target, ListRanges changedRanges)
{
//todo: analisar se é preciso colocar try catch para manter os eventos da app ativos mesmo em caso de erro.
Application.EnableEvents = false;

var eventHandlers = NinjectContainer.Kernel.Get<ExpenseWorksheet.Events>();
var eventHandlers = _factory.CreateEventHandlers();
eventHandlers.OnChange(target, changedRanges);

Application.EnableEvents = true;
}

private void Expenses_ActivateEvent()
{

}

private void Expenses_Shutdown(object sender, System.EventArgs e)
{
}
Expand All @@ -64,13 +66,14 @@ private void Expenses_Shutdown(object sender, System.EventArgs e)
/// </summary>
private void InternalStartup()
{
this.Startup += Expenses_Startup;
this.Startup += ExpensesStartup;
this.Shutdown += Expenses_Shutdown;

}


#endregion


}
}
14 changes: 10 additions & 4 deletions Solution2010/ModernCashFlow.Excel2010/Incomes.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Excel;
using ModernCashFlow.Excel2010.ApplicationCore.Factories;
using ModernCashFlow.Excel2010.WorksheetLogic;
using Ninject;

namespace ModernCashFlow.Excel2010
{
public partial class Incomes
{

private IIncomeWorksheetFactory _factory;

private void Incomes_Startup(object sender, System.EventArgs e)
{
this.tblIncomes.Change += (this.tblIncomes_Change);
this.tblIncomes.BeforeRightClick += (tblIncomes_BeforeRightClick);
this.tblIncomes.SelectionChange += (tblIncomes_SelectionChange);

ThisWorkbook.NotifySheetLoaded(this);

//todo: review DI process for the factory itself.
_factory = NinjectContainer.Kernel.Get<IIncomeWorksheetFactory>();
}


void tblIncomes_SelectionChange(Range target)
{
var eventHandlers = NinjectContainer.Kernel.Get<IncomeWorksheet.Events>();
var eventHandlers = _factory.CreateEventHandlers();
eventHandlers.OnSelectionChange(target);

}

void tblIncomes_BeforeRightClick(Range target, ref bool cancel)
{
Application.EnableEvents = false;

var popup = NinjectContainer.Kernel.Get<IncomeWorksheet.ContextMenus>();
var popup = _factory.CreateContextMenu();
popup.ShowContextMenu(target, ref cancel);

Application.EnableEvents = true;
Expand All @@ -40,7 +46,7 @@ private void tblIncomes_Change(Range target, ListRanges changedRanges)
//todo: analisar se é preciso colocar try catch para manter os eventos da app ativos mesmo em caso de erro.
Application.EnableEvents = false;

var eventHandlers = NinjectContainer.Kernel.Get<IncomeWorksheet.Events>();
var eventHandlers = _factory.CreateEventHandlers();
eventHandlers.OnChange(target, changedRanges);

Application.EnableEvents = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
<ManifestCertificateThumbprint>6ABF3D24FDA35234FD755D46378F1924FD961114</ManifestCertificateThumbprint>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core">
<HintPath>..\packages\Castle.Core.3.0.0.4001\lib\net40-client\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Office.Interop.Excel, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
Expand All @@ -106,8 +109,12 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.Tools.Applications.Runtime, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Ninject">
<HintPath>..\Libraries\Ninject.dll</HintPath>
<Reference Include="Ninject, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Ninject.3.0.1.10\lib\net40\Ninject.dll</HintPath>
</Reference>
<Reference Include="Ninject.Extensions.Factory">
<HintPath>..\packages\Ninject.Extensions.Factory.3.0.1.0\lib\net40\Ninject.Extensions.Factory.dll</HintPath>
</Reference>
<Reference Include="Office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<EmbedInteropTypes>True</EmbedInteropTypes>
Expand Down Expand Up @@ -145,6 +152,8 @@
<Compile Include="ApplicationCore\CommandHandler.cs" />
<Compile Include="ApplicationCore\CommandManager.cs" />
<Compile Include="ApplicationCore\DataManager.cs" />
<Compile Include="ApplicationCore\Factories\IIncomeWorksheetFactory.cs" />
<Compile Include="ApplicationCore\Factories\IExpenseWorksheetFactory.cs" />
<Compile Include="ApplicationCore\IncomeController.cs" />
<Compile Include="ApplicationCore\ExpenseController.cs" />
<Compile Include="Commands\ConfigureSidePanelCommand.cs" />
Expand Down Expand Up @@ -288,6 +297,7 @@
<DependentUpon>Config.cs</DependentUpon>
</None>
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
Loading

0 comments on commit 9c7855a

Please sign in to comment.