Valentyn Vynogradskiy
Valentyn Vynogradskiy

Reputation: 653

DDD with MongoDB

I can't believe I didn't find good article about it. So it might be a 1000 times asked question.

I'm writing a sample, which consists of Nancy MVC, plus Service Layer, Core Layer, DAL. Where DAL use MongoDB. What I'm trying to achieve is to separate MongoDB from Core. In any example I'm reading about mongoDB I'm seeing something like follows:

public class Customer
{       
    public ObjectId Id{get;set;}
    public string Name { get; set; }
    public string Address { get; set; }
    IEnumerable<string> Telephones { get; set; }
    [BsonElement("PublicWebPage")]
    public WebPage PublicPage { get; set; }
} 

Which from my understanding is not so good, as I need to install MongoDB Driver into my Core. On the other hand I can try put this models to DAL and Write almost the same one in Core, and with help of some mapper map one to another, that means that it will be two duplicated objects.

What I'm trying to find is the approach that may be will copy EF Fluent API approach, or any different one, that can help me to keep my models clean.

Upvotes: 3

Views: 2366

Answers (1)

Craig Wilson
Craig Wilson

Reputation: 12624

Everything that can be mapped with attributes in MongoDB is also able to be done programatically. Avoiding ObjectId would be the only other necessary change. See the documentation here: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/

On a separate note, having two models that look almost the same for very different purposes isn't wrong. Depending on your application, this might be the best way to keep each layer's responsibility from interfering with another's.

Upvotes: 4

Related Questions