C# 为什么使用存储库作为属性?
我正在阅读存储库的使用情况。有时我看到存储库是实体的属性。我想知道优点和缺点是什么。
public interface IRepository<T>
{
T GetById(int id);
void Update(T);
}
public class FooRepository : IRepository<Foo>
{
public Foo GetById(int i)
{ /* code ..*/ }
void Update(Foo)
{ /*code..*/ }
}
public class Foo
{
public IRepository Repository {get;set;}
public void Update()
{
Repository.Update(this);
}
}
为什么使用这个方法?将存储库和实体对象分开使用不是更有意义吗?那么实体对象不知道任何存储库?
编辑:
但是如果您有一个主对象和不同的子对象怎么办:
public class MainObject
{
public int Id {get;set;}
public List<ISubject> SubObjects {get;}
}
public interface ISubObject
{
}
public class SubObjectA : ISubObject
{
public string SomeProperty {get;set;}
public int Id {get;set;}
public int MainObjectId {get;set;}
}
public class SubObjectB : ISubObject
{
public string AnotherProperty{get;set;}
public int Id {get;set;}
public int MainObjectId {get;set;}
}
所以 SubObjectA 和 SubObjectB 是不同的类型,但实现 ISubject 接口。主对象具有这些子对象的列表。每个子对象都有它自己的存储库。您将如何加载子对象?
I am reading up upon the Repository usage. Sometimes I see that a repository is a property of an entity. I was wondering what the pro's and cons are.
public interface IRepository<T>
{
T GetById(int id);
void Update(T);
}
public class FooRepository : IRepository<Foo>
{
public Foo GetById(int i)
{ /* code ..*/ }
void Update(Foo)
{ /*code..*/ }
}
public class Foo
{
public IRepository Repository {get;set;}
public void Update()
{
Repository.Update(this);
}
}
Why use this apporach? Doesn't it makes more sense to use the repository and the entity object seperated? So that the entity object doesn't know about any repositories?
EDIT:
But what if you have one main object and different sub-objects:
public class MainObject
{
public int Id {get;set;}
public List<ISubject> SubObjects {get;}
}
public interface ISubObject
{
}
public class SubObjectA : ISubObject
{
public string SomeProperty {get;set;}
public int Id {get;set;}
public int MainObjectId {get;set;}
}
public class SubObjectB : ISubObject
{
public string AnotherProperty{get;set;}
public int Id {get;set;}
public int MainObjectId {get;set;}
}
So SubObjectA and SubObjectB are different types but implement the ISubject interface. The main object has a list of these subobjects. Each sub-object has it's own repository. How would you load the sub-objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
某些实体实现需要访问创建它们的上下文,例如实现延迟加载。请考虑以下事项:
您可以从 MyEntity 实现返回 MyEntity 的子类,这需要对创建它的存储库的引用。
我想说,在从实体派生的类中保留对源存储库的引用很好,另一方面,基实体类不应该了解存储库。
Some entity implementations require access to the context in which they are created, to implement lazy loading for example. Consider the following:
You can return sub-classes of MyEntity from the an MyEntity implementation, that require a reference to the repository that created it.
I would say, that keeping a reference to the source repository in a class derived from an entity is fine, on the other hand, the base entity class should not have knowledge about the repository.
我不会使用这种方法。更新自身不是 Foo 的责任,而是存储库的责任。如果您正在应用持续的无知模式。
这段代码有点味道:
它除了将调用转发到存储库之外什么也不做。
I would not use this method. Its not Foo's responsibility to update itself its the repository's responsibility. If you are applying persistent ignorance pattern.
This code is a smell:
It doesn't do anything but forward the call to repository.
您可以将其与 Spring http://www.springframework.net/
或其他依赖注入工具 一起使用动态加载对象。只要它实现了接口,它就以相同的方式工作。检查面向方面的编程。
看看下面的文章
DDD 存储库的优点和缺点
You can use that along with Spring http://www.springframework.net/
or another dependency injection util to load objects dynamically. As long as it implements the interface it works in the same way. Checkout Aspect orientated programming.
Have a look at the following post
Pros and cons of DDD Repositories