Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/net/Microsoft.Data.Services.dll
Binary file not shown.
28,163 changes: 28,163 additions & 0 deletions lib/net/Microsoft.Data.Services.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Data.Services">
<HintPath>..\..\lib\net\Microsoft.Data.Services.dll</HintPath>
</Reference>
<Reference Include="FirebirdSql.Data.FirebirdClient">
<HintPath>..\..\lib\teamcity\firebird\FirebirdSql.Data.FirebirdClient.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.TestDatabaseSetup/TestDatabaseSetup.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<include name="FirebirdSql.Data.FirebirdClient.dll" />
<include name="System.Data.SqlServerCe.dll" />
<include name="Npgsql.dll" />
<include name="Microsoft.Data.Services.dll" />
</assemblyfileset>
</target>
<target name="generate-assemblyinfo" depends="init common.generate-assemblyinfo" />
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate/NHibernate.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<include name="System.XML.dll" />
<include name="System.Xml.Linq.dll" />
<include name="System.Data.dll" />
<include name="Microsoft.Data.Services.dll" />
<include name="Iesi.Collections.dll" />
<include name="Antlr3.Runtime.dll" />
<include name="Remotion.Linq.dll" />
Expand Down
4 changes: 4 additions & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.Data.Services">
<HintPath>..\..\lib\net\Microsoft.Data.Services.dll</HintPath>
</Reference>
<Reference Include="Antlr3.Runtime, Version=3.1.0.39271, Culture=neutral, PublicKeyToken=3a9cab8f8d22bfb7">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\net\Antlr3.Runtime.dll</HintPath>
Expand All @@ -88,6 +91,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="NHibernateDataContext.cs" />
<Compile Include="ADOException.cs" />
<Compile Include="AdoNet\MySqlClientBatchingBatcher.cs" />
<Compile Include="AdoNet\MySqlClientBatchingBatcherFactory.cs" />
Expand Down
328 changes: 328 additions & 0 deletions src/NHibernate/NHibernateDataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.Reflection;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Linq;

