Code gardening following Tree<>, Lazyload

This commit is contained in:
Rick Falkvinge
2015-02-22 15:43:43 +01:00
parent 0d21921f65
commit f983173c54
12 changed files with 68 additions and 148 deletions
@@ -3,7 +3,7 @@ using Swarmops.Common.Interfaces;
namespace Swarmops.Basic.Types.Financial
{
public class BasicFinancialAccount : IHasIdentity
public class BasicFinancialAccount : IHasIdentity, IHasParentIdentity
{
public BasicFinancialAccount (int financialAccountId, string name, FinancialAccountType accountType,
int organizationId, int parentFinancialAccountId, int ownerPersonId,
@@ -52,12 +52,10 @@ namespace Swarmops.Basic.Types.Financial
public int LinkBackward { protected get; set; }
public int LinkForward { protected get; set; }
#region IHasIdentity Members
#region Interface Members
public int Identity
{
get { return FinancialAccountId; }
}
public int Identity { get { return FinancialAccountId; } }
public int ParentIdentity { get { return ParentFinancialAccountId; } }
#endregion
}
+4 -6
View File
@@ -2,16 +2,14 @@ using Swarmops.Common.Interfaces;
namespace Swarmops.Basic.Types.Structure
{
public class BasicOrganization : IHasIdentity
public class BasicOrganization : IHasIdentity, IHasParentIdentity
{
private string nameShort;
#region IHasIdentity Members
#region Interface Members
public int Identity
{
get { return this.OrganizationId; }
}
public int Identity { get { return this.OrganizationId; } }
public int ParentIdentity { get { return this.ParentOrganizationId; } }
#endregion
+1 -1
View File
@@ -315,7 +315,7 @@ namespace Swarmops.Logic.DataObjects
Geographies line = Geography.FromIdentity (org.AnchorGeographyId).GetLine();
Geography[] arrLine = line.ToArray();
Array.Reverse (arrLine);
Countries countries = Countries.GetAll();
Countries countries = Countries.All;
int foundCntry = 1; //sweden
foreach (Geography geo in arrLine)
{
+1 -11
View File
@@ -2,14 +2,13 @@ using System;
using Swarmops.Basic.Types.Financial;
using Swarmops.Common.Enums;
using Swarmops.Database;
using Swarmops.Logic.Interfaces;
using Swarmops.Logic.Structure;
using Swarmops.Logic.Support;
using Swarmops.Logic.Swarm;
namespace Swarmops.Logic.Financial
{
public class FinancialAccount : BasicFinancialAccount, ITreeNode, IOwnerSettable
public class FinancialAccount : BasicFinancialAccount, IOwnerSettable
{
private FinancialAccount (BasicFinancialAccount basic)
: base (basic)
@@ -214,8 +213,6 @@ namespace Swarmops.Logic.Financial
}
}
#region ITreeNode Members
private ObjectOptionalData _optionalData;
public Geography AssignedGeography
@@ -269,18 +266,11 @@ namespace Swarmops.Logic.Financial
}
}
public int ParentIdentity
{
get { return ParentFinancialAccountId; }
}
public int[] ChildrenIdentities
{
get { return Children.Identities; }
}
#endregion
#region IOwnerSettable members
public void SetOwner (Person newOwner)
-30
View File
@@ -1,30 +0,0 @@
using System.Collections.Generic;
namespace Swarmops.Logic.Interfaces
{
internal interface ITreeNode
{
int Identity { get; }
int ParentIdentity { get; }
string Name { get; }
int[] ChildrenIdentities { get; }
}
public interface ITreeNodeObject
{
int Identity { get; }
int ParentIdentity { get; }
string Name { get; }
int[] ChildrenIdentities { get; }
ITreeNodeObject ParentObject { get; }
List<ITreeNodeObject> ChildObjects { get; }
}
}
-1
View File
@@ -202,7 +202,6 @@
<Compile Include="Financial\Salaries.cs" />
<Compile Include="Financial\Salary.cs" />
<Compile Include="Financial\TaxLevels.cs" />
<Compile Include="Interfaces\TreeNode.cs" />
<Compile Include="Financial\ExpenseClaim.cs" />
<Compile Include="Financial\FinancialAccount.cs" />
<Compile Include="Financial\FinancialAccountRow.cs" />
+21 -13
View File
@@ -2,12 +2,31 @@ using System.Collections.Generic;
using Swarmops.Basic.Types;
using Swarmops.Basic.Types.Structure;
using Swarmops.Database;
using Swarmops.Logic.Cache;
using Swarmops.Logic.Support;
namespace Swarmops.Logic.Structure
{
public class Countries : List<Country>
public class Countries : PluralBase<Countries, Country, BasicCountry>
{
public static Countries GetAll()
public static Countries All
{
get
{
// Cached - the list of countries doesn't change that much
Countries result = (Countries) GuidCache.Get ("Countries.All");
if (result == null)
{
result = Countries.GetAll();
GuidCache.Set ("Countries.All", result);
}
return result;
}
}
private static Countries GetAll() // private - uncached version shouldn't be used outside this class
{
return FromArray (SwarmDb.GetDatabaseForReading().GetAllCountries());
}
@@ -17,16 +36,5 @@ namespace Swarmops.Logic.Structure
return FromArray (SwarmDb.GetDatabaseForReading().GetCountriesInUse());
}
public static Countries FromArray (BasicCountry[] basicArray)
{
Countries result = new Countries {Capacity = (basicArray.Length*11/10)};
foreach (BasicCountry basic in basicArray)
{
result.Add (Country.FromBasic (basic));
}
return result;
}
}
}
+1 -39
View File
@@ -7,12 +7,11 @@ using Swarmops.Common.Enums;
using Swarmops.Common.Generics;
using Swarmops.Database;
using Swarmops.Logic.Cache;
using Swarmops.Logic.Interfaces;
namespace Swarmops.Logic.Structure
{
[Serializable]
public class Geography : BasicGeography, ITreeNode, ITreeNodeObject
public class Geography : BasicGeography
{
#region Construction and Creation
@@ -64,45 +63,8 @@ namespace Swarmops.Logic.Structure
public static readonly int RootIdentity = 1; // The identity of the root geography (i.e., "World")
public static readonly int SwedenId = 30;
public static readonly int DenmarkId = 357;
public static readonly int FinlandId = 358;
public static readonly int IgnoreGeography = -1; // Used as parameter to methods where geography is optional
#region ITreeNode Members
public int ParentIdentity
{
get { return ParentGeographyId; }
}
public int[] ChildrenIdentities
{
get { return Children.Identities; }
}
[XmlIgnore] // interface cannot be serialized
[SoapIgnore]
public ITreeNodeObject ParentObject
{
get { return Parent; }
}
[XmlIgnore] // interface cannot be serialized
[SoapIgnore]
public List<ITreeNodeObject> ChildObjects
{
get
{
List<ITreeNodeObject> retVal = new List<ITreeNodeObject>();
foreach (Geography child in Children)
retVal.Add (child);
return retVal;
}
}
#endregion
public Geographies Children
{
+1 -32
View File
@@ -12,13 +12,12 @@ using Swarmops.Database;
using Swarmops.Logic.Cache;
using Swarmops.Logic.Communications;
using Swarmops.Logic.Financial;
using Swarmops.Logic.Interfaces;
using Swarmops.Logic.Support;
using Swarmops.Logic.Swarm;
namespace Swarmops.Logic.Structure
{
public class Organization : BasicOrganization, ITreeNode, IComparable, ITreeNodeObject
public class Organization : BasicOrganization, IComparable
{
#region Creation and Construction
@@ -132,36 +131,6 @@ namespace Swarmops.Logic.Structure
#endregion
#region ITreeNode Members
public int ParentIdentity
{
get { return ParentOrganizationId; }
}
public int[] ChildrenIdentities
{
get { return Children.Identities; }
}
public ITreeNodeObject ParentObject
{
get { return Parent; }
}
public List<ITreeNodeObject> ChildObjects
{
get
{
List<ITreeNodeObject> retVal = new List<ITreeNodeObject>();
foreach (Organization child in Children)
retVal.Add (child);
return retVal;
}
}
#endregion
private ObjectOptionalData OptionalData
{
+1 -1
View File
@@ -13,7 +13,7 @@ public class GetGeographyData : System.Web.Services.WebService
[WebMethod]
public Countries GetCountries()
{
return Countries.GetAll();
return Countries.All;
}
[WebMethod]
+33 -7
View File
@@ -4,6 +4,7 @@ using System.Globalization;
using System.Threading;
using Resources;
using Swarmops.Common.Generics;
using Swarmops.Logic.Cache;
using Swarmops.Logic.Structure;
namespace Swarmops.Frontend.Automation
@@ -48,15 +49,21 @@ namespace Swarmops.Frontend.Automation
// Not in cache. Construct.
_geoTree = Geographies.Tree;
Countries countries = Countries.GetAll();
_countryLookup = new Dictionary<int, Country>();
_countryLookup = (Dictionary<int, Country>) GuidCache.Get ("Json-GeographiesTree._countryLookup");
foreach (Country country in countries)
if (_countryLookup == null)
{
if (country.GeographyId > Geography.RootIdentity)
Countries countries = Countries.All;
_countryLookup = new Dictionary<int, Country>();
foreach (Country country in countries)
{
_countryLookup[country.GeographyId] = country;
if (country.GeographyId > Geography.RootIdentity)
{
_countryLookup[country.GeographyId] = country;
}
}
GuidCache.Set("Json-GeographiesTree._countryLookup", _countryLookup);
}
List<TreeNode<Geography>> rootNodes = _geoTree.RootNodes;
@@ -66,7 +73,7 @@ namespace Swarmops.Frontend.Automation
rootNodes = _geoTree[parentGeographyId].Children;
}
accountsJson = RecurseTreeMap (rootNodes, false);
accountsJson = RecurseTreeMap (rootNodes, initialExpand);
Cache.Insert (cacheKey, accountsJson, null, DateTime.Now.AddMinutes (15), TimeSpan.Zero);
// cache lasts for fifteen minutes, no sliding expiration
@@ -86,8 +93,27 @@ namespace Swarmops.Frontend.Automation
{
Geography geography = geographyNode.Data;
string geoName = TestLocalization (geography.Name);
if (_countryLookup.ContainsKey (geography.Identity))
{
// Special case for country nodes: "[NativeName] ([LocalizedName])", e.g. "Deutschland (Tyskland)" for Germany when in Swedish
string localizedCountryName = GeographyNames.ResourceManager.GetString("Country_" + _countryLookup[geography.Identity].Code);
string nativeCountryName = geography.Name.Split ('(')[0].Trim();
if (localizedCountryName != nativeCountryName)
{
geoName = nativeCountryName + " (" + localizedCountryName + ")";
}
else
{
geoName = nativeCountryName;
}
}
string element = string.Format ("\"id\":{0},\"text\":\"{1}\"", geography.Identity,
JsonSanitize (TestLocalization (geography.Name)));
JsonSanitize (TestLocalization (geoName)));
if (_countryLookup.ContainsKey (geography.Identity))
{
+1 -1
View File
@@ -42,7 +42,7 @@ namespace Swarmops.Frontend.Pages.v5.Swarm
private void Populate()
{
Countries allCountries = Countries.GetAll();
Countries allCountries = Countries.All;
this.DropCountries.Items.Clear();
foreach (Country country in allCountries)