Merge branch 'use_a_real_ioc_container' into develop

This commit is contained in:
mgroves 2011-01-02 17:27:44 -05:00
commit b9a30b6d62
6 changed files with 2934 additions and 45 deletions

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Content;
using Android.Util;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.StockData;
@ -14,13 +13,12 @@ namespace MonoStockPortfolio.Core.Services
private readonly IPortfolioRepository _portRepo;
private readonly IStockDataProvider _stockRepo;
public PortfolioService(Context context) : this(new AndroidSqlitePortfolioRepository(context),
new YahooStockDataProvider())
{
}
// public PortfolioService(Context context) : this(new AndroidSqlitePortfolioRepository(context),
// new YahooStockDataProvider())
// {
// }
public PortfolioService(IPortfolioRepository portfolioRepository,
IStockDataProvider stockDataProvider)
public PortfolioService(IPortfolioRepository portfolioRepository, IStockDataProvider stockDataProvider)
{
_portRepo = portfolioRepository;
_stockRepo = stockDataProvider;

View file

@ -4,7 +4,6 @@ using System.IO;
using System.Linq;
using System.Net;
using Android.Util;
using Android.Widget;
using FileHelpers;
using MonoStockPortfolio.Entities;

View file

@ -1,59 +1,26 @@
using System;
using System.Collections.Generic;
using Android.Content;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using PostSharp.Aspects;
namespace MonoStockPortfolio.Framework
{
public class IoCAttribute : LocationInterceptionAspect
{
private static Context _context;
public override void OnGetValue(LocationInterceptionArgs args)
{
if(_context == null)
if (ServiceLocator.Context == null)
{
var activityContext= (Context)args.Instance;
_context = activityContext.ApplicationContext.ApplicationContext;
ServiceLocator.Context = activityContext.ApplicationContext.ApplicationContext;
}
var locationType = args.Location.LocationType;
var instantiation = GetInstance(locationType);
var instantiation = ServiceLocator.Get(locationType);
if(instantiation != null)
{
args.SetNewValue(instantiation);
}
args.ProceedGetValue();
}
private object GetInstance(Type locationType)
{
if (DependencyMap.ContainsKey(locationType))
{
return DependencyMap[locationType]();
}
return null;
}
private IDictionary<Type, Func<object>> DependencyMap
{
get { return _dependencyMap ?? (_dependencyMap = DefaultDependencies()); }
}
private static IDictionary<Type, Func<object>> _dependencyMap;
private IDictionary<Type, Func<object>> DefaultDependencies()
{
var map = new Dictionary<Type, Func<object>>();
map.Add(typeof(IPortfolioService), () => new PortfolioService(_context));
map.Add(typeof(IPortfolioRepository), () => new AndroidSqlitePortfolioRepository(_context));
return map;
}
public void LoadDependencies(IDictionary<Type, Func<object>> dependencies)
{
_dependencyMap = dependencies;
}
}
}

View file

@ -0,0 +1,36 @@
using System;
using Android.Content;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Core.StockData;
using TinyIoC;
namespace MonoStockPortfolio.Framework
{
public static class ServiceLocator
{
public static Context Context { get; set; }
private static TinyIoCContainer _container;
public static object Get(Type serviceType)
{
if (_container == null)
{
_container = RegisterTypes();
}
return _container.Resolve(serviceType);
}
private static TinyIoCContainer RegisterTypes()
{
var container = TinyIoCContainer.Current;
container.Register<Context>(Context);
container.Register<IStockDataProvider, YahooStockDataProvider>().AsMultiInstance();
container.Register<IPortfolioService, PortfolioService>().AsMultiInstance();
container.Register<IPortfolioRepository, AndroidSqlitePortfolioRepository>().AsMultiInstance();
return container;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -67,6 +67,8 @@
<Compile Include="Activites\PortfolioActivity.designer.cs">
<DependentUpon>PortfolioActivity.cs</DependentUpon>
</Compile>
<Compile Include="Framework\ServiceLocator.cs" />
<Compile Include="Framework\TinyIoC.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Framework\StringExtensions.cs" />