Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Mar 20, 2012
0 parents commit 5880d8c
Show file tree
Hide file tree
Showing 26 changed files with 1,297 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
_ReSharper.*
*.ReSharper
*.cache
*.user
*.suo
bin/
obj/
20 changes: 20 additions & 0 deletions CommandBasedComponentsLab.sln
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandBasedComponents", "Source\CommandBasedComponents.csproj", "{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}.Debug|x86.ActiveCfg = Debug|x86
{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}.Debug|x86.Build.0 = Debug|x86
{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}.Release|x86.ActiveCfg = Release|x86
{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
79 changes: 79 additions & 0 deletions Source/CommandBasedComponents.csproj
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{C349CA6C-8CAB-4513-BF1E-CFA7B13E601C}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CommandBasedComponents</RootNamespace>
<AssemblyName>CommandBasedComponents</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Scratchpad.cs" />
<Compile Include="Core\NotNullAttribute.cs" />
<Compile Include="Infrastructure\WindowsServiceFacadeReal.cs" />
<Compile Include="Infrastructure\WindowsServiceLocatorReal.cs" />
<Compile Include="Core\ActionDisposable.cs" />
<Compile Include="Core\ChainCommand.cs" />
<Compile Include="Core\CleanupStrategy.cs" />
<Compile Include="Core\CommandExtensions.cs" />
<Compile Include="Core\ContextKey.cs" />
<Compile Include="Core\DecoratedCommandBase.cs" />
<Compile Include="Core\IDecoratorCommand.cs" />
<Compile Include="Core\Interceptor.cs" />
<Compile Include="Core\TransformerDecorators.cs" />
<Compile Include="Core\Context.cs" />
<Compile Include="Core\ICommand.cs" />
<Compile Include="Core\IContext.cs" />
<Compile Include="Core\IContextReader.cs" />
<Compile Include="Core\IContextWriter.cs" />
<Compile Include="Core\LoggingCommand.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
19 changes: 19 additions & 0 deletions Source/Core/ActionDisposable.cs
@@ -0,0 +1,19 @@
using System;

namespace CommandBasedComponents.Core
{
public class ActionDisposable : IDisposable
{
readonly Action _action;

public ActionDisposable(Action action)
{
_action = action;
}

public void Dispose()
{
_action();
}
}
}
47 changes: 47 additions & 0 deletions Source/Core/ChainCommand.cs
@@ -0,0 +1,47 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace CommandBasedComponents.Core
{
public abstract class ChainCommand : DecoratedCommandBase, IEnumerable<ICommand>, ICommand
{
readonly List<ICommand> _chain = new List<ICommand>();

protected ChainCommand(Func<ICommand, ICommand> decoratorCommand)
: base(decoratorCommand)
{
}

public virtual void Execute(IContext context)
{
if(context == null)
{
throw new ArgumentNullException("context");
}

using(var scope = new Context(context))
{
foreach(var command in _chain)
{
ExecuteDecorated(scope, command);
}
}
}

public IEnumerator<ICommand> GetEnumerator()
{
return _chain.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public void Add(ICommand command)
{
_chain.Add(command);
}
}
}
9 changes: 9 additions & 0 deletions Source/Core/CleanupStrategy.cs
@@ -0,0 +1,9 @@
namespace CommandBasedComponents.Core
{
public enum CleanupStrategy
{
None,
Dispose,
Scope
}
}
98 changes: 98 additions & 0 deletions Source/Core/CommandExtensions.cs
@@ -0,0 +1,98 @@
using System;
using System.Diagnostics;

namespace CommandBasedComponents.Core
{
[DebuggerNonUserCode]
public static class CommandExtensions
{
public static ICommand IfHasKey(this ICommand command, object key)
{
if(key == null)
{
throw new ArgumentNullException("key");
}

return new IfCommand(command, context => context.HasValue(key));
}

public static ICommand If(this ICommand command, Predicate<IContext> action)
{
if(action == null)
{
throw new ArgumentNullException("action");
}

return new IfCommand(command, action);
}

public static ICommand IgnoreExceptions(this ICommand command)
{
return new IgnoreExceptionsCommand(command);
}

public static ICommand GetDecorated(this IDecoratorCommand command)
{
ICommand baseCommand = command;
while(baseCommand is IDecoratorCommand)
{
baseCommand = ( (IDecoratorCommand)baseCommand ).Inner;
}
return baseCommand;
}

class IfCommand : IDecoratorCommand
{
readonly Predicate<IContext> _action;

public IfCommand(ICommand inner, Predicate<IContext> action)
{
if(inner == null)
{
throw new ArgumentNullException("inner");
}
if(action == null)
{
throw new ArgumentNullException("action");
}
Inner = inner;
_action = action;
}

public void Execute(IContext context)
{
if(_action(context))
{
Inner.Execute(context);
}
}

public ICommand Inner { get; private set; }
}

class IgnoreExceptionsCommand : IDecoratorCommand
{
public IgnoreExceptionsCommand(ICommand inner)
{
if(inner == null)
{
throw new ArgumentNullException("inner");
}
Inner = inner;
}

public void Execute(IContext context)
{
try
{
Inner.Execute(context);
}
catch(Exception)
{
}
}

public ICommand Inner { get; private set; }
}
}
}

0 comments on commit 5880d8c

Please sign in to comment.