English 中文(简体)
MVC, ORM and data access patterns
原标题:MVC, ORM, and data access patterns

I think I ve hit that "paralysis by analysis" state. I have an MVC app, using EF as an ORM. So I m trying to decide on the best data access pattern, and so far I m thinking putting all data access logic into controllers is the way to go.. but it kinda doesn t sound right. Another option is creating an external repository, handling data interactions. Here s my pros/cons:

如果把数据输入控制器,我最后将采用这样的编码:

using (DbContext db = new DbContext())
{
    User user = db.Users.Where(x=>x.Name == "Bob").Single();
    user.Address.Street = "some st";
    db.SaveChanges();
}

So with this, I get full benefits of lazy loading, I close connection right after I m done, I m flexible on where clause - all the niceties. The con - I m mixing a bunch of stuff in a single method - data checking, data access, UI interactions.

With Repository, I m externalizing data access, and in theory can just replace repos if I decide to use ado.net or go with different database. But, I don t see a good clean way to realize lazy loading, and how to control DbContext/connection life time. Say, I have IRepository interface with CRUD methods, how would I load a List of addresses that belong to a given user ? Making methods like GetAddressListByUserId looks ugly, wrong, and will make me to create a bunch of methods that are just as ugly, and make little sense when using ORM.

I m sure this problem been solved like million times, and hope there s a solution somewhere..


还有一个关于储存模式的问题——你如何处理属于财产的物体? E.g. 用户有一份地址清单,你如何检索该清单? 建立地址存放处? 在投管处,地址标的不一定要提到用户,也不必提及有背书的Id领域,而是必须提到所有。 更多的代码、更多的暴露特性。

最佳回答

你们选择的方法在很大程度上取决于你将要从事的项目类型。 对于需要采用<代码>Rapid Application Development的小型项目(RAD),在监控人员中直接使用你的电子格式,但项目增长越多,就越大,而且你会开始遇到越来越多的问题。 如果你想要妥善设计和保持,则有几种不同的做法,但总的来说,你可以坚持如下:

保持你的控制员和看法的清晰度。 主计长只应控制应用程序流,而不控制数据存取,甚至控制业务逻辑。 意见只应当用于陈述——给它一个观点,并将作为Html(没有商业逻辑或计算)。 www.un.org/Depts/DGACM/index_spanish.htm 每一种观点都是这样做的相当干净的方法。

典型的控制者行动希望:

public ActionResult UpdateCompany(CompanyViewModel model)
{
    if (ModelState.IsValid)
    {
        Company company = SomeCompanyViewModelHelper.
                          MapCompanyViewModelToDomainObject(model);
        companyService.UpdateCompany(company);
        return RedirectToRoute(/* Wherever you go after company is updated */);
    }
    // Return the same view with highlighted errors
    return View(model);
}

由于上述原因,可以总结贵国的数据获取(可测试性、便于转换数据提供方或投管处或不论何等)。 <代码> 交存<>/代码>模式是一个很好的选择,但在此,你们也有一些实施选择。 关于一般/非基因储存库的讨论一直很多,无论是否应当归还<代码>。 表格 但最终需要你们选择。

Btw,为什么你想要装 la? 作为一项规则,你确切知道你需要什么数据才能得出具体的看法,因此,你为什么会选择以推迟的方式来收集数据,从而增加数据库电话,而不是急切地把你所需要的一切放在一起? 在个人方面,我认为,有多个<条码”的征兆。 2. 选择与儿童有关系或没有子女的 objects。 E.g.

public class CompanyRepository
{
    Get(int Id);
    Get(string name);
    GetWithEmployees(int id);
    ...
}

这似乎是一种微乎其微的超高技能,你可以选择一种不同的方法,但只要你遵循一种模式,就更加容易维持该守则。

问题回答

我个人这样做:

我拥有一个抽象的域层,它不仅拥有CRUD的方法,而且还有专门方法,例如用户Manager.Authenticate()等。 它内部使用数据存取逻辑,或数据存取层抽象(取决于我需要的抽象程度)。

至少具有抽象的依赖性总是更好。 以下是一些建议:

  • you can replace one implementation with another at a later time.
  • you can unit test your controller when needed.

至于控制人员本身,它有2个建筑商:1个拥有一个抽象的域网(例如域网),另一个(避风)建筑商选择了违约执行。 这样,你的控制员在网络应用时间(寻找空房)和单位测试(注入模拟域层)期间生活得较好。

Also, to be able to easily switch to another domain at a later time, be sure to inject the domain creator, instead of domain itself. This way, localizing the domain layer construction to the domain creator, you can switch to another implementation at any time, by just reconstructing the domain creator (by creator I mean some kind of factory).

我希望这一帮助。

Addition:

  • I would not recommend having CRUD methods in domain layer, because this will become a nightmare whenever you rich the unit-testing phase, or even more, when you need to change the implementation to the new one at a later time.

这真的是在你想要你的法典的地方。 如果你需要一个物体的数据存取,你可以把它放在一个保存物体后面,或放在控制器身上:你将用一系列的GetByXXX电话或相当的密码来抓。 无论怎样,你都能够控制连接的寿命。 因此,现在你们需要问一下:我为什么想要我的人活下来?

在个人方面,我想把它从控制者手中夺走。 因此,我指的是把它转移到另一个层面。 或许可以采用一种可供你使用的一系列GetByXXX电话的模式。 他们ure。 错误? 否则我会说。 至少在相同的逻辑层中,它们都包含在一起,而不是分散在控制器中,而控制器与验证代码混在一起。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签