Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneralizing LINQ Model
    primarykey
    data
    text
    <p>I have a problem with model design in MVC pattern and I am stuck with the statically type of C#. </p> <p>What I want to do is just to make a group of classes that do all database insert, update, delete operations. This group consists of a subgroup of classes that is mapped from each table in database and a subgroup of table-model classes that access the table classes. </p> <p>I am using LINQ mapping to create the table class:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Linq.Mapping; using Iris.Libraries; namespace Iris.Models.Tables { [Table] public class Users { [Column(IsPrimaryKey = true)] public string User_Id; [Column] public string Name; [Column] public string Password; [Column] public int Userlevel_Id; public Users() { } } } </code></pre> <p>The table then accessed by the model class:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Linq; using Iris.Models.Tables; using Iris.Models.DataContexts; namespace Iris.Models { public class UserModel { private UserDataContext dataContext; private Table&lt;Users&gt; users; public UserModel() { this.dataContext = new UserDataContext(Config.CONNECTION_STRING); this.users = this.dataContext.GetTable&lt;Users&gt;(); } public List&lt;Users&gt; Select() { var data = from user in this.users select user; return data.ToList&lt;Users&gt;(); } public Users Select(object id) { var data = from user in this.users where user.User_Id.ToString() == id.ToString() select user; return data.ToList&lt;Users&gt;()[0]; } public void Insert (Users user) { this.dataContext.Users.InsertOnSubmit(user); this.dataContext.SubmitChanges(); } public void Update(Users user) { var queryableData = from row in this.dataContext.Users where row.User_Id == user.User_Id select row; var editedData = queryableData.Single&lt;Users&gt;(); editedData.User_Id = user.User_Id; editedData.Name = user.Name; editedData.Password = user.Password; editedData.Userlevel_Id = user.Userlevel_Id; this.dataContext.SubmitChanges(); } public void Delete(Users user) { var queryableData = from row in this.dataContext.Users where row.User_Id == user.User_Id select row; var deletedData = queryableData.Single&lt;Users&gt;(); this.dataContext.Users.DeleteOnSubmit(deletedData); this.dataContext.SubmitChanges(); } } } </code></pre> <p>Above pair of codes works fine without any problem but I want to avoid writing the 'almost same' code for model class again and again since there are a lot of table in the database. To achieve that purpose, I try to make a generalized class of model and every table-model extended from it.</p> <pre><code>public class Users : Model {} </code></pre> <p>The problem comes from </p> <pre><code>private Table&lt;Users&gt; users; </code></pre> <p>Which is the class in Table&lt;> always different for each table. I've been searching around for days and didn't find any answer to solve this problem.</p> <p>Is it really impossible to generalize the table-model like mine above? Or there are any other way to avoid writing same code repeatedly? Anyone, please help me.. :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload