Skip to content

NH-3722 - Remove entity mode switching capability #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 26, 2017
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ current-test-configuration
# to satisfy later build steps. But it should not be committed.
NHibernate.dll
TestResult.xml
.vscode
43 changes: 3 additions & 40 deletions doc/reference/modules/persistent_classes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,11 @@ namespace Eg
(using <literal>Dictionaries</literal> of <literal>Dictionary</literal>s at runtime) . With this approach, you don't
write persistent classes, only mapping files.
</para>

<para>
By default, NHibernate works in normal POCO mode. You may set a default entity
representation mode for a particular <literal>ISessionFactory</literal> using the
<literal>default_entity_mode</literal> configuration option (see
<xref linkend="configuration-optional-properties"/>.
</para>


<para>
The following examples demonstrates the representation using <literal>Map</literal>s (Dictionary).
First, in the mapping file, an <literal>entity-name</literal> has to be declared
instead of (or in addition to) a class name:
instead of a class name:
</para>

<programlisting><![CDATA[<hibernate-mapping>
Expand Down Expand Up @@ -305,16 +298,13 @@ namespace Eg
</hibernate-mapping>]]></programlisting>

<para>

Note that even though associations are declared using target class names,
the target type of an associations may also be a dynamic entity instead
of a POCO.
</para>

<para>
After setting the default entity mode to <literal>dynamic-map</literal>
for the <literal>ISessionFactory</literal>, we can at runtime work with
<literal>Dictionaries</literal> of <literal>Dictionaries</literal>:
At runtime we can work with <literal>Dictionaries</literal> of <literal>Dictionaries</literal>:
</para>

<programlisting><![CDATA[using(ISession s = OpenSession())
Expand Down Expand Up @@ -345,33 +335,6 @@ using(ITransaction tx = s.BeginTransaction())
to the NHibernate mapping, the database schema can easily be normalized and sound,
allowing to add a proper domain model implementation on top later on.
</para>

<para>
Entity representation modes can also be set on a per <literal>ISession</literal>
basis:
</para>

<programlisting><![CDATA[using (ISession dynamicSession = pocoSession.GetSession(EntityMode.Map))
{
// Create a customer
var frank = new Dictionary<string, object>();
frank["name"] = "Frank";
dynamicSession.Save("Customer", frank);
...
}
// Continue on pocoSession
]]></programlisting>


<para>
Please note that the call to <literal>GetSession()</literal> using an
<literal>EntityMode</literal> is on the <literal>ISession</literal> API, not the
<literal>ISessionFactory</literal>. That way, the new <literal>ISession</literal>
shares the underlying ADO connection, transaction, and other context
information. This means you don't have to call <literal>Flush()</literal>
and <literal>Close()</literal> on the secondary <literal>ISession</literal>, and
also leave the transaction and connection handling to the primary unit of work.
</para>
</sect1>

<sect1 id="persistent-classes-tuplizers" revision="1">
Expand Down
90 changes: 28 additions & 62 deletions src/NHibernate.DomainModel/CustomPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public CustomPersister(PersistentClass model, ICacheConcurrencyStrategy cache, I
this.factory = factory;
}

private static void CheckEntityMode(EntityMode entityMode)
{
if (EntityMode.Poco != entityMode)
{
throw new ArgumentOutOfRangeException("entityMode", "Unhandled EntityMode : " + entityMode);
}
}

#region IEntityPersister Members

public ISessionFactoryImplementor Factory
Expand Down Expand Up @@ -377,21 +369,11 @@ public object ForceVersionIncrement(object id, object currentVersion, ISessionIm
return null;
}

public EntityMode? GuessEntityMode(object obj)
{
if (!IsInstance(obj, EntityMode.Poco))
{
return null;
}
else
{
return EntityMode.Poco;
}
}
public EntityMode EntityMode => EntityMode.Poco;

public bool IsInstrumented(EntityMode entityMode)
public bool IsInstrumented
{
return false;
get { return false; }
}

public bool HasInsertGeneratedProperties
Expand Down Expand Up @@ -424,7 +406,7 @@ public object CreateProxy(object id, ISessionImplementor session)

public object[] GetPropertyValuesToInsert(object obj, IDictionary mergeMap, ISessionImplementor session)
{
return GetPropertyValues(obj, session.EntityMode);
return GetPropertyValues(obj);
}

public void ProcessInsertGeneratedProperties(object id, object entity, object[] state, ISessionImplementor session)
Expand All @@ -435,109 +417,91 @@ public void ProcessUpdateGeneratedProperties(object id, object entity, object[]
{
}

public System.Type GetMappedClass(EntityMode entityMode)
public System.Type MappedClass
{
CheckEntityMode(entityMode);
return typeof(Custom);
get { return typeof(Custom); }
}

public bool ImplementsLifecycle(EntityMode entityMode)
public bool ImplementsLifecycle
{
CheckEntityMode(entityMode);
return false;
get { return false; }
}

public bool ImplementsValidatable(EntityMode entityMode)
public bool ImplementsValidatable
{
CheckEntityMode(entityMode);
return false;
get { return false; }
}

public System.Type GetConcreteProxyClass(EntityMode entityMode)
public System.Type ConcreteProxyClass
{
CheckEntityMode(entityMode);
return typeof (Custom);
get { return typeof(Custom); }
}

public void SetPropertyValues(object obj, object[] values, EntityMode entityMode)
public void SetPropertyValues(object obj, object[] values)
{
CheckEntityMode(entityMode);
SetPropertyValue(obj, 0, values[0], entityMode);
SetPropertyValue(obj, 0, values[0]);
}

public void SetPropertyValue(object obj, int i, object value, EntityMode entityMode)
public void SetPropertyValue(object obj, int i, object value)
{
CheckEntityMode(entityMode);
((Custom) obj).Name = (string) value;
}

public object[] GetPropertyValues(object obj, EntityMode entityMode)
public object[] GetPropertyValues(object obj)
{
CheckEntityMode(entityMode);
Custom c = (Custom) obj;
return new Object[] {c.Name};
}

public object GetPropertyValue(object obj, int i, EntityMode entityMode)
public object GetPropertyValue(object obj, int i)
{
CheckEntityMode(entityMode);
return ((Custom)obj).Name;
}

public object GetPropertyValue(object obj, string name, EntityMode entityMode)
public object GetPropertyValue(object obj, string name)
{
CheckEntityMode(entityMode);
return ((Custom)obj).Name;
}

public object GetIdentifier(object obj, EntityMode entityMode)
public object GetIdentifier(object obj)
{
CheckEntityMode(entityMode);
return ((Custom)obj).Id;
}

public void SetIdentifier(object obj, object id, EntityMode entityMode)
public void SetIdentifier(object obj, object id)
{
CheckEntityMode(entityMode);
((Custom) obj).Id = (string) id;
}

public object GetVersion(object obj, EntityMode entityMode)
public object GetVersion(object obj)
{
CheckEntityMode(entityMode);
return null;
}

public object Instantiate(object id, EntityMode entityMode)
public object Instantiate(object id)
{
CheckEntityMode(entityMode);
Custom c = new Custom();
c.Id = (string)id;
return c;
}

public bool IsInstance(object entity, EntityMode entityMode)
public bool IsInstance(object entity)
{
CheckEntityMode(entityMode);
return entity is Custom;
}

public bool HasUninitializedLazyProperties(object obj, EntityMode entityMode)
public bool HasUninitializedLazyProperties(object obj)
{
CheckEntityMode(entityMode);
return false;
}

public void ResetIdentifier(object entity, object currentId, object currentVersion, EntityMode entityMode)
public void ResetIdentifier(object entity, object currentId, object currentVersion)
{
CheckEntityMode(entityMode);
((Custom)entity).Id = (string)currentId;
}

public IEntityPersister GetSubclassEntityPersister(object instance, ISessionFactoryImplementor factory,
EntityMode entityMode)
public IEntityPersister GetSubclassEntityPersister(object instance, ISessionFactoryImplementor factory)
{
CheckEntityMode(entityMode);
return this;
}

Expand All @@ -553,6 +517,8 @@ public IComparer VersionComparator
get { return null; }
}

public IEntityTuplizer EntityTuplizer => null;

#endregion
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/CacheTest/CacheFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TestSimpleCache()

private CacheKey CreateCacheKey(string text)
{
return new CacheKey(text, NHibernateUtil.String, "Foo", EntityMode.Poco, null);
return new CacheKey(text, NHibernateUtil.String, "Foo", null);
}

public void DoTestCache(ICacheProvider cacheProvider)
Expand Down
12 changes: 6 additions & 6 deletions src/NHibernate.Test/CacheTest/FilterKeyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public void ToStringIncludeAll()
string filterName = "DescriptionLike";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pLike", "so%");
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
Assert.That(fk.ToString(), Is.EqualTo("FilterKey[DescriptionLike{'pLike'='so%'}]"));

filterName = "DescriptionEqualAndValueGT";
f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pDesc", "something").SetParameter("pValue", 10);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
Assert.That(fk.ToString(), Is.EqualTo("FilterKey[DescriptionEqualAndValueGT{'pDesc'='something', 'pValue'='10'}]"));
}

Expand All @@ -51,23 +51,23 @@ private void FilterDescLikeToCompare(out FilterKey fk, out FilterKey fk1)
const string filterName = "DescriptionLike";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pLike", "so%");
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);

var f1 = new FilterImpl(sessions.GetFilterDefinition(filterName));
f1.SetParameter("pLike", "%ing");
fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
}

