简体   繁体   English

如何使用 mongoDb 数据库尊重清洁代码架构?

[英]How to respect Clean Code Architecture with mongoDb database?

I'm setuping a C# Asp.Net Core Api that will grow quite a bit in the future.我正在设置一个 C# Asp.Net Core Api 将来会增长很多。 So I'm trying to respect the Clean Code architecture, with my domain in the center without any dependences and everything around:所以我试图尊重干净的代码架构,我的域在中心,没有任何依赖和周围的一切:

public abstract class Entity
{
    public Guid Id { get; set; }
}

I'm currently implementing the repositories.我目前正在实施存储库。 My issue is that for mongoDb, it seems mandatory to either provide the [BsonId] attribute, either use the BsonId in my entities.我的问题是,对于 mongoDb,似乎必须提供[BsonId]属性,或者在我的实体中使用 BsonId。 But that implies adding a mongoDb reference in my entity project, which I'm not a big fan.但这意味着在我的实体项目中添加 mongoDb 引用,我不是它的忠实粉丝。

public interface IRepository<TDocument> where TDocument : Entity
{
    IQueryable<TDocument> AsQueryable();
    IEnumerable<TDocument> FilterBy(
        Expression<Func<TDocument, bool>> filterExpression);
    IEnumerable<TProjected> FilterBy<TProjected>(
        Expression<Func<TDocument, bool>> filterExpression,
        Expression<Func<TDocument, TProjected>> projectionExpression);
    Task<TDocument> FindOne(Expression<Func<TDocument, bool>> filterExpression);

    Task<TDocument> FindById(Guid id);
    Task InsertOne(TDocument document);
    Task InsertMany(ICollection<TDocument> documents);
    Task ReplaceOne(TDocument document);
    Task DeleteOne(Expression<Func<TDocument, bool>> filterExpression);
    Task DeleteById(Guid id);
    Task DeleteMany(Expression<Func<TDocument, bool>> filterExpression);
}

In the example I found on Clean Architecture, they are mostly using entity framework, which doesn't require absolutely attributes to work.在我在 Clean Architecture 上找到的示例中,它们主要使用实体框架,它不需要绝对属性即可工作。

I could imagine doing another class and using AutoMapper to map between each other, but that seems cumbersome, since I always want to persist everything that are in my business object, and that could lead to some errors.我可以想象做另一个 class 并在彼此之间使用 AutoMapper 到 map,但这似乎很麻烦,因为我总是想坚持我的业务 object,这可能会导致一些错误。

Isn't there a way to indicate per collection(or even globally) what is the Id in the repository or when saving?没有办法指示每个集合(甚至全局)存储库中的 Id 是什么或保存时?

You could use the "BsonClassMap":您可以使用“BsonClassMap”:

BsonClassMap.RegisterClassMap<SomeEntity>(cm =>
        {
            cm.AutoMap();
            cm.SetIgnoreExtraElements(true);
            cm.MapIdMember(c => c.Id);
        });

Reference: https://mongodb.github.io/mongo-csharp-driver/2.14/reference/bson/mapping/参考: https ://mongodb.github.io/mongo-csharp-driver/2.14/reference/bson/mapping/

The above solution using static BsonClassMap should allow you to identify your Id member without "polluting" your domain.使用 static BsonClassMap的上述解决方案应该允许您识别您的 Id 成员,而不会“污染”您的域。

BsonClassMap.RegisterClassMap<SomeEntity>(cm =>
    {
        cm.AutoMap();
        cm.SetIgnoreExtraElements(true);
        cm.MapIdMember(c => c.Id);
    });

One word of advice, for your own sanity: as an architect, you'll need to pick and choose which rules you follow religiously, and where it makes sense to make exceptions.一句忠告,为了你自己的理智:作为一名建筑师,你需要挑选并选择你虔诚地遵循哪些规则,以及在哪里做出例外是有意义的。

You may find on occasion that a low-risk package reference will make your life considerably easier, whether it be something for data annotations, or a package that gives you Guard clauses or the Specification pattern, etc... I wouldn't be afraid to break the rule or to embrace some anti-patterns when carefully and thoughtfully considered, and you know the reason WHY it is an anti-pattern.您有时可能会发现,低风险的 package 参考将使您的生活变得更加轻松,无论是用于数据注释的东西,还是为您提供保护条款或规范模式的 package 等等……我不会害怕在仔细和深思熟虑的情况下打破规则或接受一些反模式,并且您知道为什么它是反模式的原因。

If you were suggesting adding a dependency to something that was not lightweight, I might suggest you never do it.如果您建议向非轻量级的东西添加依赖项,我可能会建议您永远不要这样做。 However, there are certain packages that are intentionally compact, and adding them to your Core/Domain project shouldn't bring shame to your family name.但是,有些包是故意紧凑的,将它们添加到您的核心/域项目中不应给您的姓氏带来耻辱。 :) :)

Happy coding!快乐编码!

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

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