From 592d74f9d8c1dadf71ba8805998ec1c7302f924f Mon Sep 17 00:00:00 2001 From: "Matthew D. Groves" Date: Sun, 6 Mar 2011 13:46:04 -0500 Subject: [PATCH] refactored ioc/service locator to hopefully improve clarity/readability --- MonoStockPortfolio/Framework/IoCAttribute.cs | 30 +++++++++++++------ .../Framework/ServiceLocator.cs | 3 ++ .../Presenters/MainPresenter.cs | 1 - 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/MonoStockPortfolio/Framework/IoCAttribute.cs b/MonoStockPortfolio/Framework/IoCAttribute.cs index 0e15824..9d810df 100644 --- a/MonoStockPortfolio/Framework/IoCAttribute.cs +++ b/MonoStockPortfolio/Framework/IoCAttribute.cs @@ -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; + } + } } } \ No newline at end of file diff --git a/MonoStockPortfolio/Framework/ServiceLocator.cs b/MonoStockPortfolio/Framework/ServiceLocator.cs index 9b51220..5aa1990 100644 --- a/MonoStockPortfolio/Framework/ServiceLocator.cs +++ b/MonoStockPortfolio/Framework/ServiceLocator.cs @@ -14,7 +14,10 @@ namespace MonoStockPortfolio.Framework static ServiceLocator() { + // presenters IttyBittyIoC.Register(() => new MainPresenter(new AndroidSqlitePortfolioRepository(Context))); + + // services/repositories IttyBittyIoC.Register(() => new YahooStockDataProvider()); IttyBittyIoC.Register(() => new PortfolioService(new AndroidSqlitePortfolioRepository(Context), new YahooStockDataProvider())); IttyBittyIoC.Register(() => new AndroidSqlitePortfolioRepository(Context)); diff --git a/MonoStockPortfolio/Presenters/MainPresenter.cs b/MonoStockPortfolio/Presenters/MainPresenter.cs index 4f220ab..1ffb620 100644 --- a/MonoStockPortfolio/Presenters/MainPresenter.cs +++ b/MonoStockPortfolio/Presenters/MainPresenter.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Android.Util; using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Entities; using System.Linq;