views:

75

answers:

3

I am doing a CRUD operation on Database generated class(using LINQ2SQL) in my WPF application. All of my DB tables have IsDelete property exists. So I want to define an abstract/interface class to do the SoftDelete(). My question here is, how can I define my Generic class in such a way as to access T.IsDelete = true ? or in code I want to do something like below

public abstract class CRUDOperations <T> : where T is????
{
  .......
  protected virtual SoftDelete()
  {
     T.IsDeleted = true;
  }
}

Where T is DB generated Table classes by LINQ To SQL (DBML), which I cant impose an Interface or base class on top of it?. Or is there a technique in DB to have a base table which has the same Base/Derive concepts of C#

A: 

Aren't the Database generated classes derived from Entity?

Myles
this is what I can see MyTableClass: INotifyPropertyChanging, INotifyPropertyChanged
Jobi Joy
Sounds like EF; the question is LINQ-to-SQL
Marc Gravell
+3  A: 

1) Define ISoftDelete

public interface ISoftDelete
{
  bool IsDeleted { get; set; }
}

2) Create partial classes for all your entities

public partial class MyLinqEntity : ISoftDelete { }
public partial class MyOtherLinqEntity : ISoftDelete { }
public partial class MyFurtherLinqEntity : ISoftDelete { }

3) Add your interface to the generic type restriction

public abstract class CRUDOperations <T> : where T is ISoftDelete
Will
Thanks, Looks like a solution but does Step2 is a huge work for me on all my Table Classes right?
Jobi Joy
Yep. There are other ways of doing this in the designer, but I prefer this method as I'll have all these partials I can use to modify my entities. Also, if I have to ditch the DBML and start over again (happens more than I would like) I won't have to redo all my customization work.
Will
IIRC, you can't do this in the designer - you have to hand-edit the dbml.
Marc Gravell
+2  A: 

You can declare this on an interface:

public interface ICanDelete {
    bool IsDeleted {get;set;}
}

and let implicit interface implementation work its magic. Then the question becomes: "how to let it know that my entities implement this interface?". Two options:

  • add a partial class per entity (in a different file):

    partial class Customer : ICanDelete {}
    partial class Order : ICanDelete {}
    
  • edit the dbml to specify this interface as the base class:

    <Database EntityBase="Some.NameSpace.ICanDelete" ... >
    

    (this makes LINQ-to-SQL's code-generation add the : Some.NameSpace.ICanDelete for you)

Then just use this interface in the generic clause:

where T : ICanDelete
Marc Gravell
Perfet that worked for me, with a very minimal code change.
Jobi Joy