/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using EFCachingProvider;
using EFCachingProvider.Caching;
using EFProviderWrapperToolkit;
using EFTracingProvider;
using Microsoft.Data.Extensions;
using Everest.Library;
using System.IO;
using System.Data.Common;
namespace Everest.CmsServices.Models{
public partial class ExtendedEverestCmsEntities : EverestCmsEntities
{
private TextWriter logOutput;
public ExtendedEverestCmsEntities()
: this("name=EverestCmsEntities")
{
}
public ExtendedEverestCmsEntities(string connectionString)
: base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
connectionString,
"EFTracingProvider",
"EFCachingProvider"
))
{
}
#region Tracing Extensions
private EFTracingConnection TracingConnection
{
get { return this.UnwrapConnection<EFTracingConnection>(); }
}
public event EventHandler<CommandExecutionEventArgs> CommandExecuting
{
add { this.TracingConnection.CommandExecuting += value; }
remove { this.TracingConnection.CommandExecuting -= value; }
}
public event EventHandler<CommandExecutionEventArgs> CommandFinished
{
add { this.TracingConnection.CommandFinished += value; }
remove { this.TracingConnection.CommandFinished -= value; }
}
public event EventHandler<CommandExecutionEventArgs> CommandFailed
{
add { this.TracingConnection.CommandFailed += value; }
remove { this.TracingConnection.CommandFailed -= value; }
}
private void AppendToLog(object sender, CommandExecutionEventArgs e)
{
if (this.logOutput != null)
{
this.logOutput.WriteLine(e.ToTraceString().TrimEnd());
this.logOutput.WriteLine();
}
}
public TextWriter Log
{
get { return this.logOutput; }
set
{
if ((this.logOutput != null) != (value != null))
{
if (value == null)
{
CommandExecuting -= AppendToLog;
}
else
{
CommandExecuting += AppendToLog;
}
}
this.logOutput = value;
}
}
#endregion
#region Caching Extensions
private EFCachingConnection CachingConnection
{
get { return this.UnwrapConnection<EFCachingConnection>(); }
}
public ICache Cache
{
get { return CachingConnection.Cache; }
set { CachingConnection.Cache = value; }
}
public CachingPolicy CachingPolicy
{
get { return CachingConnection.CachingPolicy; }
set { CachingConnection.CachingPolicy = value; }
}
#endregion
}
/// <summary>
///
/// </summary>
public partial class EverestCmsEntities
{
public const string DataKeyInContext = "__DataContext__";
public static IEverestCmsDataContext GetDataContext()
{
return GetDataContext(false);
}
/// <summary>
/// Gets the data context.
/// There will only single instance for the same web context.
/// </summary>
/// <param name="enableCaching">if set to <c>true</c> [enable caching].</param>
/// <returns></returns>
public static IEverestCmsDataContext GetDataContext(bool enableCaching)
{
IEverestCmsDataContext dataContext;
if (HttpContext.Current != null)
{
dataContext = (IEverestCmsDataContext)HttpContext.Current.Items[DataKeyInContext];
if (dataContext == null || dataContext.ObjectContext == null)
{
dataContext = CreateDataContext(enableCaching);
HttpContext.Current.Items[DataKeyInContext] = dataContext;
}
}
else
{
dataContext = CreateDataContext(enableCaching);
}
return dataContext;
}
internal static IEverestCmsDataContext CreateDataContext(bool enableCaching)
{
var dataContext = new EverestDataContextWrapper(new ExtendedEverestCmsEntities());
dataContext.EnableCaching = enableCaching;
return dataContext;
}
///// <summary>
///// Creates the db command.
///// </summary>
///// <param name="commandText">The command text.</param>
///// <param name="commandType">Type of the command.</param>
///// <param name="parameters">The parameters.</param>
///// <returns></returns>
//public static DbCommand CreateDbCommand(string commandText, CommandType commandType, params object[] parameters)
//{
// EverestCmsEntities dataContext = new EverestCmsEntities();
// return dataContext.CreateStoreCommand(commandText, commandType, parameters);
//}
///// <summary>
///// Creates the db command.
///// </summary>
///// <param name="commandText">The command text.</param>
///// <param name="paramters">The paramters.</param>
///// <returns></returns>
//public static DbCommand CreateDbCommand(string commandText, params object[] paramters)
//{
// EverestCmsEntities dataContext = new EverestCmsEntities();
// return dataContext.CreateStoreCommand(commandText, paramters);
//}
}
}
|