private void FilterDescValueToCompare(out FilterKey fk, out FilterKey fk1)
{
const string filterName = "DescriptionEqualAndValueGT";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pDesc", "something").SetParameter("pValue", 10);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);

var f1 = new FilterImpl(sessions.GetFilterDefinition(filterName));
f1.SetParameter("pDesc", "something").SetParameter("pValue", 11);
fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
}

[Test]
Expand Down
16 changes: 8 additions & 8 deletions src/NHibernate.Test/CacheTest/QueryKeyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ private void QueryKeyFilterDescLikeToCompare(out QueryKey qk, out QueryKey qk1)
const string filterName = "DescriptionLike";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pLike", "so%");
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
ISet<FilterKey> fks = new HashSet<FilterKey> { fk };
qk = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);

var f1 = new FilterImpl(sessions.GetFilterDefinition(filterName));
f1.SetParameter("pLike", "%ing");
var fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
fks = new HashSet<FilterKey> { fk1 };
qk1 = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);
}
Expand All @@ -57,13 +57,13 @@ private void QueryKeyFilterDescValueToCompare(out QueryKey qk, out QueryKey qk1)

var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pDesc", "something").SetParameter("pValue", 10);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
ISet<FilterKey> fks = new HashSet<FilterKey> { fk };
qk = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);

