Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn

mxriverlynn/1.originalcode.cs Secret

Created Sep 30, 2010
Embed
What would you like to do?
A DSL For Significant Numbers
private AddAssetResult HandleAssetFamily(SortContainer container, SystemAsset systemAsset)
{
AddAssetResult result = null;
var assetFamilies = assetFamilyDao.GetByAssetType(systemAsset.AssetTypeId);
if (assetFamilies == null || assetFamilies.Count == 0)
{
result = new AddAssetResult(SortContainerResources.NoAssetFamily);
}
if (assetFamilies != null && assetFamilies.Count > 1)
{
result = new AddAssetResult(SortContainerResources.MultipleAssetFamilies);
}
if (assetFamilies != null && assetFamilies.Count == 1)
{
var assetFamily = assetFamilies[0];
SetContainerFamily(container, assetFamily);
result = VerifyFamilyIsCompatible(systemAsset, container);
}
return result;
}
private AddAssetResult HandleAssetFamily(SortContainer container, SystemAsset systemAsset)
{
var assetFamilies = assetFamilyDao.GetByAssetType(systemAsset.AssetTypeId);
var result = assetFamilies.CheckCount<AssetFamily, AddAssetResult>()
.WhenZero(() => new AddAssetResult(SortContainerResources.NoAssetFamily))
.WhenOne(() => AddAssetWithFamily(systemAsset, container, assetFamilies[0]))
.WhenMany(() => new AddAssetResult(SortContainerResources.MultipleAssetFamilies))
.Result;
return result;
}
public static class CountCheckerExtensions
{
public static CountChecker<TResult> CheckCount<TCollectionType, TResult>(this IEnumerable<TCollectionType> enumerable)
{
int theCount = 0;
if (enumerable != null)
theCount = enumerable.Count();
return new CountChecker<TResult>(theCount);
}
}
public class CountChecker<TResult>
{
private readonly int theCount;
public TResult Result { get; private set; }
public CountChecker(int theCount)
{
this.theCount = theCount;
}
public CountChecker<TResult> WhenZero(Func<TResult> zeroAction)
{
if (theCount == 0)
Result = zeroAction();
return this;
}
public CountChecker<TResult> WhenOne(Func<TResult> oneAction)
{
if (theCount == 1)
Result = oneAction();
return this;
}
public CountChecker<TResult> WhenMany(Func<TResult> manyAction)
{
if (theCount > 1)
Result = manyAction();
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.