Skip to content

Instantly share code, notes, and snippets.

@kellypleahy
Created September 21, 2009 17:19
Show Gist options
  • Select an option

  • Save kellypleahy/190382 to your computer and use it in GitHub Desktop.

Select an option

Save kellypleahy/190382 to your computer and use it in GitHub Desktop.
public interface ICommandRepository<T>
{
void SaveOrUpdate<U>(U item) where U: T;
void Delete<U>(U item) where U: T;
}
/* then, library code asks for ICommandRepository<Core.Customer> and so do clients.
However, when clients call SaveOrUpdate, they'll be passing a Extended.Customer,
but when library code calls it, they'll be passing what they think is a Core.Customer
(though it will still be a Extended.Customer, of course).
this is not the real problem, though, since ICommandRespository doesn't even need
SaveOrUpdate to be generic (you could easily make the argument be of type Core.Customer
and have no issues). The real problem is IQueryRepository - it needs to be:
*/
public interface IQueryRepository<T>
{
U Get(int id) where U: T
...
}
/* and then the library code will call Get<Core.Customer> and the extension code
will call Get<Extended.Customer>. The tricky part is getting NH set up right so
that when you ask for a Core.Customer, it gives you an Extended.Customer. I'm not
sure if this is possible or not, but I suppose it must be. Personally, I define
"Get" like this:
void Get(int id, out U target) where U: T
so that callers don't have to pass the generic argument, but this makes a bit
of a mess of it's own since you can't use "var" with this code. So you should try
both and decide which you like less and throw that one away ;)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment