mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-12-24 12:03:02 +00:00
Merge branch 'use_a_real_ioc_container' into develop
This commit is contained in:
commit
b9a30b6d62
6 changed files with 2934 additions and 45 deletions
|
@ -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;
|
||||
|
|
|
@ -4,7 +4,6 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using System.Net;
|
||||
using Android.Util;
|
||||
using Android.Widget;
|
||||
using FileHelpers;
|
||||
using MonoStockPortfolio.Entities;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
36
MonoStockPortfolio/Framework/ServiceLocator.cs
Normal file
36
MonoStockPortfolio/Framework/ServiceLocator.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
2887
MonoStockPortfolio/Framework/TinyIoC.cs
Normal file
2887
MonoStockPortfolio/Framework/TinyIoC.cs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -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" />
|
||||
|
|
Loading…
Reference in a new issue