简体   繁体   English

.NET,分层架构和MongoDB - 什么用作ID?

[英].NET, Layered Architecture & MongoDB - What to use as ID?

I'm developing a .NET web service while trying to maintain a layered architecture that keeps my Models in one project and DB access (DAL) in a different one. 我正在开发一个.NET Web服务,同时尝试维护一个分层的体系结构,将我的模型保存在一个项目中,将数据库访问(DAL)保存在另一个项目中。 The idea behind this is that if I have to change the DB technology, it's just a matter of creating a differnet DAL, while the rest of the application remains unchanged. 这背后的想法是,如果我必须更改数据库技术,只需创建一个不同的DAL,而应用程序的其余部分保持不变。

In the data-access layer that I'm developing, I am using Mongo DB C# Driver . 在我正在开发的数据访问层中,我使用的是Mongo DB C#Driver

I've seen that: 我见过:

  • Properties named "ID" will be mapped by the C# driver as the database's "_id" ( Convention over configuration ); 名为“ID”的属性将由C#驱动程序映射为数据库的“_id”( 约定优于配置 );

  • Int + Auto-increment in MongoDB is not a good idea ; Mong +DB中的Int +自动增量不是一个好主意 ;

  • Using Guid's as ID in MongoDB isn't a good idea either ; 在MongoDB中使用Guid作为ID 也不是一个好主意 ;

  • The recommended data type for the ID of documents stored in MongoDB is ObjectID . MongoDB中存储的文档ID的推荐数据类型是ObjectID The C# driver provides a class to represent this; C#驱动程序提供了一个表示它的类;

    • However, if I use this data type (from MongoDB.Bson ) in my Models, then they will become dependent on the MongoDB C# Driver and I don't want that: I want my models to be DB-independent; 但是,如果我在我的模型中使用这种数据类型(来自MongoDB.Bson ),那么它们将依赖于MongoDB C#驱动程序而我不希望这样:我希望我的模型与数据库无关; only my DALs can depend on whatever data access technologies I use. 只有我的DAL可以依赖于我使用的任何数据访问技术。

So what data type should I use for my POCOs' IDs in order to have guarantee uniqueness in the DB? 那么我应该将哪种数据类型用于我的POCO ID,以便在数据库中保证唯一性? Would a string representation of a Guid be horrible in terms of performance? Guid的字符串表示在性能方面是否可怕?

Your feedback is welcome. 欢迎您的反馈。

Good question. 好问题。

From Experience, I can say that you're right: both GUIDs and auto-increment aren't the best idea (with GUID being a lot better than auto-increments), but not only for the reason mentioned in the SO question you linked to, but mostly because you need to be aware of the implications of monotonic vs. non-monotonic keys. 根据经验,我可以说你是对的:GUID和自动增量都不是最好的主意(GUID比自动增量好很多),但不仅仅是因为你链接的SO问题中提到的原因但是,主要是因为您需要了解单调与非单调键的含义。

With the ObjectIds , I see three options: 使用ObjectIds ,我看到三个选项:

  • Map between domain model and DAL. 域模型和DAL之间的映射。 In the domain model, you could use the objectid's string representation. 在域模型中,您可以使用objectid的字符串表示。 That's a bit annoying, but it forces you to separation of concerns. 这有点烦人,但它迫使你分离关注点。

  • Use your own data type and implement a type converter / mongodb serializer. 使用您自己的数据类型并实现类型转换器/ mongodb序列化程序。 I haven't tried that but I don't see why this wouldn't work. 我没试过,但我不明白为什么这不起作用。

  • Accept the MongoDB dependency. 接受MongoDB依赖项。 After all, if you really swap out your database, that will be a huge task. 毕竟,如果你真的换掉了你的数据库,那将是一项艰巨的任务。 Different databases have very different characteristics and require very different data models. 不同的数据库具有非常不同的特征,并且需要非常不同的数 The whole "swap out the database" in a minute is bogus IMHO, it's never that easy and a database is a much leakier abstraction than anyone wants to admit. 在一分钟内整个“交换数据库”是虚假的恕我直言,它从来没有那么容易,数据库是一个比任何人都想承认的更漏洞的抽象。 Trying to keep independent is a PITA. 试图保持独立是PITA。 Anyway, doing a seek-and-destroy on the word ObjectId will be less than 1% of the other work. 无论如何,对ObjectId这个词进行搜索和破坏将不到其他工作的1%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM