Creating Proxy Objects after the document has been converted to an entity.

57 views
Skip to first unread message

JT

unread,
May 16, 2012, 6:03:16 PM5/16/12
to rav...@googlegroups.com
I am using StructureMap and Castle for DI/IoC/Proxy Objects. When retrieving a document, I would like to generate a proxy on that object so I can still utilize interceptors for things like INPC and Logging. I implemented IDocumentConversionListener, and added the logic to create a proxy object, but RavenDb is still handing back the non-proxied object.

public void DocumentToEntity(object entity, Raven.Json.Linq.RavenJObject document, Raven.Json.Linq.RavenJObject metadata) {

var proxyModel = ProxyGenerator.CreateClassProxyWithTarget(
entity.GetType(),
new Type[] { typeof(INotifyPropertyChanged) },
entity,
ProxyGenerationOptions.Default,
new ModelInterceptor());

entity = proxyModel;

}

Is it not possible to manipulate the object in this listener? Or should I just be doing this in a completely different place?

Thanks for your time,
JT

Oren Eini (Ayende Rahien)

unread,
May 16, 2012, 7:42:43 PM5/16/12
to rav...@googlegroups.com
JT,
No, this isn't how you would do this.
You aren't replacing the entity that we hold.
If you want to do that, you need to go through the Json Serializer.
Here is the docs on how to do this:

JT

unread,
May 16, 2012, 9:00:39 PM5/16/12
to rav...@googlegroups.com
Having a hard time finding documentation on these listeners. Can you explain what the benefit is to having the entity object being passed? Trying to figure out why this would be exposed to me if it isn't the actual 'entity' that get's passed to me from RavenDb.

Oren Eini (Ayende Rahien)

unread,
May 17, 2012, 4:37:00 AM5/17/12
to rav...@googlegroups.com
This allows you to do things like set properties on the entity that comes from the metadata.

JT

unread,
May 17, 2012, 9:37:56 AM5/17/12
to rav...@googlegroups.com
Ok thanks. I'm looking forward to more documentation on listeners. Or even introducing some new listeners that will let you manipulate the actual entity that gets casted to the domain object during a session.Load<TheObject>(id);

I guess I'm a bit spoiled by NHibernate which gave us the ability to attach our own interceptors to the proxied object being handed back. 

As of right now, it looks like I have to proxy the object manually after every query, but I'm going to look a bit deeper into the JsonSerializer & play with the metadata coming back.

On Wednesday, May 16, 2012 6:03:16 PM UTC-4, JT wrote:

Bryan Johns

unread,
May 17, 2012, 9:52:17 AM5/17/12
to rav...@googlegroups.com
I wonder if AutoMapper would help with what you're trying to do?

According to the AutoMapper.org website it is, "...built to solve a
deceptively complex problem - getting rid of code that mapped one
object to another."

Disclaimer: I am not associated with, nor have I ever actually used AutoMapper.

--
Bryan Johns
K4GDW
http://www.greendragonweb.com

Do not meddle in the affairs of dragons, for you are crunchy and taste
good with ketchup.

Oren Eini (Ayende Rahien)

unread,
May 17, 2012, 9:52:07 AM5/17/12
to rav...@googlegroups.com
You HAVE the ability to do that.
Here is how:

http://james.newtonking.com/projects/json/help/CustomCreationConverter.html

You attach this behavior to the docStore.Conventison.CustomizeSerializer event.

JT

unread,
May 17, 2012, 10:18:18 AM5/17/12
to rav...@googlegroups.com
Interesting project. The issue I am running into is needing to grab and manipulate my object before the query returns it to the UI/Webservice, etc..

So I would still run into this scenario using something like AutoMapper

JT

unread,
May 17, 2012, 10:20:03 AM5/17/12
to rav...@googlegroups.com
Thanks, the  docStore.Conventison.CustomizeSerializer  is what I was having a hard time finding.

Will play with that now. 

JT

unread,
May 17, 2012, 11:49:08 AM5/17/12
to rav...@googlegroups.com
Well, after learning a LOT more about the JSON library and following your advice I figured it out!

You saved me once again Oren!
Reply all
Reply to author
Forward
0 new messages