Skip to content

Commit

Permalink
applied Dan Jasek's patch for AR-297
Browse files Browse the repository at this point in the history
  • Loading branch information
hconceicao committed Mar 14, 2011
1 parent fbe2f25 commit e5aca61
Show file tree
Hide file tree
Showing 16 changed files with 239 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Castle.ActiveRecord.Framework.Internal.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Initial Catalog=test;Data Source=.;Integrated Security=True;" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
<!--add key="connection.connection_string" value="Data Source=.;Initial Catalog=ActiverRecordTest;Integrated Security=True;Pooling=False" /-->
</config>
Expand Down
6 changes: 3 additions & 3 deletions Castle.ActiveRecord.Tests/App-net-2.0.config
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.\SQLExpress;Initial Catalog=Test;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>

Expand All @@ -37,7 +37,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.\SQLExpress;Initial Catalog=Test2;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>

Expand All @@ -46,7 +46,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=Test2;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>

Expand Down
6 changes: 3 additions & 3 deletions Castle.ActiveRecord.Tests/App-net-3.5.config
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.\SQLExpress;Initial Catalog=Test;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>

Expand All @@ -37,7 +37,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.\SQLExpress;Initial Catalog=Test2;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>

Expand All @@ -46,7 +46,7 @@
<add key="dialect" value="NHibernate.Dialect.MSSql2005Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=Test2;Integrated Security=True" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord" />
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate" />
</config>
</activerecord>
Expand Down
6 changes: 3 additions & 3 deletions Castle.ActiveRecord.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=Test;Integrated Security=True"/>
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord"/>
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate"/>
</config>

Expand All @@ -22,7 +22,7 @@
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=Test2;Integrated Security=True"/>
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord"/>
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate"/>
</config>

Expand All @@ -31,7 +31,7 @@
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="connection.connection_string" value="Data Source=.;Initial Catalog=Test2;Integrated Security=True"/>
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
<add key="proxyfactory.factory_class" value="Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord"/>
<add key="query.factory_class" value="NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory, NHibernate"/>
</config>

Expand Down
2 changes: 1 addition & 1 deletion Castle.ActiveRecord.Tests/Config/ConfigureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Castle.ActiveRecord.Tests.Config
using Castle.ActiveRecord.Framework.Scopes;
using NUnit.Framework;
using Castle.ActiveRecord.Framework.Config;
using NHibernate.ByteCode.Castle;
using Castle.ActiveRecord.ByteCode;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
Expand Down
4 changes: 2 additions & 2 deletions Castle.ActiveRecord.Tests/Config/StorageConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Castle.ActiveRecord.Tests.Config
{
using Castle.ActiveRecord.Framework.Config;
using NHibernate.ByteCode.Castle;
using Castle.ActiveRecord.ByteCode;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
Expand Down Expand Up @@ -74,7 +74,7 @@ public void DialectIsAdded()
public void ProxyFactoryIsAdded()
{
AssertConfiguration(_configuration.ProxiedBy<ProxyFactoryFactory>(), "proxyfactory.factory_class",
"NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
"Castle.ActiveRecord.ByteCode.ProxyFactoryFactory, Castle.ActiveRecord");
}
}
}
2 changes: 1 addition & 1 deletion Castle.ActiveRecord.Tests/DefaultConfigurationsTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace Castle.ActiveRecord.Tests

using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord.ByteCode;

using NHibernate.ByteCode.Castle;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
Expand Down
21 changes: 20 additions & 1 deletion Castle.ActiveRecord.Tests/LazyTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void CanSaveAndLoadLazyEntityOutsideOfScope()

PostLazy postFromDb = PostLazy.Find(post.Id);
Assert.AreEqual("a", postFromDb.Title);

}

[Test]
Expand Down Expand Up @@ -84,5 +83,25 @@ public void CanLoadLazyProperty()
Assert.AreEqual(teststring, System.Text.Encoding.UTF8.GetString(fromDb));
}
}

[Test]
public void CanLoadLazyBelongsToOutsideOfScope() {
ActiveRecordStarter.Initialize(GetConfigSource(),
typeof(ObjectWithLazyAssociation), typeof(VeryLazyObject));
Recreate();

var lazy = new VeryLazyObject();
lazy.Title = "test";
ActiveRecordMediator.Save(lazy);

var obj = new ObjectWithLazyAssociation();
obj.LazyObj = lazy;
ActiveRecordMediator.Save(obj);

var objFromDb = (ObjectWithLazyAssociation)ActiveRecordMediator.FindByPrimaryKey(typeof(ObjectWithLazyAssociation), obj.Id);
Assert.False(NHibernate.NHibernateUtil.IsInitialized(objFromDb.LazyObj));
Assert.AreEqual("test", objFromDb.LazyObj.Title);
Assert.True(NHibernate.NHibernateUtil.IsInitialized(objFromDb.LazyObj));
}
}
}
33 changes: 33 additions & 0 deletions Castle.ActiveRecord.Tests/Model/ObjectWithLazyAssociation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.ActiveRecord.Tests.Model {
[ActiveRecord]
public class ObjectWithLazyAssociation {
private int id;
private VeryLazyObject lazyObj;

[PrimaryKey]
public int Id {
get { return id; }
set { id = value; }
}

[BelongsTo(Lazy = FetchWhen.OnInvoke)]
public VeryLazyObject LazyObj {
get { return lazyObj; }
set { lazyObj = value; }
}
}
}
50 changes: 50 additions & 0 deletions Castle.ActiveRecord/ByteCode/LazyInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Reflection;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Type;

