public Session(ITransaction transaction, string userName)
: base(transaction as IEventSource)
{
if (transaction == null)
throw new ArgumentNullException("transaction");
if (String.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (String.Equals(userName, User.SystemName, StringComparison.OrdinalIgnoreCase) ||
String.Equals(userName, User.PublicName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(String.Format("Cannot open a session for user '{0}'.", userName));
Transaction = transaction;
Context = transaction.Context.CreateSessionContext();
Context.RegisterInstance(this);
Transaction.Context.Route<QueryEvent>(OnQueryCommand);
Transaction.GetTableManager().AddInternalTables(new SessionTableContainer(this));
access = new SessionAccess(this);
if (!transaction.Database.Sessions.Add(this))
throw new InvalidOperationException("The session was already in the database session list");
User = new User(this, userName);
startedOn = DateTimeOffset.UtcNow;
this.OnEvent(new SessionEvent(SessionEventType.Begin));
}