Dynamic Proxy : wrapping constructors

230 Views Asked by At

I'm taking a stab at creating a Active Record implementation (I know about Castle's initiative, and it's very good) for another type of data provider (ESRIs geodatabases, using ESRIs .NET libraries) and I'm reaching something interesting.

I have a question nevertheless. I have my ActiveRecord classes which is like this:

public interface IActiveRecord<T> : IActiveRecord where T : class
{
    T Create();
    void Update(T record);
    void Delete(T record);
}

public interface IActiveRecord
{
    int ObjectId { get; set; }
    bool Managed { get; }
    bool IsValid { get; }

    IObject EsriObject { get; set; }

    IGeometry Geometry { get; set; }

    void Save();
    void Delete();
}

I have static Create methods, which go to DynamicProxy and generate me a proxy. But how I can enforce that the instance generated for a inheriting class is proxied too?

public class ActiveRecord<T> : IActiveRecord where T : IActiveRecord,new()
{

    //  protected constructors

    public static T Create(IObject obj)
    {
        var record = Create();
        record.EsriObject = obj;
        return (T)record;
    }
}

// inherited class
[Workspace(@"C:\teste.gdb")]
[Table("TB_PARCEL",Geometry=esriGeometryType.esriGeometryPolygon)]
public class Parcel : ActiveRecord<Parcel>,IParcel
{
    [Field(4, "NM_PARCEL_ID", esriFieldType.esriFieldTypeString)]
    public virtual string ParcelId { get; set; }

    [Field(5, "NR_PARCEL_NO", esriFieldType.esriFieldTypeInteger)]
    public virtual int StreetNumber { get; set; }
    public virtual IOwner ParcelOwner { get; set; }
}

Take a look at the tests. The first three tests get intercepted as usual, but not the fourth test. I need A) prevent the user from instancing it's own classes (bad approach for the API in my opinion) or find a way to return from the inherited classes constructors the proxies.

    [TestMethod]
    public void ActiveRecordConstructor()
    {
        Parcel p1 = Parcel.Create();
        Assert.IsFalse(p1.Managed);
        Assert.AreEqual(null, p1.ParcelId);

        Parcel p2 = Parcel.Create(2);
        Assert.IsFalse(p2.Managed);

        IObject fake = _repository.StrictMock<IObject>();

        using (_repository.Record())
        {
            fake.Stub(x => x.get_Value(4)).Return("teste");
        }

        using (_repository.Playback())
        {
            Parcel p3 = Parcel.Create(fake);
            Assert.IsTrue(p3.Managed);
            Assert.AreEqual("teste", p3.ParcelId);
        }

        // this wont be intercepted
        Parcel p4 = new Parcel();
        Assert.IsFalse(p4.Managed);
        Assert.IsNull(p4.ParcelId);
    }

In short I need that whenever a user creates a new Class(), it returns a proxied object. Is that possible while allowing inheritance?

Thanks!

1

There are 1 best solutions below

0
On

DynamicProxy cannot intercept calls to constructors. It has to control the creation of the object.