var f1 = new FilterImpl(sessions.GetFilterDefinition(filterName));
f1.SetParameter("pDesc", "something").SetParameter("pValue", 11);
var fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk1 = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
fks = new HashSet<FilterKey> { fk1 };
qk1 = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);
}
Expand Down Expand Up @@ -111,15 +111,15 @@ public void ToStringWithFilters()
string filterName = "DescriptionLike";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pLike", "so%");
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
ISet<FilterKey> fks = new HashSet<FilterKey> { fk };
var qk = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);
Assert.That(qk.ToString(), Does.Contain(string.Format("filters: ['{0}']",fk)));

filterName = "DescriptionEqualAndValueGT";
f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pDesc", "something").SetParameter("pValue", 10);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);
fks = new HashSet<FilterKey> { fk };
qk = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);
Assert.That(qk.ToString(), Does.Contain(string.Format("filters: ['{0}']", fk)));
Expand All @@ -131,12 +131,12 @@ public void ToStringWithMoreFilters()
string filterName = "DescriptionLike";
var f = new FilterImpl(sessions.GetFilterDefinition(filterName));
f.SetParameter("pLike", "so%");
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);

filterName = "DescriptionEqualAndValueGT";
var fv = new FilterImpl(sessions.GetFilterDefinition(filterName));
fv.SetParameter("pDesc", "something").SetParameter("pValue", 10);
var fvk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes, EntityMode.Poco);
var fvk = new FilterKey(filterName, f.Parameters, f.FilterDefinition.ParameterTypes);

ISet<FilterKey> fks = new HashSet<FilterKey> { fk, fvk };
var qk = new QueryKey(sessions, SqlAll, new QueryParameters(), fks, null);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/Criteria/AddNumberProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public AddNumberProjection(string propertyName, int numberToAdd)
{
this.propertyName = propertyName;
this.numberToAdd = numberToAdd;
typedValue = new TypedValue(NHibernateUtil.Int32, this.numberToAdd, EntityMode.Poco);
typedValue = new TypedValue(NHibernateUtil.Int32, this.numberToAdd);
}

public override bool IsAggregate
Expand Down
Loading