简体   繁体   中英

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> (ie EntitySet<House> or EntitySet<Bungalow> ) as long as your implementation of FindByID can be appropriately expressed generically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM