开发者

LINQ2SQL DataLayer / Repository Suggestion

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 MotorRenewalDataRepo开发者_JS百科sitory()         
        {

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

            }

        }

    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