简体   繁体   中英

UOW + Repository for classic ado.net with MVC

I have read two questions from stackoverflow.com

  1. Is Classic ADO.NET still used?
  2. Using ASP.Net MVC with Classic ADO.Net

I really appreciate both of them.
I like Repository Design Pattern so much that I would like to use it at my project which use ASP.net MVC and Classic Ado.net.

Above question No. 2 have explained about how could I use repository pattern and classic ado.net technologies.

One thing I would like to know is that Is this possible to use Repository pattern which combined with Unit of work , when it comes to classic ado.net technologies.

Could anyone please give me any reference links about that?

If there is references only for Repository + Classic Ado.net, then let me know that reference as well.

It is indeed possible to combine UoW, Repository and 'classic' ADO.NET.

Your UoW will need to get to the relevant repository based on the entity type, though. You could have your repositories implement a generic interface. For instance:

public interface IRepository<T>
{
    void Add(T instance);
    T Get(Guid id);
    void Remove(Guid id);
    void Remove(T instance);
}

Since each UoW is a separate instance you will want to use a factory. This factory will then either inject the repositories into the UoW or give it some IRepositoryProvider instance that can return the relevant repository based on type. So conceptually:

public interface IRepositoryProvider
{
    IRepository<T> Get<T>();
}

These are very broad strokes so I hope that makes sense.

IMO if you want to use Repository and UoW there is no reason to use "classic" ADO.Net. The various ORM frameworks implement the Repository+UoW patterns for you, you just plug them into your applications (assuming your app is designed around these patterns).

I'd only use classic ADO.Net when I didn't want to use Repository/UoW patterns (eg Transaction Script pattern).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM