如何检索库的域对象 [英] How to retrieve Domain Object from Repositories

查看:153
本文介绍了如何检索库的域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,了解库域对象关系。下面是一些信息,我知道的领域设计(他们也可能是错误的或不准确)。而这些想法,我不能找到一种方式来获得从信息库中的域对象。

I have a little problem understanding repository-domain object relation. Here is some information I know about domain design(they may also be wrong or not accurate). And with these in mind, I can't find a way to obtain a domain object from the repository.

在DDD域应该知道,需要只包含什么的企业和其他一切必须被清除出域。没关系。从任何业务也抽象的数据访问是一个很好的做法了。该应用程序并不需要知道我们存储的数据或我们如何存储数据。我们只要求存储库给我们一个域对象,它为我们提供了我们想要的对象或其他方式是否有效,我们也给存储库中的域对象,并将其发送到存储。

In DDD the domain should know and contain only whats needed for the business and everything else must be cleared out of the domain. That's fine. And also abstracting data access from any business is a good practice too. The application doesn't need to know where we store data or how we store data. We only ask the repository to give us a domain object and it gives us the object we want or the other way is valid too, we give the repository a domain object and it sends it to the storage.

声明公共setter方法​​域对象也是面向对象设计的一个非常不好的做法,因为我们不能够控制谁在访问什么,改变什么。因此,它是什么所需的对象外,只露出一个很好的做法。

Declaring public setters for domain objects is also a very bad approach in object oriented design since we won't be able to control who is accessing what and changing what. So it is a good practice to expose only whats needed for outside of the object.

因此​​,与这些在我的脑海,我不能想出一个办法来实现我的库。我可以用我的代码中任何ORM或纯SQL和检索数据。

So with these in my mind, I can't figure out a way to implement my repositories. I can use any ORM or pure sql in my code and retrieve data.

但我不能创建持久性对象的域对象;

But I can't create domain objects from persistence objects;


  1. 因为他们没有公开制定者,我不能创建和设置字段值。

  2. 声明包含所有领域的公共构造似乎并不正确。我可能有几个车型,填补了,这意味着我必须定义不同的参数集几个构造函数。

任何帮助将是赞赏...

Any help will be appreciated...

推荐答案

有选择有:

1。奥姆斯可以与私人领域的合作。

据我所知,奥姆斯(如的实体框架 NHibernate的)可以设置通过非性。-public制定者

As I know, ORMs (e.g. Entity Framework, NHibernate) can set properties via non-public setters.

有是证明了这一点对于实体框架的例子 - 的实体框架,私有构造及私人二传手的。

There is an example that proves it for Entity Framework - Entity Framework, Private Constructors and Private Setters.

如果您使用的NHibernate的制定者应公共/受保护的虚拟 / 保护内部虚拟私人支持字段都可以使用。你可以找到在NHibernate的 SO问题属性访问权限策略的详细信息。

If you use NHibernate your setters should be public/protected virtual/protected internal virtual or private backing field can be used. You can find more information in the Property Access strategies in NHibernate SO question.

2。 反射的都可以使用。

2. Reflection can be used.

它可以被用来获取访问私有字段/属性也。 它可以通过反射设置私有财产。

It can be used to get access to private fields/properties also. It is possible to set private property via reflection.

3。这不是一个不好的做法,有公共构造来构建你的实体。

声明包含所有领域的公共构造没有按T似乎是正确的。我可能有几个车型,填补了,这意味着我必须定义几个构造函数使用不同的参数集。

Declaring public constructors containing all of the fields doesn't seems right. I might have several models to fill in, this means I have to define several constructors with different sets of parameters.

您的域实体需要只有一个公共构造与他们的属性完整列表。这足以只有一个,尽管有几款车型,以填补在构造函数中。这是库的责任正确地调用构造函数和地图模型到它的参数。

Your Domain Entities need only one public constructor with full list of properties they have. It is enough to have only one constructor in spite of having several models to fill in. It is a responsibility of repository to invoke constructor and map model into its parameters correctly.

编辑:

4。 Automapper 的都可以使用。

4. Automapper can be used.

以下试验结果表明, AutoMapper 可以映射通过私人setter方法​​的属性。

The following test shows that AutoMapper can map properties via private setters.

[TestClass]
public class AutomapperTest
{
    [TestMethod]
    public void Test()
    {
        // arrange
        Mapper.CreateMap<AModel, A>();
        var model = new AModel { Value = 100 };

        //act
        var entity = Mapper.Map<A>(model);

        // assert
        entity.Value.Should().Be(100);
        entity.Value.Should().Be(model.Value);
    }
}

public class AModel
{
    public int Value { get; set; }
}

public class A
{
    public int Value { get; private set; }
} 

这篇关于如何检索库的域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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