working with PostSharp/IoC stuff

This commit is contained in:
mgroves 2010-12-19 15:34:20 -05:00
parent 7eb5623d25
commit 8686848d3e
6 changed files with 72 additions and 11 deletions

View file

@ -16,11 +16,6 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
private const int DATABASE_VERSION = 1;
private const string POSITION_TABLE_NAME = "Positions";
public AndroidSqlitePortfolioRepository(SQLiteDatabase db)
{
_db = db;
}
public AndroidSqlitePortfolioRepository(Context context)
{
_dbHelper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

View 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;
}
}
}

View file

@ -16,18 +16,16 @@ namespace MonoStockPortfolio
{
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
private IPortfolioService _svc;
[IoC] private IPortfolioService _svc;
[IoC] private IPortfolioRepository _repo;
private IList<Portfolio> _portfolios;
private string[] _longClickOptions;
private IPortfolioRepository _repo;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_svc = new PortfolioService(this);
_repo = new AndroidSqlitePortfolioRepository(this);
SetContentView(Resource.layout.main);
RefreshList();
@ -67,7 +65,6 @@ namespace MonoStockPortfolio
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")
{
// Edit

View file

@ -38,6 +38,10 @@
<ItemGroup>
<Reference Include="Mono.Android" />
<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.Core" />
<Reference Include="System.Xml.Linq" />
@ -45,6 +49,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EditPortfolioActivity.cs" />
<Compile Include="IoCAttribute.cs" />
<Compile Include="PortfolioActivity.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="AddPositionActivity.cs" />
@ -54,6 +59,9 @@
<Compile Include="Validation\FormValidator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="PostSharp.Custom.targets">
<SubType>Designer</SubType>
</None>
<None Include="Resources\AboutResources.txt" />
</ItemGroup>
<ItemGroup>

View 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

Binary file not shown.