本文整理汇总了C#中Dapper.DynamicParameters.AddOutput方法的典型用法代码示例。如果您正苦于以下问题:C# DynamicParameters.AddOutput方法的具体用法?C# DynamicParameters.AddOutput怎么用?C# DynamicParameters.AddOutput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dapper.DynamicParameters
的用法示例。
在下文中一共展示了DynamicParameters.AddOutput方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadStream
public async Task<StreamEventsPage> ReadStream(string streamId, int fromVersion, int count, StreamReadDirection direction = StreamReadDirection.Forward)
{
Guard.NullOrWhiteSpace(() => streamId);
await _log.Debug("Reading stream {@streamId} from version {fromVersion} to version {count}", streamId, fromVersion, count);
// create parameters
var parameters = new DynamicParameters();
parameters.AddDynamicParams(new {
StreamId = streamId,
FromVersion = fromVersion,
Count = count,
ReadForward = direction == StreamReadDirection.Forward ? 1 : 0
});
parameters.AddOutput("Error");
parameters.AddReturnValue();
IEnumerable<StreamEventData> result;
// execute operation
using(var connection = new SqlConnection(_settings.ConnectionString)) {
// found a hardcore bug with Dapper!
// if we exit a query without executing a select, the .QueryAsync<T> fails because it
// tries to read the resultsetschema and fails.
// therefore we do not have access to return or output values.
result = await connection
.QueryAsync<StreamEventData>(
sql : "ReadStream",
param : parameters,
commandType: CommandType.StoredProcedure)
.ConfigureAwait(false);
}
// check for errors
switch(parameters.GetOutput<int>("Error")) {
case -100:
throw new StreamNotFoundException(streamId);
case -200:
throw new StreamDeletedException(streamId, fromVersion);
}
await _log.Information("Stream {@streamId} read from version {fromVersion} to version {count}", streamId, fromVersion, count);
// return stream page
var streamVersion = parameters.GetReturnValue();
var streamEvents = result.Select(
streamEventData => {
var metadata = _serializer.Deserialize<IDictionary<string, string>>(streamEventData.Metadata);
var domainEventType = Type.GetType(metadata[EventMetadataKeys.ClrType].ToString());
var streamEvent = _serializer.DeserializeAs<object>(streamEventData.Data, domainEventType);
return new StreamEvent(streamEvent, metadata);
}).ToList();
var lastReadEventVersion = streamEvents.Any()
? int.Parse(streamEvents.Last().Metadata[EventMetadataKeys.Version])
: -1;
await _log.Debug("Stream {@streamId} event serialization finished", streamId);
return new StreamEventsPage(
streamId : streamId,
fromVersion: fromVersion,
toVersion : lastReadEventVersion,
lastVersion: streamVersion,
events : streamEvents,
direction : direction);
}