.NET的ORM,不可变的值对象,结构,默认构造,和只读属性属性、对象、结构、NET

2023-09-03 12:46:57 作者:单身老帅哥也有风度的一面

我刚开始接触.NET的ORM,到了那里我还没有实体框架和NHibernate的决定点。但在这两种情况下,我运行到一个问题,即他们似乎想让我妥协以各种方式我的域模型的完整性,特别是在C#对象设计的细微之处。这是关于这个问题的几个问题之一。

I am just getting started with .NET ORMs, to the point where I haven't even decided between Entity Framework and NHibernate. But in both cases, I'm running into a problem in that they seem to want me to compromise the integrity of my domain model in various ways, especially on finer points of C# object design. This is one of several questions on the subject.

我很习惯在适当的物业,看起来像这样的模式执行不变性:

I am very used to enforcing immutability on appropriate properties with a pattern that looks like this:

public class Foo
{
    private readonly string bar;
    public string Bar { return this.bar; }

    public Foo(string bar)
    {
        this.bar = bar;
    }
}

这似乎并没有被NHibernate的或实体框架的支持。他们希望默认构造函数和公开制定者;看来即使私人 setter方法​​(和默认的构造函数)的工作(有时?),因为奥姆斯可以使用反射。

This does not appear to be supported by NHibernate or Entity Framework. They want default constructors and public setters; it appears even private setters (and default constructors) work (sometimes?) because the ORMs can use reflection.

我想我可以用私人制定者和私人默认构造函数绕过这些。至少这样的公共API不受损害。这只是我修改了我所有的班级的实施,以增加未使用私人构造,并具有信任未来Domenic他明白私人我的制定者真正的意思是不要叫我除了在构造函数中。持久层泄漏到我的域对象的设计。

I suppose I can get around these by using private setters and a private default constructor. At least then the public API is not compromised. It is just that I am modifying all of my classes' implementations to add unused private constructors, and having to trust future-Domenic that he understands private on my setters really means "don't call me except in the constructor." The persistence layer is leaking into my domain object design.

这也只是似乎没有必要---为什么不能ORM知道使用非默认的构造函数?也许他们都能够,我只是没有找到合适的博客文章,解释了如何。

It also just seems unnecessary---why can't the ORM know to use the non-default constructor? Maybe they are able to, and I just didn't find the right blog post explaining how.

最后,在某些情况下,我的不可变的值对象的真正合身的(不变)的值类型的,即结构秒。我的猜测是,这是可能的,因为在数据库中我的结构领域将在同一行父实体存储在显示出来。你能否确认/拒绝? 本博客文章看起来很有希望,因为它给出了一个肯定的答案,但$量C $ C(这实际上是针对有问题的值类型)蹒跚的头脑。

Finally, in some cases my immutable value objects actually fit well as (immutable) value types, i.e. structs. My guess is that this is possible, since in the database the fields of my struct will show up in the same row that the parent entity is stored. Can you confirm/deny? This blog post looks promising in that it gives an affirmative answer, but the amount of code (which is in fact specific to the value type in question) staggers the mind.

这是令人沮丧的,经过几年读书一样的有效的C#的或博客像埃里克利珀的,这给如何设计EX pressive和防弹C#对象伟大的建议书,需要使用奥姆斯是让我扔了很多知识窗外。我希望这里有人能指出我的错误,无论是在他们的能力我还是掌握在我的思考领域建模和奥姆斯的作用。

It is frustrating that after several years reading books like Effective C# or blogs like those of Eric Lippert, which give great advice on how to design expressive and bulletproof C# objects, the need to use ORMs is making me throw much of that knowledge out of the window. I am hoping that someone here can point out where I am wrong, either in my grasp of their capabilities or in my thinking about domain modeling and the role of ORMs.

推荐答案

正如评论指出的那样,你将不得不作出一些妥协,当/如果采用一个ORM。记得在我看来,事情是提高生产率远远超过这些妥协的成本。

As the comments point out, you will have to make some compromises when/if you adopt an ORM. The thing to remember in my opinion is that the gains in productivity far outweigh the "costs" of these compromises.

我也想指出你(因为你是新来的,EF),你可以的自定义T4模板产生EF实体和环境。我认为,如果你玩这个,你可以化解大部分的东西你不喜欢。

I did want to point out to you (since you're new to EF) that you can customize the T4 templates that generate EF entities and contexts. I think if you play around with this, you can iron out most of the things you don't like.

 
精彩推荐
图片推荐