多个数据提供者/ORM 上的存储库模式?
假设我有以下数据模型:
public class Account
{
public string Username { get; set; }
public string Password { get; set; }
}
public class Configuration
{
public string Key { get; set; }
public string Value { get; set; }
}
目前,每个模型都有自己的数据访问存储库,并使用实体框架作为其工作单元/DbContext。我计划将配置部分从实体框架中取出,并使用 Redis 或 Memcached 作为其数据访问。我什至可能将 EF 切换到 NHibernate 或根本不使用 ORM,并且可能将数据库切换到 MongoDB 或 CouchDB。
这样做的好方法是什么?对我的业务逻辑中所有底层的东西一无所知?使用什么样的模式?针对这样的变化进行设计是否可能,或者只是一件坏事?
谢谢 :)
Let just say I have the following data models:
public class Account
{
public string Username { get; set; }
public string Password { get; set; }
}
public class Configuration
{
public string Key { get; set; }
public string Value { get; set; }
}
For now, each of them has their own repository for data access, and uses entity framework as its unit of work/DbContext. I'm planning to pull out the Configuration part out of the entity frame and use Redis or Memcached as its data access. I might even to switch the EF to NHibernate or no ORM at all, and I might switch the database to MongoDB or CouchDB.
What is the good way to do this? To be ignorant of all those lower layer stuff in my business logic? What kind of pattern to use? Is it possible or is it just bad things to design for changes like this?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如上一篇文章所述,您应该走“界面之路”。
我个人不会直接为每个 orm 实现存储库,但我使用了一些变化。
使用你的例子......
然后你创建你的存储库
我对这个解决方案很满意,因为我最终只有一个存储库,90%的时间使用linq来查询,所以我不必为每个工作单元编写sql每次我必须使用分页编写“GetAllProducts”时,我不必为每个工作单元编写相同的代码(和测试),而只需为我的存储库编写相同的代码(和测试)。这显然是一个简单的例子,所以我希望你能明白。
您可以创建一个 RepositoryBase 来实现使用 linq 的 Find() 或 Query() 方法。
然后用你的 Castle Windsor 或 ninject 或任何你可以注入你喜欢的工作单元。希望有帮助。
更新:
我的 UnitofWorkBase 实现 nhibernate 的示例是类似的:
}
As stated in the previous post, you should go the "way of the Interface".
I personally do not implement directly the repository for each orm but I use a little variation.
Using your example...
then you create your repository
I am happy with this solution because I end up with only one repository that 90% of the time use linq to query so I don't have to write the sql for each unit of work and every time I have to write a "GetAllProducts" with paging I do not have to write the same code (and tests) for every unit of work, but only for my repository. This is a simple example obviously so I hope you get the idea.
You can make a RepositoryBase that implement a method Find() or Query() which use linq.
Then with your Castle Windsor or ninject or whatever you can inject the unit of work you prefer. Hope it helps.
Update:
a sample of my UnitofWorkBase that implement nhibernate is something similar:
}
使用接口。
然后你在每个数据访问对象(ef、mongdb 等)上实现这个接口。
在您的业务逻辑代码中,您仅使用接口而不是实际对象。
我使用工厂模式来创建数据访问对象,但您可以使用每个 IoC 模式。
Use an interface.
then you implement this interface on every data access object (ef,mongdb, etc. etc).
In your business logic code you use just the interface and not the acual object.
i use the factory pattern to create the data access objects, but you can use every IoC pattern.