mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2025-01-12 11:15:47 +00:00
working with PostSharp/IoC stuff
This commit is contained in:
parent
7eb5623d25
commit
8686848d3e
6 changed files with 72 additions and 11 deletions
|
@ -16,11 +16,6 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
||||||
private const int DATABASE_VERSION = 1;
|
private const int DATABASE_VERSION = 1;
|
||||||
private const string POSITION_TABLE_NAME = "Positions";
|
private const string POSITION_TABLE_NAME = "Positions";
|
||||||
|
|
||||||
public AndroidSqlitePortfolioRepository(SQLiteDatabase db)
|
|
||||||
{
|
|
||||||
_db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AndroidSqlitePortfolioRepository(Context context)
|
public AndroidSqlitePortfolioRepository(Context context)
|
||||||
{
|
{
|
||||||
_dbHelper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
_dbHelper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||||
|
|
55
MonoStockPortfolio/IoCAttribute.cs
Normal file
55
MonoStockPortfolio/IoCAttribute.cs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Android.Content;
|
||||||
|
using MonoStockPortfolio.Core.PortfolioRepositories;
|
||||||
|
using MonoStockPortfolio.Core.Services;
|
||||||
|
using PostSharp.Aspects;
|
||||||
|
|
||||||
|
namespace MonoStockPortfolio
|
||||||
|
{
|
||||||
|
public class IoCAttribute : LocationInterceptionAspect
|
||||||
|
{
|
||||||
|
private static Context _context;
|
||||||
|
|
||||||
|
public override void OnGetValue(LocationInterceptionArgs args)
|
||||||
|
{
|
||||||
|
if(_context != null) _context = (Context)args.Instance;
|
||||||
|
|
||||||
|
var locationType = args.Location.LocationType;
|
||||||
|
var instantiation = GetInstance(locationType);
|
||||||
|
if(instantiation != null)
|
||||||
|
{
|
||||||
|
args.SetNewValue(instantiation);
|
||||||
|
}
|
||||||
|
args.ProceedGetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object GetInstance(Type locationType)
|
||||||
|
{
|
||||||
|
if (DependencyMap.ContainsKey(locationType))
|
||||||
|
{
|
||||||
|
return DependencyMap[locationType]();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IDictionary<Type, Func<object>> DependencyMap
|
||||||
|
{
|
||||||
|
get { return _dependencyMap ?? (_dependencyMap = DefaultDependencies()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IDictionary<Type, Func<object>> _dependencyMap;
|
||||||
|
private static 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,18 +16,16 @@ namespace MonoStockPortfolio
|
||||||
{
|
{
|
||||||
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
|
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
|
||||||
|
|
||||||
private IPortfolioService _svc;
|
[IoC] private IPortfolioService _svc;
|
||||||
|
[IoC] private IPortfolioRepository _repo;
|
||||||
|
|
||||||
private IList<Portfolio> _portfolios;
|
private IList<Portfolio> _portfolios;
|
||||||
private string[] _longClickOptions;
|
private string[] _longClickOptions;
|
||||||
private IPortfolioRepository _repo;
|
|
||||||
|
|
||||||
protected override void OnCreate(Bundle bundle)
|
protected override void OnCreate(Bundle bundle)
|
||||||
{
|
{
|
||||||
base.OnCreate(bundle);
|
base.OnCreate(bundle);
|
||||||
|
|
||||||
_svc = new PortfolioService(this);
|
|
||||||
_repo = new AndroidSqlitePortfolioRepository(this);
|
|
||||||
|
|
||||||
SetContentView(Resource.layout.main);
|
SetContentView(Resource.layout.main);
|
||||||
|
|
||||||
RefreshList();
|
RefreshList();
|
||||||
|
@ -67,7 +65,6 @@ namespace MonoStockPortfolio
|
||||||
|
|
||||||
private void tr_LongClick_Options(object sender, DialogClickEventArgs e, Portfolio selectedPortfolio)
|
private void tr_LongClick_Options(object sender, DialogClickEventArgs e, Portfolio selectedPortfolio)
|
||||||
{
|
{
|
||||||
//Toast.MakeText(this, "Option: " + _longClickOptions[e.Which], ToastLength.Long).Show();
|
|
||||||
if(_longClickOptions[e.Which] == "Edit")
|
if(_longClickOptions[e.Which] == "Edit")
|
||||||
{
|
{
|
||||||
// Edit
|
// Edit
|
||||||
|
|
|
@ -38,6 +38,10 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Reference Include="Mono.Android" />
|
<Reference Include="Mono.Android" />
|
||||||
<Reference Include="mscorlib" />
|
<Reference Include="mscorlib" />
|
||||||
|
<Reference Include="PostSharp.SL, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b13fd38b8f9c99d7, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
<HintPath>..\libs\PostSharp.SL.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
@ -45,6 +49,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="EditPortfolioActivity.cs" />
|
<Compile Include="EditPortfolioActivity.cs" />
|
||||||
|
<Compile Include="IoCAttribute.cs" />
|
||||||
<Compile Include="PortfolioActivity.cs" />
|
<Compile Include="PortfolioActivity.cs" />
|
||||||
<Compile Include="MainActivity.cs" />
|
<Compile Include="MainActivity.cs" />
|
||||||
<Compile Include="AddPositionActivity.cs" />
|
<Compile Include="AddPositionActivity.cs" />
|
||||||
|
@ -54,6 +59,9 @@
|
||||||
<Compile Include="Validation\FormValidator.cs" />
|
<Compile Include="Validation\FormValidator.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<None Include="PostSharp.Custom.targets">
|
||||||
|
<SubType>Designer</SubType>
|
||||||
|
</None>
|
||||||
<None Include="Resources\AboutResources.txt" />
|
<None Include="Resources\AboutResources.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
6
MonoStockPortfolio/PostSharp.Custom.targets
Normal file
6
MonoStockPortfolio/PostSharp.Custom.targets
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup>
|
||||||
|
<PostSharpAssemblyBindingPolicySet>Silverlight20</PostSharpAssemblyBindingPolicySet>
|
||||||
|
</PropertyGroup>
|
||||||
|
</Project>
|
BIN
libs/PostSharp.SL.dll
Normal file
BIN
libs/PostSharp.SL.dll
Normal file
Binary file not shown.
Loading…
Reference in a new issue