Can I add a common function to a set of EntitySet<TEntity> Classes - LINQ to SQL
I am using LINQ to SQL
and want to add functions to autogenerated EntitySet< TEntity >
collections.
eg.
City.Houses
where Houses
is automatically generated EntitySet < House >
I am using extension method to add extract function to the EntitySet < House >
class
so that I can get something like
City.House.FindByID(id);
but now I also have
City.Bunglows.FindByID(id);
now FindByID
basically does the same thing for both these classes.
Will I have to extend Bunglows
also and implement the same function again. Can't I do some kind of inhertiance on the autogenerated EntitySet< TEntity >
classes??
Commenting on Andrew Hare's answer
But that will make FindByID() available on all EntitySet< TEntity > classes. What if I want to implement that function on these 2 particular entity sets only???
Make your extension method generic like this:
public static TEntity FindByID<TEntity>
(this EntitySet<TEntity> source, Int32 id)
{
// ...
}
This will allow you to use this extension method on any closed constructed instance of EntitySet<TEntity>
(i.e. EntitySet<House>
or EntitySet<Bungalow>
) as long as your implementation of FindByID
can be appropriately expressed generically.
精彩评论