namespace NHibernate
{
public abstract class NHibernateDataContext : MarshalByRefObject, IUpdatable, IDisposable
{
private readonly ISet<Object> entitiesToUpdate = new HashSet<Object>();
private readonly ISet<Object> entitiesToRemove = new HashSet<Object>();

protected NHibernateDataContext(ISessionFactory sessionFactory)
{
this.Session = sessionFactory.OpenSession();
}

~NHibernateDataContext()
{
this.Dispose(false);
}

protected ISession Session
{
get;
private set;
}

protected virtual void Dispose(Boolean disposing)
{
if (disposing == true)
{
this.Session.Dispose();
this.Session = null;

this.entitiesToRemove.Clear();
this.entitiesToUpdate.Clear();
}
}

public IQueryable<T> Query<T>() where T : class
{
return (this.Session.Query<T>());
}

public T Get<T>(Object id) where T : class
{
return (this.Session.Get<T>(id));
}

public Status Status<T>(T entity) where T : class
{
return (this.Session.GetSessionImplementation().PersistenceContext.GetEntry(entity).Status);
}

public void Save<T>(T entity) where T : class
{
this.Session.Save(entity);
}

public Boolean Contains<T>(T entity) where T : class
{
return (this.Session.Contains(entity));
}

public T Attach<T>(T entity) where T: class
{
return(this.Session.Merge(entity));
}

public void Detach<T>(T entity) where T : class
{
this.Session.Evict(entity);
}

public void Refresh<T>(T entity) where T : class
{
this.Session.Refresh(entity);
}

public void Delete<T>(T entity) where T : class
{
this.Session.Delete(entity);
}

#region IUpdatable members
/// <summary>
/// Creates the resource of the given type and belonging to the given container
/// </summary>
/// <param name="containerName">container name to which the resource needs to be added</param>
/// <param name="fullTypeName">full type name i.e. Namespace qualified type name of the resource</param>
/// <returns>Object representing a resource of given type and belonging to the given container</returns>
Object IUpdatable.CreateResource(String containerName, String fullTypeName)
{
System.Type t = System.Type.GetType(fullTypeName, true);
Object resource = Activator.CreateInstance(t);

this.entitiesToUpdate.Add(resource);

return resource;
}

/// <summary>
/// Gets the resource of the given type that the query points to
/// </summary>
/// <param name="query">query pointing to a particular resource</param>
/// <param name="fullTypeName">full type name i.e. Namespace qualified type name of the resource</param>
/// <returns>Object representing a resource of given type and as referenced by the query</returns>
Object IUpdatable.GetResource(IQueryable query, String fullTypeName)
{
Object resource = null;

foreach (Object item in query)
{
if (resource != null)
{
throw (new DataServiceException("The query must return a single resource"));
}

resource = item;
}

if (resource == null)
{
throw (new DataServiceException(404, "Resource not found"));
}

// fullTypeName can be null for deletes
if ((fullTypeName != null) && (resource.GetType().FullName != fullTypeName))
{
throw (new Exception("Unexpected type for resource"));
}

return (resource);
}


/// <summary>
/// Resets the value of the given resource to its default value
/// </summary>
/// <param name="resource">resource whose value needs to be reset</param>
/// <returns>same resource with its value reset</returns>
Object IUpdatable.ResetResource(Object resource)
{
return (resource);
}

/// <summary>
/// Sets the value of the given property on the target Object
/// </summary>
/// <param name="targetResource">target Object which defines the property</param>
/// <param name="propertyName">name of the property whose value needs to be updated</param>
/// <param name="propertyValue">value of the property</param>
void IUpdatable.SetValue(Object targetResource, String propertyName, Object propertyValue)
{
PropertyInfo propertyInfo = targetResource.GetType().GetProperty(propertyName);
propertyInfo.SetValue(targetResource, propertyValue, null);

if (this.entitiesToUpdate.Contains(targetResource) == false)
{
this.entitiesToUpdate.Add(targetResource);
}
}

/// <summary>
/// Gets the value of the given property on the target Object
/// </summary>
/// <param name="targetResource">target Object which defines the property</param>
/// <param name="propertyName">name of the property whose value needs to be updated</param>
/// <returns>the value of the property for the given target resource</returns>
Object IUpdatable.GetValue(Object targetResource, String propertyName)
{
PropertyInfo propertyInfo = targetResource.GetType().GetProperty(propertyName);
return (propertyInfo.GetValue(targetResource, null));
}

/// <summary>
/// Sets the value of the given reference property on the target Object
/// </summary>
/// <param name="targetResource">target Object which defines the property</param>
/// <param name="propertyName">name of the property whose value needs to be updated</param>
/// <param name="propertyValue">value of the property</param>
void IUpdatable.SetReference(Object targetResource, String propertyName, Object propertyValue)
{
(this as IUpdatable).SetValue(targetResource, propertyName, propertyValue);
}

/// <summary>
/// Adds the given value to the collection
/// </summary>
/// <param name="targetResource">target Object which defines the property</param>
/// <param name="propertyName">name of the property whose value needs to be updated</param>
/// <param name="resourceToBeAdded">value of the property which needs to be added</param>
void IUpdatable.AddReferenceToCollection(Object targetResource, String propertyName, Object resourceToBeAdded)
{
PropertyInfo pi = targetResource.GetType().GetProperty(propertyName);

if (pi == null)
{
throw (new Exception("Can't find property"));
}

IList collection = pi.GetValue(targetResource, null) as IList;

if (collection != null)
{
collection.Add(resourceToBeAdded);
}

if (this.entitiesToUpdate.Contains(targetResource) == false)
{
this.entitiesToUpdate.Add(targetResource);
}
}

/// <summary>
/// Removes the given value from the collection
/// </summary>
/// <param name="targetResource">target Object which defines the property</param>
/// <param name="propertyName">name of the property whose value needs to be updated</param>
/// <param name="resourceToBeRemoved">value of the property which needs to be removed</param>
void IUpdatable.RemoveReferenceFromCollection(Object targetResource, String propertyName, Object resourceToBeRemoved)
{
PropertyInfo pi = targetResource.GetType().GetProperty(propertyName);

if (pi == null)
{
throw (new Exception("Can't find property"));
}

IList collection = pi.GetValue(targetResource, null) as IList;

if (collection != null)
{
collection.Remove(resourceToBeRemoved);
}

if (this.entitiesToUpdate.Contains(targetResource) == false)
{
this.entitiesToUpdate.Add(targetResource);
}
}

/// <summary>
/// Delete the given resource
/// </summary>
/// <param name="targetResource">resource that needs to be deleted</param>
void IUpdatable.DeleteResource(Object targetResource)
{
this.entitiesToRemove.Add(targetResource);
}

/// <summary>
/// Saves all the pending changes made till now
/// </summary>
void IUpdatable.SaveChanges()
{
FlushMode originalFlushMode = this.Session.FlushMode;

using (ITransaction transaction = this.Session.BeginTransaction())
{
this.Session.FlushMode = FlushMode.Commit;

foreach (Object entity in this.entitiesToUpdate)
{
this.Session.SaveOrUpdate(entity);
}

foreach (Object entity in this.entitiesToRemove)
{
this.Session.Delete(entity);
}

transaction.Commit();
}

this.Session.FlushMode = originalFlushMode;
}

/// <summary>
/// Returns the actual instance of the resource represented by the given resource Object
/// </summary>
/// <param name="resource">Object representing the resource whose instance needs to be fetched</param>
/// <returns>The actual instance of the resource represented by the given resource Object</returns>
Object IUpdatable.ResolveResource(Object resource)
{
return (resource);
}

/// <summary>
/// Revert all the pending changes.
/// </summary>
void IUpdatable.ClearChanges()
{
ISessionImplementor impl = this.Session.GetSessionImplementation();
IPersistenceContext ctx = impl.PersistenceContext;

foreach (Object obj in ctx.EntityEntries.Keys.OfType<Object>().ToArray())
{
this.Session.Evict(obj);

if (this.entitiesToUpdate.Contains(obj) == true)
{
this.entitiesToUpdate.Remove(obj);
}

if (this.entitiesToRemove.Contains(obj) == true)
{
this.entitiesToRemove.Remove(obj);
}
}
}
#endregion

#region IDisposable members
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}