需要Repository模式和依赖注入的更深入的例子 [英] Need a more in depth example of repository pattern and dependency injection

查看:386
本文介绍了需要Repository模式和依赖注入的更深入的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过asp.net一些教程MVC存储库模式上来和例子是有一个表,该表晚餐后去。基本上设立是创建一个接口,然后一个具体的类,它实现了界面和程序关中控制器类的接口。该接口具有典型的CRUD方法。并为每种类型的接口,如果您要使用此模式来创建。例如有一个方法的GetList用晚餐类型。如果您有需要在执行CRUD功能,10种不同的类型?这是否意味着有10具体类10接口,只是为了能够切换DB技术的道路的好处?
我试图找出如何将此模式应用到一个标准的3 - 三层架构(对象层,业务逻辑层,数据访问层)

After going through some tutorials on asp.net mvc the repository pattern came up and the example was with one table, the dinners table. Basically the set up was to create an interface and then a concrete class which implemented the interface and program off the interface in the controller class. The Interface has your typical crud methods. Does an interface for each type have to be created if you are going to use this pattern. For instance there was a GetList method with a Dinner type. What if you have 10 different types that you need to perform crud functionality on? does this mean 10 interfaces with 10 concrete classes just for the benefit of being able to switch the db technology down the road? I am trying to figure out how to apply this pattern to a standard 3 - tier architecture (Object Layer, Business Logic Layer, Data Access Layer).

感谢。

推荐答案

这是我的方式通常做在我的实现。

This is the way i usually do it in my implementations.

有一个通用接口,IEntityRepository它定义了基本的CRUD结构。在我实现我定义了以下成员:

A generic interface, IEntityRepository which defines your basic CRUD structure. In my implementations i define the following members:


  1. 插入

  2. 更新

  3. 删除

  4. 获取

  5. GetPaged

  6. GETALL

  7. 找到(这是使用predicate生成器生成的where子句)

我创建另一个接口IMyentityRepository它继承IEntityRepository。这让我添加任何特定实体成员,仍然能够当我需要使用DI。然后我创建密封类MyentityRepository它继承IMyentityRepository并实现所有成员。

I create another interface IMyentityRepository which inherits IEntityRepository. This allows me to add any entity-specific members and still be able to use DI when i need to. I then create my sealed class MyentityRepository which inherits IMyentityRepository and implements all members.

当您使用依赖注入,您可以注册您的具体类型MyentityRepository接口(IMyentityRepository)。

When you use Dependency Injection you can register your interface (IMyentityRepository) for concrete type of MyentityRepository.

在我而言,我实际上并没有完成。我对仓库之上创建一个服务层封装,并在更广泛的方式揭露它。例如,假设您想为您的用户的帐户可能涉及比简单的创建一个数据库记录更多的工作。在你的服务,你有一个叫做CREATEUSER()成员可以调用多个库成员的执行。
在同是作为我的仓库层我的业务层构建。我有IEntityService共同CRUD成员,IMyentityService为实现特定实体成员和MyentityService。 MyentityService类将需要IMyentityService的一个实例(您可以使用您选择的IoC框架注入的话)你的服务层也可以做验证和任何业务逻辑。我做验证的控制器。那么,在技术上,我调用它在我的控制器和得到的结果,我可以再写入的ModelState。

In my case, i wasn't actually done. I created a service layer on top of the repository to encapsulate it and expose it in a more general way. For instance, say you want to create an account for your user which may involve more work than simply creating a database record. In your service, you'd have a member called CreateUser() which may call multiple repository members in its implementation. My service layer is built in the same was as my repository layer. I have IEntityService for common CRUD members, IMyentityService for entity-specific members and MyentityService for implementation. MyentityService class would require an instance of IMyentityService (You can inject it with your IoC framework of choice) Your service layer may also do validation and any business logic. I do validation in controllers. Well, technically, i invoke it my controllers and get the result that i can then write to ModelState.

希望这有助于一点。

这篇关于需要Repository模式和依赖注入的更深入的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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