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 941533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:08:05+00:00 2026-05-15T22:08:05+00:00

I have implemented a repository pattern in my asp.net mvc web application… But i

  • 0

I have implemented a repository pattern in my asp.net mvc web application… But i want to know is this a good repository pattern or still can i improve it more…

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaxiMVC.BusinessObjects;

namespace TaxiMVC.Models
{
    public class ClientRepository
    {
        private TaxiDataContext taxidb = new TaxiDataContext();
        Client cli = new Client();

        //Get all Clients
        public IQueryable<ClientBO> FindAllClients(int userId)
        {
            var client = from c in taxidb.Clients
                         where c.CreatedBy == userId && c.IsDeleted == 0 
                         select new ClientBO()
                         {
                             ClientId = c.ClientId,
                             ClientName= c.ClientName,
                             ClientMobNo= Convert.ToString(c.ClientMobNo),
                             ClientAddress= c.ClientAddress
                         };
            return client;
        }

        //Get Client By Id
        public ClientBO FindClientById(int userId,int clientId)
        {
            return (from c in taxidb.Clients
                    where c.CreatedBy == userId && c.ClientId == clientId && c.IsDeleted == 0
                         select new ClientBO()
                         {
                             ClientId = c.ClientId,
                             ClientName= c.ClientName,
                             ClientMobNo= Convert.ToString(c.ClientMobNo),
                             ClientAddress= c.ClientAddress
                         }).FirstOrDefault();
        }

        //Insert a new client
        public bool ClientInsert(ClientBO clientBO)
        {
            cli.ClientName = clientBO.ClientName;
            cli.ClientMobNo = Convert.ToInt64(clientBO.ClientMobNo);
            cli.ClientAddress = clientBO.ClientAddress;
            cli.CreatedDate = clientBO.CreatedDate;
            cli.IsDeleted = clientBO.IsDeleted;
            cli.CreatedBy = clientBO.CreatedBy;

            if (!taxidb.Clients.Where(c => c.ClientMobNo == cli.ClientMobNo).Any())
            {
                taxidb.Clients.InsertOnSubmit(cli);
                taxidb.SubmitChanges();
                return true;
            }
            else
                return false;
        }

      //Client Update
        public ClientBO updateClient(ClientBO clientBO)
        {
            var table = taxidb.GetTable<Client>();
            var cli = table.SingleOrDefault(c => c.ClientId == clientBO.ClientId && c.CreatedBy==clientBO.CreatedBy);
            cli.ClientName = clientBO.ClientName;
            cli.ClientMobNo = Convert.ToInt64(clientBO.ClientMobNo);
            cli.ClientAddress = clientBO.ClientAddress;
            taxidb.SubmitChanges();
            return clientBO;
        }

        //Delete Clients
        public bool deleteClients(string Ids, int userId)
        {
            var idsToDelete = Ids.Split(',').Select(c => Convert.ToInt32(c));
            var clientsToDelete = taxidb.Clients.Where(c => idsToDelete.Contains(c.ClientId));
            foreach (var client in clientsToDelete)
            {
                client.IsDeleted = Convert.ToByte(1); 
            }
            taxidb.SubmitChanges();
            return true;
        }
     }
}

and my ClientBo.cs,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TaxiMVC.BusinessObjects
{
    public class ClientBO
    {
        public int ClientId { get; set; }
        public string ClientName { get; set; }
        public string ClientMobNo { get; set; }
        public string ClientAddress { get; set; }
        public DateTime CreatedDate { get; set; }
        public byte IsDeleted { get; set; }
        public int CreatedBy { get; set; }
    }
}

I Didn’t implement an IRepository here… Should i ve to implement it or should my repository can still be improved… Any suggestion….

  • 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-05-15T22:08:05+00:00Added an answer on May 15, 2026 at 10:08 pm

    Hmm, there is definitely a couple of things that I would do to improve this.

    First: I would define an interface for your Repository to implement. This allows you greater control over dependencies, and when coupled with a Inversion Of Control/Dependency Injection (IOC/DI) framework, this is hugely improved. IOC/DI frameworks include StructureMap or NInjet. Have a read of this from Scott Hanselman, it’s a pretty comprehensive list.

    Your interface may look like:

    public interface IClientRepository
    {
        IQueryable<ClientBO> FindAllClients(int userId);
        ClientBO FindClientById(int userId, int clientId);
        ClientInsert(ClientBO clientBO);
        ClientBO updateClient(ClientBO clientBO);
        bool deleteClients(string Ids, int userId);
    }
    

    Second: don’t do your Business Object (ClientBO) to persistent object (Client) conversion inside of your repository. This means that if you make any changes to your BO, then you’ll need to go through and change your entire repository.

    I notice you have a lot of left-right assignment code, eg.

    cli.ClientName = clientBO.ClientName;
    

    I would seriously investigate the use of AutoMapper. It makes this “monkey code” a hell of a lot easier.

    EDIT: Here is a blog post that describes how to use AutoMapper to remove the left-right assignment code.

    Third: Your naming structure is all over the shop. We have: FindAllClients(), ClientInsert(), updateClient() all in the one class. Very very poor naming. For your repository, try to model your methods on what will be happening on the DB side. Try Add or Insert, Delete, FindAll or GetAll, Find or GetAll, SaveChanges, method names.

    Don’t append/prepend the type to the method name, as your are in the ClientRepository, it’s implied that you’ll be adding or getting Client‘s.

    Fourth: Your mixing your LINQ syntax. In some places your using the declarative query syntax and other places your using the method syntax. Pick 1 and use it everywhere.

    Fifth: This line worries me:

     if (!taxidb.Clients.Where(c => c.ClientMobNo == cli.ClientMobNo).Any())
    

    This looks suspiciously like business logic to me. Not something that should be in the repository. Either declare the column to be UNIQUE in the DB, or move that logic into another validation layer.

    These were the main things that jumped out at me. A couple of these are personal preference, but I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make an implementation with repository pattern with ASP.NET MVC 2 and
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I have an ASP.NET MVC 3 (Razor) Web Application, with a particular page which
Or I don't understand this at all. I have started my ASP.NET MVC application
After going through some tutorials on asp.net mvc the repository pattern came up and
I'm trying to get started with the repository pattern and ASP.NET MVC, and I
I am a newbie to ASP.net & MVC 2, and have understood the basic
Here's the scenario: ASP.NET MVC2 Web Application Entity Framework 4 (Pure POCO's, Custom Data
I am building an asp.net MVC2 web app using StructureMap. I have created a
I have implemented what I thought was a pretty decent representation of MVC in

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.