mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-12-25 03:00:21 +00:00
refactored TinyIoC stuff into ServiceLocator class
This commit is contained in:
parent
bca11551f5
commit
c851ea9516
3 changed files with 40 additions and 55 deletions
|
@ -1,78 +1,26 @@
|
|||
using Android.Content;
|
||||
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||
using MonoStockPortfolio.Core.Services;
|
||||
using MonoStockPortfolio.Core.StockData;
|
||||
using PostSharp.Aspects;
|
||||
using TinyIoC;
|
||||
|
||||
namespace MonoStockPortfolio.Framework
|
||||
{
|
||||
public class IoCAttribute : LocationInterceptionAspect
|
||||
{
|
||||
private static Context _context;
|
||||
private static TinyIoCContainer _container;
|
||||
|
||||
public override void OnGetValue(LocationInterceptionArgs args)
|
||||
{
|
||||
if(_context == null)
|
||||
if (ServiceLocator.Context == null)
|
||||
{
|
||||
var activityContext= (Context)args.Instance;
|
||||
_context = activityContext.ApplicationContext.ApplicationContext;
|
||||
}
|
||||
|
||||
if (_container == null)
|
||||
{
|
||||
_container = RegisterTypes();
|
||||
ServiceLocator.Context = activityContext.ApplicationContext.ApplicationContext;
|
||||
}
|
||||
|
||||
var locationType = args.Location.LocationType;
|
||||
var instantiation = ServiceLocator.Get(locationType);
|
||||
|
||||
var instantiation = _container.Resolve(locationType);
|
||||
if(instantiation != null)
|
||||
{
|
||||
args.SetNewValue(instantiation);
|
||||
}
|
||||
args.ProceedGetValue();
|
||||
}
|
||||
|
||||
private 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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,6 +67,7 @@
|
|||
<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" />
|
||||
|
|
Loading…
Reference in a new issue