namespace Castle.ActiveRecord.ByteCode
{
class LazyInitializer : NHibernate.ByteCode.Castle.LazyInitializer
{
public LazyInitializer(string entityName, Type persistentClass, object id,
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
IAbstractComponentType componentIdType, ISessionImplementor session) :
base(entityName, persistentClass, id, getIdentifierMethod, setIdentifierMethod, componentIdType, session) { }

/// <summary>
/// Perform an ImmediateLoad of the actual object for the Proxy.
/// </summary>
public override void Initialize() {
ISession newSession = null;
try
{
//If the session has been disconnected, reconnect before continuing with the initialization.
if (Session == null || !Session.IsOpen || !Session.IsConnected) {
newSession = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(PersistentClass);
Session = newSession.GetSessionImplementation();
}
base.Initialize();
}
finally
{
if (newSession != null) ActiveRecordMediator.GetSessionFactoryHolder().ReleaseSession(newSession);
}
}
}
}
71 changes: 71 additions & 0 deletions Castle.ActiveRecord/ByteCode/ProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Castle.DynamicProxy;
using NHibernate;
using NHibernate.ByteCode.Castle;
using NHibernate.Engine;
using NHibernate.Proxy;

namespace Castle.ActiveRecord.ByteCode
{
class ProxyFactory : AbstractProxyFactory
{

protected static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof(ProxyFactory));
private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator();

protected static ProxyGenerator DefaultProxyGenerator
{
get { return ProxyGenerator; }
}

/// <summary>
/// Build a proxy using the Castle.DynamicProxy library.
/// </summary>
/// <param name="id">The value for the Id.</param>
/// <param name="session">The Session the proxy is in.</param>
/// <returns>A fully built <c>INHibernateProxy</c>.</returns>
public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
{
try
{
var initializer = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod,
SetIdentifierMethod, ComponentIdType, session);

object generatedProxy = IsClassProxy
? ProxyGenerator.CreateClassProxy(PersistentClass, Interfaces, initializer)
: ProxyGenerator.CreateInterfaceProxyWithoutTarget(Interfaces[0], Interfaces,
initializer);

initializer._constructed = true;
return (INHibernateProxy)generatedProxy;
}
catch (Exception e)
{
log.Error("Creating a proxy instance failed", e);
throw new HibernateException("Creating a proxy instance failed", e);
}
}

public override object GetFieldInterceptionProxy()
{
var proxyGenerationOptions = new ProxyGenerationOptions();
var interceptor = new LazyFieldInterceptor();
proxyGenerationOptions.AddMixinInstance(interceptor);
return ProxyGenerator.CreateClassProxy(PersistentClass, proxyGenerationOptions, interceptor);
}
}
}
42 changes: 42 additions & 0 deletions Castle.ActiveRecord/ByteCode/ProxyFactoryFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using NHibernate.Bytecode;
using NHibernate.Proxy;

namespace Castle.ActiveRecord.ByteCode
{
/// <summary>
/// The factory infrastructure used to build AR proxy objects.
/// Use this one if you want automatic session management durring proxy hydration.
/// </summary>
public class ProxyFactoryFactory : IProxyFactoryFactory
{

public IProxyFactory BuildProxyFactory()
{
return new ProxyFactory();
}

public IProxyValidator ProxyValidator
{
get { return new DynProxyTypeValidator(); }
}

public bool IsInstrumented(System.Type entityClass)
{
return true;
}
}
}
3 changes: 3 additions & 0 deletions Castle.ActiveRecord/Castle.ActiveRecord-vs2010.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@
<Compile Include="Attributes\WithAccessAttribute.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="ByteCode\LazyInitializer.cs" />
<Compile Include="ByteCode\ProxyFactory.cs" />
<Compile Include="ByteCode\ProxyFactoryFactory.cs" />
<Compile Include="Framework\ActiveRecordBase.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace Castle.ActiveRecord.Framework.Config
using System.Collections.Generic;
using System.Data;

using NHibernate.ByteCode.Castle;
using Castle.ActiveRecord.ByteCode;

using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.ActiveRecord.Framework.Config
namespace Castle.ActiveRecord.Framework.Config
{
using Castle.ActiveRecord.ByteCode;

using NHibernate.Bytecode;
using NHibernate.ByteCode.Castle;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
Expand Down
Loading

0 comments on commit e5aca61

Please sign in to comment.