Issue implementing future pattern with linfu dynamic proxy

0 views
Skip to first unread message

mcintyre321

unread,
Jun 5, 2009, 10:59:13 AM6/5/09
to LinFu.Framework
I'm trying to write a Future pattern proxy which lazy loads a value
when it is called. It works (see the first test), but only if I
explicitly cast my create delegate. Is there some way to make the
second test pass without changing it?

internal class Future
{

public static T Create<T>(Func<T> get) where T : class
{
ProxyFactory factory = new ProxyFactory();
var interceptor = new GenericFuture<T>(get);
return factory.CreateProxy<T>(interceptor);
}

internal class GenericFuture<T> : IInterceptor
{
private readonly Func<T> getter;
private object instance;

public GenericFuture(Func<T> getter)
{
this.getter = getter;
}

public object Intercept(InvocationInfo info)
{
var target = instance ?? (instance = getter());
return info.TargetMethod.Invoke(target,
info.Arguments);
}
}
}

public class FutureTests
{
[Fact]
public void TestThatDoesntPass()
{
List<string> list = new List<string>() { "hello" };
IList<string> lazyList = Future.Create(() => list as
IList<string>); //<-- notice the cast
list[0] = "goodbye";
Assert.Equal("goodbye", lazyList[0]);
}

[Fact]
public void TestThatIWantToPassButInsteadThrowsAnException()
{
List<string> list = new List<string>() { "hello" };
IList<string> lazyList = Future.Create(() => list);
list[0] = "goodbye";
Assert.Equal("goodbye", lazyList[0]);
}
}
Reply all
Reply to author
Forward
0 new messages