OOP和DAL结构 [英] OOP and DAL structure

查看:111
本文介绍了OOP和DAL结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

典型的scinario.我有一个带有多个表的数据库.
使用VS ORM工具生成DataContext类.

使用存储库模式,我将为每个表创建一个存储库类.

但是问题是,我也想使用接口来简化测试,而又不使用dabatase.

我的问题是:
我是否还必须为每个表创建一个接口,或者我可以为所有表使用1个接口而以某种方式逃脱吗?请注意,每种方法都将使用单独的对象.即,产品更新方法将采用产品对象,而客户更新方法将带走客户对象."

提前谢谢.

公司表接口示例

Hi All,

Typical scinario. I have a Database with several tables.
Using VS ORM tool to generate DataContext classes.

Using the Repository pattern, I will create a repository class per table.

BUT the issue is, I want to use interfaces also for ease of testing without having a dabatase.

My question is:
"Do I have to creare an interface per table also, or can I somehow get away with using 1 interface for all tables? noting that each method will take seperate object. i.e a product update methind will take a product object, but customer update method will take customer objects."

Thanks in advance.

Company Table Interface example

internal interface ICompanies
    {
        void Create(Company company);
        void Delete(Company company);
        void Update(Company company);
        void Save();
        IQueryable<Company> Select();
        Company GetSingle(int companyId);
    }



公司位置界面示例



Company Locations Interface example

interface ICompanyLocations
    {
        void Create(CompanyLocation CompanyLocation);
        void Delete(CompanyLocation CompanyLocation);
        void Update(CompanyLocation CompanyLocation);
        IQueryable<CompanyLocation> Select();
        void Save();
        CompanyLocation GetSingle(CompanyLocationID);
    }

推荐答案


您可以对所有实体使用IRepository通用接口:
Hi,
You can use the IRepository generic interface for all the entities:
public interface IRepository<T>
                where T : class
{
  T GetById(int id);
  IEnumerable<T> GetAll();
  IEnumerable<T> Query(Expression<Func<T, bool>> filter);
  void Add(T entity);
  void Remove(T entity);
}


好像在这里有设计问题. CompanyLocation是与公司相关的实体,对吗?因此,单独获取CompanyLocation 没有任何意义.公司应包含CompanyLocation ,并且获得公司将自动获得位置.

因此,请以逻辑方式对接口进行建模,而不是为每个表创建一个接口.我建议您看一下nHibernate,而不是VS ORM工具.
Looks like you have a design issue here. CompanyLocation is an entity which is related to the company, right? So getting CompanyLocation individually doesn''t make sense. Company should contain CompanyLocation and getting a company will get locations automatically.

So model your interfaces in a logical way rather than one interface per table. I''d suggest you to take a look at nHibernate rather than the VS ORM tool.


这篇关于OOP和DAL结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