3

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.

TechWisdom
  • 3,960
  • 4
  • 33
  • 40
  • 1
    @Valentyn...I've just found your question having got the same problem myself. Did you find a suitable solution? – Stu1986C May 07 '17 at 14:58
  • 1
    @Stu1986C... You might want to make DAL Models and use something like AutoMapper that will save your tome, but unfortunately for that issue this is solution that i found convenient without falling into over engineering and "theoretical" programming. – Valentyn Vynogradskiy May 08 '17 at 15:13

1 Answers1

4

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.

Craig Wilson
  • 12,174
  • 3
  • 41
  • 45