Created
September 17, 2010 13:05
-
-
Save ToJans/584200 to your computer and use it in GitHub Desktop.
Code snip of a CQRS app I'm currently writing...
This file contains hidden or 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
public class Server : IAggregateRoot,IDisposable | |
{ | |
public Server() | |
{ | |
} | |
~Server() | |
{ | |
Dispose(); | |
} | |
static private PermanentCache<string,Guid> TableGuidCache = new PermanentCache<string,Guid>( x=>Guid.NewGuid()); | |
// public props for memento purposes | |
public string Id {get;set;} | |
public string Address = "127.0.0.1:5500"; | |
public string PocketNr = "1"; | |
public string LastServerUpdateArticles="0"; | |
public string LastServerUpdateButtons="0"; | |
public Guid AggregateRootId {get;set;} | |
bool ShouldRebuildChannel = true; | |
IHoTouchChannel _Channel; | |
IHoTouchChannel Channel | |
{ | |
get | |
{ | |
if (_Channel == null || ShouldRebuildChannel) | |
{ | |
if (_Channel!=null) | |
{ | |
_Channel.Dispose(); | |
} | |
// this seems odd; I'm injecting dependencies here => would some IOC be more appropriate ? | |
_Channel = new HoTouchChannel(new TcpChannel(Address)); | |
} | |
return _Channel; | |
} | |
} | |
public IEnumerable<IMessage> OnGetOpenTables() | |
{ | |
var tables = Channel.ServerRequest<Table[]>("request@getOpenTables@"); | |
foreach(var t in tables) | |
t.AggregateRootId = TableGuidCache[t.Name]; | |
yield return new OpenTablesReceived() {AggregateRootId = this.AggregateRootId,Tables = tables}; | |
} | |
public IEnumerable<IMessage> OnGetTable(string name) | |
{ | |
yield return new OpenTable() { AggregateRootId = TableGuidCache[name],Name = name }; | |
} | |
public IEnumerable<IMessage> OnRequestRemoteConfigurationSettings() | |
{ | |
var cr = new RemoteConfigurationSettingsReceived(){AggregateRootId = this.AggregateRootId}; | |
if (Channel.IsConnected) | |
{ | |
var ts = Channel.ServerRequest<Nullable<DateTime>>("Request@SendServerDateAndTime@", null) ?? DateTime.Now; | |
if (ts.Subtract(DateTime.Now).Duration().Minutes > 15) | |
throw new Exception("The servertime is out of sync with the time on the pocket"); | |
cr.Settings = Channel.ServerRequest<RemoteConfigurationSettings>("update@Settings"); | |
try { cr.Catalog = Channel.ServerRequest<Catalog>("UPDATE@Articles@" + LastServerUpdateArticles);} catch (NoNewDataException) {} | |
try { cr.Buttons = Channel.ServerRequest<Buttons>("UPDATE@Buttons@" + LastServerUpdateButtons); } catch (NoNewDataException) {} | |
} | |
yield return cr; | |
} | |
public IEnumerable<IMessage> OnRequestRemoteReceipt(string table,Guid OrderSagaId) | |
{ | |
if (!Channel.IsConnected) | |
yield break; | |
var rcpt = Channel.ServerRequest<Receipt>("request@getReceiptInfo@" + table) ; | |
yield return new UpdateRemoteReceiptInOrderSaga { AggregateRootId = OrderSagaId,RemoteReceipt = rcpt }; | |
} | |
public IEnumerable<IMessage> OnServerAddressChanged(string serveraddress) | |
{ | |
Address = serveraddress; | |
ShouldRebuildChannel = true; | |
yield break; | |
} | |
public void Dispose() | |
{ | |
if (_Channel!=null) | |
_Channel.Dispose(); | |
} | |
} |
This file contains hidden or 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
public ServerCommandHandlers() | |
{ | |
} | |
public IEnumerable<IMessage> HandleCommand(OpenTable message, Server ar) | |
{ | |
return ar.OnOpenTable(message.Name); | |
} | |
public IEnumerable<IMessage> HandleCommand(RequestRemoteConfigurationSettings message,Server ar) | |
{ | |
return ar.OnRequestRemoteConfigurationSettings(); | |
} | |
public IEnumerable<IMessage> HandleCommand(GetOpenTables message, Server ar) | |
{ | |
return ar.OnGetOpenTables(); | |
} | |
public IEnumerable<IMessage> HandleCommand(ChangeServerAddress message, Server ar) | |
{ | |
return ar.OnServerAddressChanged(message.ServerAddress); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment