1

I'm using LinqToSql in one of my projects and I want a bunch of tables to do something when they are loaded.

Each table implements a partial class an inherits a base class that does some business logic.

My code looks something like this:

public partial class Table1 : MyBaseClass {}
public partial class Table2 : MyBaseClass {}
..
public partial class Table150 : MyBaseClass {}

public abstract class MyBaseClass
{
   public void OnLoaded()
   {
      // do something
   }

   // more code
}

The partial void OnLoaded method suits me, but I don't want to re-implement the method on every table.

Since I didn't find a way to do things it will make me implement my tables and look something like this:

public partial class Table1 : MyBaseClass
{
     // This code is "redundant"
     public partial void OnLoaded() // partial method
     {
          base.OnLoaded(); // non partial base method
     }
}

I can't use a code generator like T4Templates or something since I don't know the exact table names that I need plus some tables will need to do some specific overriding's in their partial class.

I'd like somehow to implement the partial method using the base implementation.

Is there any nice approach to achieve something like this? Or even something similar?

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • I've never seen a partial method and maybe I don't understand it from the little amount of research I did, but it looks like you can't make a partial method in a non-partial class (abstract). Why not just make it virtual? – Brandon Oct 21 '15 at 16:28
  • @Brandon - LinqToSql creates these methods for me and has placeholders behind the scenes. It's easy to create virtual methods but I'm not the boss in this case :) – Amir Popovich Oct 21 '15 at 16:40
  • Oh it creates the `OnLoaded` methods? Which class implements the LtS? – Brandon Oct 21 '15 at 16:47
  • @Brandon - LinqToSql creates the basic Table class. It offers a couple of partial methods that actually act as events. A partial method is a method that can be implemented. If it's not implemented then during compilation it's calls are removed. If it is implemented then it's compiled the same way that other methods are compiled. Since LtS does all the work behind the scenes I can't really make these methods as simple override methods. – Amir Popovich Oct 21 '15 at 16:59
  • I don't known LtS but probably you can do something by using extension methods? Those are not implemented in the generated class but have only access to public members... – Markus Gilli Oct 21 '15 at 17:13

0 Answers0