- GH.3Tier.Demo.zip
- GH.3Tier.Demo
- 3rdPartyTools
- EntityFramework.4.3.1
- lib
- net40
- Ninject
- GH.Common
- Framework
- Business
- Client
- Persistence
- GH.Common.csproj
- LogService
- Properties
- Service References
- Domain.Models
- ServiceLocator
- Utils
- GH.Northwind
- Business
- GH.Northwind.Business.Entities
- GH.Northwind.Business.Host
- GH.Northwind.Business.Interfaces
- GH.Northwind.Business
- Clients
- GH.Northwind.Client.Common
- GH.Northwind.Web
- Content
- Site.css
- themes
- base
- images
- ui-bg_flat_0_aaaaaa_40x100.png
- ui-bg_flat_75_ffffff_40x100.png
- ui-bg_glass_55_fbf9ee_1x400.png
- ui-bg_glass_65_ffffff_1x400.png
- ui-bg_glass_75_dadada_1x400.png
- ui-bg_glass_75_e6e6e6_1x400.png
- ui-bg_glass_95_fef1ec_1x400.png
- ui-bg_highlight-soft_75_cccccc_1x100.png
- ui-icons_222222_256x240.png
- ui-icons_2e83ff_256x240.png
- ui-icons_454545_256x240.png
- ui-icons_888888_256x240.png
- ui-icons_cd0a0a_256x240.png
- jquery-ui.css
- Controllers
- GH.Northwind.Web.csproj
- GH.Northwind.Web.csproj.user
- Global.asax
- Global.asax.cs
- ModelBinders
- Models
- Properties
- Scripts
- Views
- Web.config
- Web.Debug.config
- Web.Release.config
- Persistence
- GH.Northwind.EntityFramework.Host
- GH.Northwind.EntityFramework
- GH.Northwind.Persistence
- GH.NTier.Demo.sln
- GH.NTier.Demo.suo
- instnwnd.sql
- Local.testsettings
- scaffolding.config
- TraceAndTestImpact.testsettings
- GH.3Tier.Demo-noexe.zip
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Services.Client;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using GH.Common.Framework.DomainEntities;
using GH.Common.LogService;
using GH.Common.Utils;
namespace GH.Common.Framework.Persistence.DataServiceContext
{
public class PersistenceBase<T> : IPersistence<T> where T : class /*DomainEntityBase*/
{
protected String _entitySetName = String.Empty;
public ILogger<PersistenceBase<T>> Logger { get; set; }
static public ILogger<PersistenceBase<T>> Log
{
get
{
return Log<PersistenceBase<T>>.LogProvider;
}
}
public static System.Data.Services.Client.DataServiceContext DataContext { get; set; }
public PersistenceBase()
{
}
#region IPersistence<T> Members
virtual public void Insert(T entity, bool commit)
{
InsertObject(entity, commit);
}
virtual public void Update(T entity, bool commit)
{
UpdateObject(entity, commit);
}
virtual public void Delete(T entity, bool commit)
{
DeleteObject(entity, commit);
}
virtual public void Commit()
{
DataContext.SaveChanges();
}
virtual public IQueryable<T> SearchBy(Expression<Func<T, bool>> predicate)
{
return EntitySet.Where(predicate);
}
virtual public IQueryable<T> GetAll()
{
return EntitySet;
}
virtual public T SearchById(object id)
{
return null;
}
#endregion
virtual protected IQueryable<T> EntitySet
{
get { throw new ApplicationException("PersistenceBase.EntitySet: Shouldn't get here."); }
}
virtual protected String EntitySetName
{
get { throw new ApplicationException("PersistenceBase.EntitySetName: Shouldn't get here."); }
}
protected void InsertObject(T entity, bool commit)
{
DataContext.AddObject(EntitySetName, entity);
try
{
if (commit) DataContext.SaveChanges();
}
catch (Exception e)
{
//Need to restore back the original data if DataContext.SaveChanges() fails; otherwise, next time, these bad data is still here
//DataContext.LoadProperty(DataContext, EntitySetName);
DataContext.DeleteObject(entity);
Log.Error(e);
throw;
}
}
protected void UpdateObject(T entity, bool commit)
{
DataContext.UpdateObject(entity);
try
{
if (commit) DataContext.SaveChanges();
}
catch (Exception e)
{
//Need to restore back the original data if DataContext.SaveChanges() fails; otherwise, next time, these bad data is still here
DataContext.LoadProperty(DataContext, EntitySetName);
Log.Error(e);
throw;
}
}
protected void DeleteObject(T entity, bool commit)
{
DataContext.DeleteObject(entity);
try
{
if (commit) DataContext.SaveChanges();
}
catch (Exception e)
{
//Need to restore back the original data if DataContext.SaveChanges() fails; otherwise, next time, these bad data is still here
DataContext.LoadProperty(DataContext, EntitySetName);
//DataContext.AddObject(EntitySetName, entity);
Log.Error(e);
throw;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.