none
Generic DataContext Class, Generic Table as constraint. RRS feed

  • Question

  • This is what i have:
       public partial class AddressesDataContext<T> : DataContext

    {

          ...... (Constructors and what not)....

    }

      [global::System.Data.Linq.Mapping.TableAttribute(Name="tableName")]
        public partial class customers_branches_addresses_a : INotifyPropertyChanging, INotifyPropertyChanged

    { ... Code ...}

    [global::System.Data.Linq.Mapping.TableAttribute(Name="tableName")]
        public partial class employees_addresses_a : INotifyPropertyChanging, INotifyPropertyChanged

    { ... Code ... }

    What i want to do, is provide the wanted table, once i create the an instance of the AddressesDataContext class, so when i execute a LINQ query, all CRUD operations will take place in the proper db table.

    What i tried:

    1. Having a table class, and change the TableAttribute attribute, at run time.

    2. Multiple DBMLs, each containing the wanted table, (all the tables where named "addresses") then create a sequence of inherited classes(the "final" class would have ended up inheriting all the different DataContext classes). Then have a generic class, that  would accept the "final". That didn't work because i query wouldn't know which table is to be used, since they were all named "addresses".

    3. Generic DataContextClass. E.g

    public partial class AddressesDataContext<T> : System.Data.Linq.DataContext

     {

      public T addresses

        {

            get {

                     if (T == typeof(Table<customers_addresses) {

                             return this.GetTable<customers_addresses>();

                    }

                  etc.

                }

       }

    4. Generic class but a little different:

    public partial class AddressesDataContext<T , U> : System.Data.Linq.DataContext

    where T : class

    where U : Table<T>

    I know that i could have a different repository, for each table and what not, but my employeer suggested taking a really close look at this approach.

    Thank you.

    • Edited by SpirosMstz Monday, January 11, 2016 10:33 AM
    • Moved by CoolDadTx Monday, January 11, 2016 3:15 PM LINQ related
    Monday, January 11, 2016 10:17 AM

Answers

  • Hi SpirosMstz,

    >>What i want to do, is provide the wanted table, once i create the an instance of the AddressesDataContext class, so when i execute a LINQ query, all CRUD operations will take place in the proper db table.

    You could try your 4 project, and it easy to provider you the wanted table. The following code snippet for your reference.

    public class DemoContext<T, U> where T : DataContext, new()
        where U : class
        {
            protected T DataContext = new T(); 
            public Table<U> Address
            {
                get
                {
                    return DataContext.GetTable<U>();
                }
            }
    } 

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 12, 2016 3:30 AM
    Moderator