forked from JoergEg/CQRS-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JoergEg
committed
Mar 14, 2011
1 parent
6fc7e8c
commit ef01dac
Showing
14 changed files
with
150 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
namespace CQRSSample.Commands | ||
using System.Dynamic; | ||
|
||
namespace CQRSSample.Commands | ||
{ | ||
public class DynamicCommand | ||
public class DynamicCommand<T> : DynamicObject where T : Command | ||
{ | ||
private readonly dynamic _innerCommand; | ||
private readonly dynamic _innerCommand; | ||
|
||
public DynamicCommand(T command) | ||
{ | ||
_innerCommand = command; | ||
} | ||
|
||
public T InnerCommand { get { return _innerCommand; } } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 0 additions & 26 deletions
26
CQRSSample.WpfClient/ApplicationFramework/Resources/Style.xaml
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
CQRSSample.WpfClient/ApplicationFramework/ScreenWithValidatingCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Caliburn.Micro; | ||
using CQRSSample.Commands; | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
|
||
namespace CQRSSample.WpfClient.ApplicationFramework | ||
{ | ||
public class ScreenWithValidatingCommand<T> : Screen where T : Command | ||
{ | ||
public ValidatingCommand<T> Command { get; protected set; } | ||
|
||
public IValidator<T> Validator { get; set; } | ||
|
||
/// <summary> | ||
/// Validates the command | ||
/// </summary> | ||
protected virtual ValidationResult Validate() | ||
{ | ||
return Command.Validate(); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
CQRSSample.WpfClient/ApplicationFramework/ValidatingCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using CQRSSample.Commands; | ||
using FluentValidation; | ||
using FluentValidation.Results; | ||
|
||
namespace CQRSSample.WpfClient.ApplicationFramework | ||
{ | ||
public class ValidatingCommand<T> : DynamicCommand<T>, IDataErrorInfo where T : Command | ||
{ | ||
private readonly IValidator<T> _validator; | ||
|
||
public ValidatingCommand(T command, IValidator<T> validator) | ||
: base(command) | ||
{ | ||
_validator = validator; | ||
} | ||
|
||
public override bool TryGetMember(GetMemberBinder binder, out object result) | ||
{ | ||
var ex = Expression.Property(Expression.Constant(InnerCommand), binder.Name); | ||
result = Expression.Lambda(ex).Compile().DynamicInvoke(); | ||
return true; | ||
} | ||
|
||
public override bool TrySetMember(SetMemberBinder binder, object value) | ||
{ | ||
var method = InnerCommand.GetType().GetProperty(binder.Name).GetSetMethod(); | ||
method.Invoke(InnerCommand, new[] { value }); | ||
|
||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Validates the command | ||
/// </summary> | ||
public virtual ValidationResult Validate() | ||
{ | ||
return _validator.Validate(InnerCommand); | ||
} | ||
|
||
public string this[string columnName] | ||
{ | ||
get | ||
{ | ||
var validationResults = Validate(); | ||
|
||
if (validationResults == null) return string.Empty; | ||
|
||
var columnResults = validationResults.Errors.FirstOrDefault(x => string.Compare(x.PropertyName, columnName, true) == 0); | ||
|
||
return columnResults != null ? columnResults.ErrorMessage : string.Empty; | ||
} | ||
} | ||
|
||
public string Error | ||
{ | ||
get | ||
{ | ||
var message = new StringBuilder(); | ||
|
||
foreach (var validationFailure in Validate().Errors) | ||
{ | ||
message.Append(validationFailure.ErrorMessage); | ||
message.Append(Environment.NewLine); | ||
} | ||
|
||
return message.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.