Monday, April 16, 2012

Generalizing LINQ Model

I have a problem with model design in MVC pattern and I am stuck with the statically type of C#.



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.



I am using LINQ mapping to create the table class:



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()
{
}
}
}


The table then accessed by the model class:



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<Users> users;

public UserModel()
{
this.dataContext = new UserDataContext(Config.CONNECTION_STRING);

this.users = this.dataContext.GetTable<Users>();
}

public List<Users> Select()
{
var data = from user in this.users select user;

return data.ToList<Users>();
}

public Users Select(object id)
{
var data = from user in this.users where user.User_Id.ToString() == id.ToString() select user;

return data.ToList<Users>()[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<Users>();

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<Users>();

this.dataContext.Users.DeleteOnSubmit(deletedData);

this.dataContext.SubmitChanges();
}
}
}


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.



public class Users : Model {}


The problem comes from



private Table<Users> users;


Which is the class in Table<> always different for each table. I've been searching around for days and didn't find any answer to solve this problem.



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.. :(





No comments:

Post a Comment