Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:33:55+00:00 2026-06-01T21:33:55+00:00

I have a problem with model design in MVC pattern and I am stuck

  • 0

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.. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:33:57+00:00Added an answer on June 1, 2026 at 9:33 pm

    Use the following generic method:

    System.Data.Linq.Table<T> GetTable
    {
            get { return _db.GetTable<T>(); }
    }
    

    You can use Repository Pattern, first create a generic interface:

    public interface IRepository<T> where T : class
    {
        void Add(T entity);
        void Delete(T entity);
        void Update(T entity);
        IEnumerable<T> GetAll(Func<T,bool> exp);
        ....
    }
    

    Then your generic repository model should look like the following:

    public class Repository<T> : IRepository<T>
        where T : class, IEntity
    {
        DataContext _db;
        public Repository()
        {
            _db = new DataContext("Database Connectionstring");
            _db.DeferredLoadingEnabled = false;
        }
        System.Data.Linq.Table<T> GetTable
        {
            get { return _db.GetTable<T>(); }
        }
        public IEnumerable<T> GetAll(Func<T, bool> exp)
        {
            return GetTable.Where<T>(exp);
        }
        ...
        .
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I have a design pattern problem that I was hoping someone may be
It is design problem. Let's assume that we have this kind of model in
i have very simple problem. I need to create model, that represent element of
I have read a lot about MVC design pattern, but some of the things
Suppose you have an application that utilizes the domain-model pattern, DDD and lots of
I have a general OO design question that stems from a Hibernate Model. Example
I have problem with keeping my model (represented as List) in sync with a
I have a problem adding ADO.Net Entity Data Model to my existing project, or
I have a problem when I try to persist my model. An exception is
I have a problem with calling a property() inside a dynamic model in Django.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.