本文整理汇总了C#中Transaction.Lock方法的典型用法代码示例。如果您正苦于以下问题:C# Transaction.Lock方法的具体用法?C# Transaction.Lock怎么用?C# Transaction.Lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Transaction
的用法示例。
在下文中一共展示了Transaction.Lock方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NewTransaction
public void NewTransaction()
{
var transaction = new Transaction(Guid.NewGuid(), this.book.Securities.First());
using (var tLock = transaction.Lock())
{
transaction.SetDate(DateTime.Today.ToUniversalTime(), tLock);
var split1 = transaction.AddSplit(tLock);
var split2 = transaction.AddSplit(tLock);
split1.SetAccount(this.account, tLock);
split1.SetSecurity(transaction.BaseSecurity, tLock);
split2.SetSecurity(transaction.BaseSecurity, tLock);
}
this.transactionEditor.SetSplit(transaction.Splits[0]);
this.transactionIsNew = true;
}
示例2: GivenATransactionWithBaseSecurityAndTheFollowingSplits
public void GivenATransactionWithBaseSecurityAndTheFollowingSplits(string transactionName, string baseSecurity, Table table)
{
Assume.That(table.Header, Has.Member("Account"));
Assume.That(table.Header, Has.Member("Security"));
Assume.That(table.Header, Has.Member("Amount"));
var accounts = (from a in table.Rows
group a by a["Account"] into g
let acctName = g.Key
let account = (Account)ScenarioContext.Current[acctName]
select new
{
acctName,
account
}).ToDictionary(a => a.acctName, a => a.account);
var securities = (from a in table.Rows
group a by a["Security"] into g
let secName = g.Key
let security = (Security)ScenarioContext.Current[secName]
select new
{
secName,
security
}).ToDictionary(a => a.secName, a => a.security);
Security transactionSecurity;
if (string.IsNullOrEmpty(baseSecurity))
{
var firstAccount = accounts[table.Rows[0]["Account"]];
transactionSecurity = firstAccount.Security;
}
else
{
transactionSecurity = (Security)ScenarioContext.Current[baseSecurity];
}
var transaction = new Transaction(
transactionId: Guid.NewGuid(),
baseSecurity: transactionSecurity);
var securityCount = (from a in accounts
group a.Value by a.Value.Security into g
select g.Key).Count();
if (securityCount > 1)
{
Assume.That(table.Header, Has.Member("Transaction Amount"));
}
using (var @lock = transaction.Lock())
{
foreach (var row in table.Rows)
{
var account = accounts[row["Account"]];
var security = securities[row["Security"]];
var amount = long.Parse(row["Amount"]);
var transactionAmount = securityCount > 1 ? long.Parse(row["Transaction Amount"]) : amount;
var split = transaction.AddSplit(@lock);
split.SetAccount(account, @lock);
split.SetSecurity(security, @lock);
split.SetAmount(amount, @lock);
split.SetTransactionAmount(transactionAmount, @lock);
}
}
ScenarioContext.Current.Add(transactionName, transaction);
}
示例3: Load
protected override Book Load(Uri uri)
{
var book = new Book();
var doc = XDocument.Load(uri.ToString());
var securities = new Dictionary<Guid, Security>();
var accounts = new Dictionary<Guid, Account>();
foreach (var s in doc.Element("Book").Element("Securities").Elements("Security"))
{
var f = s.Element("Format");
var decimalDigits = (int)f.Attribute("decimalDigits");
var decimalSeparator = (string)f.Attribute("decimalSeparator");
var groupSeparator = (string)f.Attribute("groupSeparator");
var groupSizes = ((string)f.Attribute("groupSizes")).Split(',').Select(g => int.Parse(g.Trim()));
var positiveFormat = (PositiveFormat)Enum.Parse(typeof(PositiveFormat), (string)f.Attribute("positiveFormat"));
var negativeFormat = (NegativeFormat)Enum.Parse(typeof(NegativeFormat), (string)f.Attribute("negativeFormat"));
var currencySymbol = (string)f.Attribute("symbol");
var format = new CurrencyFormat(decimalDigits, decimalSeparator, groupSeparator, groupSizes, currencySymbol, positiveFormat, negativeFormat);
var securityId = (Guid)s.Attribute("id");
var securityType = (SecurityType)Enum.Parse(typeof(SecurityType), (string)s.Attribute("type"));
var name = (string)s.Attribute("name");
var symbol = (string)s.Attribute("symbol");
var fractionTraded = (int)s.Attribute("fractionTraded");
var security = new Security(securityId, securityType, name, symbol, format, fractionTraded);
securities.Add(security.SecurityId, security);
book.AddSecurity(security);
}
foreach (var a in doc.Element("Book").Element("Accounts").Elements("Account"))
{
var accountId = (Guid)a.Attribute("id");
var accountType = (AccountType)Enum.Parse(typeof(AccountType), (string)a.Attribute("type"));
Security security = null;
var securityAttr = a.Attribute("securityId");
if (securityAttr != null)
{
security = securities[(Guid)securityAttr];
}
Account parentAccount = null;
var parentAttr = a.Attribute("parentAccountId");
if (parentAttr != null)
{
parentAccount = accounts[(Guid)parentAttr];
}
var name = (string)a.Attribute("name");
var smallestFraction = (int?)a.Attribute("smallestFraction");
var account = new Account(accountId, accountType, security, parentAccount, name, smallestFraction);
accounts.Add(account.AccountId, account);
book.AddAccount(account);
}
foreach (var t in doc.Element("Book").Element("Transactions").Elements("Transaction"))
{
var transactionId = (Guid)t.Attribute("id");
var securityId = (Guid)t.Attribute("securityId");
var security = securities[securityId];
var date = (DateTime)t.Attribute("date");
var transaction = new Transaction(transactionId, security);
using (var tlock = transaction.Lock())
{
transaction.SetDate(date, tlock);
foreach (var s in t.Elements("Split"))
{
var split = transaction.AddSplit(tlock);
var accountId = (Guid)s.Attribute("accountId");
var account = accounts[accountId];
split.SetAccount(account, tlock);
var splitSecurityId = (Guid)s.Attribute("securityId");
var splitSecurity = securities[splitSecurityId];
split.SetSecurity(splitSecurity, tlock);
var amount = (long)s.Attribute("amount");
split.SetAmount(amount, tlock);
var transactionAmount = (long)s.Attribute("transactionAmount");
split.SetTransactionAmount(transactionAmount, tlock);
var dateCleared = (DateTime?)s.Attribute("dateCleared");
split.SetDateCleared(dateCleared, tlock);
var reconciled = (bool)s.Attribute("reconciled");
split.SetIsReconciled(reconciled, tlock);
}
}
book.AddTransaction(transaction);
}
//.........这里部分代码省略.........
示例4: RemoveSecurity_WhenSecurityIsUsedInSplits_ThrowsException
public void RemoveSecurity_WhenSecurityIsUsedInSplits_ThrowsException()
{
// Create a new, valid book.
var book = TestUtils.CreateValidBook();
// Create a new account with no security.
var account = new Account(
Guid.NewGuid(), // OK
AccountType.Balance, // OK
null, // OK
null, // OK
"OK_NAME",
null); // OK
book.AddAccount(account);
// Create a transaction that uses this split, but not as the
var transaction = new Transaction(Guid.NewGuid(), TestUtils.TestCurrency);
using (var tLock = transaction.Lock())
{
var split1 = transaction.AddSplit(tLock);
split1.SetAccount(account, tLock);
split1.SetSecurity(TestUtils.TestCurrency, tLock);
split1.SetAmount(0, tLock);
}
book.AddTransaction(transaction);
// Assert that trying to remove the security while the account depends on it throws an InvalidOperationException.
Assert.That(() => book.RemoveSecurity(TestUtils.TestCurrency), Throws.InstanceOf<InvalidOperationException>());
}
示例5: AddTransaction_WhenAnotherTransactionHasTheSameTransactionId_ThrowsException
public void AddTransaction_WhenAnotherTransactionHasTheSameTransactionId_ThrowsException()
{
// Create a new, valid book.
var book = TestUtils.CreateValidBook();
// Create a new, valid account and add it to the book.
var account = TestUtils.CreateValidAccount();
book.AddAccount(account);
// Create a new, valid transaction and add it to the book.
var transaction1 = TestUtils.CreateValidTransaction(account);
book.AddTransaction(transaction1);
// Construct a new, valid transaction that shares its TransactionId with transaction1.
var transaction2 = new Transaction(
transaction1.TransactionId,
TestUtils.TestCurrency); // OK
using (var transactionLock = transaction2.Lock())
{
var split = transaction2.AddSplit(transactionLock);
split.SetAccount(account, transactionLock);
split.SetSecurity(account.Security, transactionLock);
}
// Assert that trying to add the transaction throws an InvalidOperationException.
Assert.That(() => book.AddTransaction(transaction2), Throws.InstanceOf<InvalidOperationException>());
}