对于数据访问层设计模式 [英] Design pattern for Data Access Layer
问题描述
您可能会觉得这是作为家庭作业。我感到很抱歉,我搜查,但未能找到这个合适的回答,
You might be feel this is as homework. i'm sorry about that and i searched but couldn't find proper answer for this,
我的问题如下:
我有几个类别和他们每个人都有了节约的方法。所以我想为数据库处理创建单独的类。
i have several classes and each of them have a method for save. so i thought to create separate class for database handling.
namespace HospitalMgt.Data
{
public static class DBConnection
{
public static string constr = "Data Source=ABD;Initial Catalog=HospitalMgt;User Id=sa;Password=123";
public static SqlConnection con;
// public static SqlCommand com;
public static SqlConnection OpenConnection()
{
con= new SqlConnection(constr);
con.Open();
return con;
}
}
}
但我觉得它不适合于实现所有教学班,DBConnection的类。
but i felt its not suited to implement all the classes with DBConnection Class.
我的问题:
- 在适合什么样的设计模式来解决这个问题? (如果有的话)
- 是好把DBConnection的是类? (我是否需要创建为接口)
我发现几篇文章DA层工厂方法,但就我所知没有适合我的情况了。
i found few articles DA layer with Factory method, but as my knowledge its not suited for my situation.
感谢您
推荐答案
通常情况下,如果我不能使用任何现有的框架,我同时使用存储库和活动模式。
Normally, if I can't use any existing framework, I use both the Repository and Active patterns.
为了简单起见,你可以只使用Repository模式。我通常把它定义是这样的:
For simplicity, you could use only the Repository pattern. I normally define it like this:
// Define a generic repository interface
public interface IRepository<Key, E> where E:IEntity<Key>>{
void Add(E entity);
void AddRange(IEnumerable<E> entities);
IEntity<Key> Get(Key key);
IEnumerable<E> GetRange(IEnumerable<Key> keys);
IEnumerable<E> GetAll();
// ..., Update, Delete methods
}
// Create an abstract class that will encapsulate the generic code
public abstract class Repository<K, E> where E:IEntity<K>>:IRepository<K, E>{
protected Repository(/*parameter you may need to implement the generic methods, like a ConnectionFactory, table name, entity type for casts, etc */){}
public override void Insert(IEntity<Key> entity){
// do the insert, treat exceptions accordingly and encapsulate them in your own and more concise Exceptions, etc
}
// ...
}
// Create the entities classes, one for each table, that will represent a row of that table
public class Car: IEntity<String>{/* Properties */}
// Create a specific repository for each table
// If the table have a composed key, just create a class representing it
public CarRepository: Repository<String, Car>{
public CarRepository(){/* pass the base parameters */}
// offer here your specific operations to this table entity
public IEnumerable<Car> GetByOwner(PersonKey ownerKey){
// do stuff
}
}
您现在有足够的工具来操作数据库,但如果你愿意,你可以使用Active模式。 一个简单的例子:
You now have enough tools to manipulate the database, but if you want, you can use the Active pattern. A simple example:
public class Person:IEntity<PersonKey>{
public PersonKey Key{get;}
public IEnumerable<Car> OwnedCars{
get{
CarRepository rep = DBSingletons.Cars;
return rep.GetByOwner(this.Key);
}
set{
// do stuff
}
}
}
显然,做你自己的实现时,你必须考虑到线程安全取得良好使用的交易,特别是跨diferent实体存储库。
Obviously, when doing your own implementations, you must take into account thread safety making good using of transactions, specially across diferent entity repositories.
// simple example
ITransaction t = TransactionFactory.GetNewTransaction();
t.begin();
try{
// create person entity
personRepository.Add(person, t);
// create cars assigned to person
carRepository.AddRange(cars, t);
t.commit();
}catch(Exception){
t.rollback();
}
只要确保你真的想创建自己的DAL,因为它可以结束beeing extremelly复杂,特别是努力开发最通用的解决方案。
Just be sure that you really want to create your own DAL since it can end beeing extremelly complex, specially trying to develop the most generic solution.
这篇关于对于数据访问层设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!