API thoughts... what if session.Store() returned the entity back?

30 views
Skip to first unread message

Jim Bolla

unread,
Jun 1, 2012, 12:26:52 PM6/1/12
to rav...@googlegroups.com
I have a lot of code, especially in tests, that looks like this pattern...

var someEntity = new SomeClass {
// set props
};
_session.Store(someEntity);

// do more stuff with someEntity

What would be kinda nice if I could do this:

var someEntity = _session.Store(new SomeClass {
// set props
});

// do more stuff with someEntity

This would save a bunch of lines of code. Alternatively, maybe this?

var someEntity = new SomeClass {
// set props
}.Store(_session);

// do more stuff with someEntity

This style could be done with an extension method that doesn't change the existing API. Thoughts? Do either of these make the code better or worse?


Chris Marisic

unread,
Jun 1, 2012, 12:29:58 PM6/1/12
to rav...@googlegroups.com


On Friday, June 1, 2012 12:26:52 PM UTC-4, Jim Bolla wrote:
What would be kinda nice if I could do this:

var someEntity = _session.Store(new SomeClass {
// set props
});

// do more stuff with someEntity


You can write an extension method that does that

 
This would save a bunch of lines of code. Alternatively, maybe this?

var someEntity = new SomeClass {
// set props
}.Store(_session);

// do more stuff with someEntity



I personally would never extend object to have Store.

Jim Bolla

unread,
Jun 1, 2012, 3:32:31 PM6/1/12
to rav...@googlegroups.com
The first option would have to be named differently to be implemented as an extension. Otherwise the compiler thinks you want the default overload. I also don't care too much for the second option.


Chris Marisic

unread,
Jun 1, 2012, 4:26:53 PM6/1/12
to rav...@googlegroups.com


On Friday, June 1, 2012 3:32:31 PM UTC-4, Jim Bolla wrote:
The first option would have to be named differently to be implemented as an extension. Otherwise the compiler thinks you want the default overload.

Really? I would think the compiler would be smart enough to understand

var x = Session.Store()

Should dispatch to the extension method because the normal method is void. Did you verify the using namespace was set to include your extension method that you tested?

Mauro Servienti

unread,
Jun 1, 2012, 5:32:48 PM6/1/12
to rav...@googlegroups.com
Overloading on return type is not supported in .net.

.m
Inviata dal mio Windows Phone

Da: Chris Marisic
Inviato: 01/06/2012 22:27
A: rav...@googlegroups.com
Oggetto: [RavenDB] Re: API thoughts... what if session.Store() returned the entity back?

Itamar Syn-Hershko

unread,
Jun 2, 2012, 5:53:57 PM6/2/12
to rav...@googlegroups.com
Do the first using an extension method

Store should just store. Having it return a reference to the object might confuse people and I really think we should avoid that
Reply all
Reply to author
Forward
0 new messages