C# 为什么使用存储库作为属性?

发布于 2024-10-19 02:25:48 字数 1083 浏览 2 评论 0原文

我正在阅读存储库的使用情况。有时我看到存储库是实体的属性。我想知道优点和缺点是什么。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-10-26 02:25:48

某些实体实现需要访问创建它们的上下文,例如实现延迟加载。请考虑以下事项:

class MyEntity
{
    public virtual IEnumerable<string> Tags { get; private set; }
}

interface IMyEntityRepository
{
    ...
}

您可以从 MyEntity 实现返回 MyEntity 的子类,这需要对创建它的存储库的引用。

internal class MyLazyEntity : MyEntity
{
    public MyLazyEntity(MyLazyEntityRepository repository)
    {
        this.Repository = repository;
    }

    public override IEnumerable<string> Tags
    {
        get
        {
            return this.Repository.LoadTagsForEntityFromXml(this.Id);
        }
    }
}

class MyLazyEntityRepository : IMyEntityRepository { }

我想说,在从实体派生的类中保留对源存储库的引用很好,另一方面,基实体类不应该了解存储库。

Some entity implementations require access to the context in which they are created, to implement lazy loading for example. Consider the following:

class MyEntity
{
    public virtual IEnumerable<string> Tags { get; private set; }
}

interface IMyEntityRepository
{
    ...
}

You can return sub-classes of MyEntity from the an MyEntity implementation, that require a reference to the repository that created it.

internal class MyLazyEntity : MyEntity
{
    public MyLazyEntity(MyLazyEntityRepository repository)
    {
        this.Repository = repository;
    }

    public override IEnumerable<string> Tags
    {
        get
        {
            return this.Repository.LoadTagsForEntityFromXml(this.Id);
        }
    }
}

class MyLazyEntityRepository : IMyEntityRepository { }

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.

埋情葬爱 2024-10-26 02:25:48

我不会使用这种方法。更新自身不是 Foo 的责任,而是存储库的责任。如果您正在应用持续的无知模式。

这段代码有点味道:

   public void Update()
   {
     Repository.Update(this);
   }

它除了将调用转发到存储库之外什么也不做。

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:

   public void Update()
   {
     Repository.Update(this);
   }

It doesn't do anything but forward the call to repository.

椵侞 2024-10-26 02:25:48

您可以将其与 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文