mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-11-14 19:29:26 +00:00
refactored ioc/service locator to hopefully improve clarity/readability
This commit is contained in:
parent
e56a165011
commit
592d74f9d8
3 changed files with 24 additions and 10 deletions
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using Android.Content;
|
||||
using PostSharp.Aspects;
|
||||
|
||||
|
@ -7,17 +8,16 @@ namespace MonoStockPortfolio.Framework
|
|||
{
|
||||
public sealed override void OnGetValue(LocationInterceptionArgs args)
|
||||
{
|
||||
args.ProceedGetValue();
|
||||
if (args.Value == null)
|
||||
args.ProceedGetValue();
|
||||
if (args.Value == null) // lazy loading
|
||||
{
|
||||
if (ServiceLocator.Context == null)
|
||||
{
|
||||
var activityContext = (Context) args.Instance;
|
||||
ServiceLocator.Context = activityContext.ApplicationContext.ApplicationContext;
|
||||
}
|
||||
var context = args.Instance as Context;
|
||||
if(context == null) throw new Exception("The IoC Aspect can only be used on a field within an Activity (or Context) object");
|
||||
|
||||
var locationType = args.Location.LocationType;
|
||||
var instantiation = ServiceLocator.Get(locationType);
|
||||
ResolveContextDependency((Context)args.Instance);
|
||||
|
||||
var dependencyType = args.Location.LocationType;
|
||||
var instantiation = ServiceLocator.Get(dependencyType);
|
||||
|
||||
if (instantiation != null)
|
||||
{
|
||||
|
@ -26,5 +26,17 @@ namespace MonoStockPortfolio.Framework
|
|||
args.ProceedGetValue();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ResolveContextDependency(Context contextObject)
|
||||
{
|
||||
if (ServiceLocator.Context == null)
|
||||
{
|
||||
// note the double ApplicationContext
|
||||
// is because the context's reference could get garbage collected while the app is still goin
|
||||
// for whatever reason, but it's reference's reference is long-running
|
||||
// and since this context dependency is mainly used for Sqlite, that's the most ideal one
|
||||
ServiceLocator.Context = contextObject.ApplicationContext.ApplicationContext;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,7 +14,10 @@ namespace MonoStockPortfolio.Framework
|
|||
|
||||
static ServiceLocator()
|
||||
{
|
||||
// presenters
|
||||
IttyBittyIoC.Register<IMainPresenter>(() => new MainPresenter(new AndroidSqlitePortfolioRepository(Context)));
|
||||
|
||||
// services/repositories
|
||||
IttyBittyIoC.Register<IStockDataProvider>(() => new YahooStockDataProvider());
|
||||
IttyBittyIoC.Register<IPortfolioService>(() => new PortfolioService(new AndroidSqlitePortfolioRepository(Context), new YahooStockDataProvider()));
|
||||
IttyBittyIoC.Register<IPortfolioRepository>(() => new AndroidSqlitePortfolioRepository(Context));
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System.Collections.Generic;
|
||||
using Android.Util;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Entities;
|
||||
using System.Linq;
|
||||
|
|
Loading…
Reference in a new issue