tags:

views:

38

answers:

1

My current respository is as follows , please suggest , i am currently using LINQ2SQL Data context per insert/delele/update

namespace Lib.Repository
{

    public class MotorRenewalDataRepository
    {
        public MotorRenewalDataRepository()         
        {

        }
        public MotorRenewalData GetByID(long id)
        {
            using(var _context=DatabaseFactory.Create(false))
            {
                return _context.MotorRenewalDatas.Where(p => p.MotorRenewalDataID == id).FirstOrDefault();
            }
        }
        public MotorRenewalData Insert(MotorRenewalData entity)
        {
            using (var _context = DatabaseFactory.Create(false))
            {
                _context.MotorRenewalDatas.InsertOnSubmit(entity);
                _context.SubmitChanges();
                return entity;
            }
        }
        public void Update(MotorRenewalData entity)
        {
            using (var _context = DatabaseFactory.Create(true))
            {
                var dbEntity = _context.MotorRenewalDatas.Where(p => p.MotorRenewalDataID == entity.MotorRenewalDataID)
                            .FirstOrDefault();                
                Common.CopyObject<MotorRenewalData>(entity, dbEntity);
                _context.SubmitChanges();

            }

        }

    }
}
A: 

If I understand your question correctly, you are looking for suggestions on how to properly implement the repository pattern. Here is a good practice of using the repository pattern. First you will want to create an interface for your repository. This is where you define what a repository can do.

public interface IRepository<T>
{
    void Add(T entity);
    void Delete(T entity);
    void Save();
    IQueryable<T> FindAll();
}

Next you can create the individual repositories. The first thing you want to do here is to create an interface for anything outside of a normal repository that you might be doing.

public interface IMotorRenewalRepository : IRepository<MotorRenewal>
{
    MotorRenewal FindMotorRenewalById(int id);
}

And that interface will implement the IRepository of MotorRenewal so that you get everything from the IRepository and everything you define in IMotorRenewalRepository. The interface is most commonly used when you what to use some sort of dependency injection when writing fake objects and unit tests for your repository.

Now write your MotorRenewalRepository and implement the IMotorRenewalRepository.

public class MotorRenewalRepository : IMotorRenewalRepository
{
    MyDataContext _dataContext = new MyDataContext();

    public void Add(MotorRenewal motorRenewal)
    {
        _dataContext.MotorRenewals.InsertOnSubmit(motorRenewal);
    }

    public void Delete(MotorRenewal motorRenewal)
    {
        _dataContext.MotorRenewals.DeleteOnSubmit(motorRenewal);
    }

    public void Save()
    {
        _dataContext.SubmitChanges();
    }

    public IQueryable<MotorRenewal> FindAll()
    {
        return _dataContext.MotorRenewals.AsQueryable();
    }

    public User FindMotorRenewalById(int id)
    {
        return _dataContext.MotorRenewals.Where(p => p.MotorRenewalDataID == id).SingleOrDefault();
    }
}

This implementation is a lot easier to understand. Notice you do not need an update. An update is really just you pulling a MotorRenewal object out of the repository, editing it, and calling .Save().

You can use a class level variable for your data context rather than creating a new one each time you call a method on your repository. MyDataContext should come from the model you created when dragging in your LinqToSql classes from your data connection.

Steve Hook
@MindlessProgrammer With respect to swhook's suggestion about reusing the DataContext, see Rick Strahl's post on DataContext lifetime management: http://www.west-wind.com/weblog/posts/246222.aspx . I recognize that we are assuming that you're creating a new `DataContext`, but may in fact be returning a cached instance. If that is the case, I'd only suggest renaming the `Create()` method to something like `Get()` to make the semantics more accurate.
Jay
MindlessProgrammer