Unified Data Access Level for MongoDB and SQL Server

Our ASP.NET MVC project uses MS SQL Server (for most data) and MongoDB (the least important things, such as audit logs, internal messaging, notifications, etc.) at the same time. So the question is, what should the data access layer architecture look like in my case? We use the PICO Framework Entity Framework generator to access SQL Server, since unit testing is important, and ideally I would prefer to extend the IEntities interface created by the Entity Framework so that business logic and user interface developers do not even know where the actual object is stored: / p>

[GeneratedCode("Entity","0.9")] public partial interface IEntities { IObjectSet<Administrator> Administrators { get; } IObjectSet<User> Users { get; } IObjectSet<Banner> Banners { get; } IObjectSet<AuditLog> AuditLogs { get; } ... } 

In this example, only AuditLog objects are stored in MongoDB, and the rest through SQL Server.

The best solution I have so far is to implement the IObjectSet <T> interface on top of the C # MongoDB driver with MongoRepository ( http://mongorepository.codeplex.com/ ) - this is half the solution for my problem. Does anyone know a better approach?

+4
source share
1 answer

The problem with MongoRepository is that your objects must be retrieved from the Entity base class or implement IEntity in each POCO. I am working on a solo project, which I called "MongoDB.Dynamic", which will be compatible with the POCO entity infrastructure classes. With MongoDB.Dynamic will allow you to use only interfaces to save data.

 [TestInitialize] public void Initialize() { Dynamic.Config.SetKeyName<ICustomer>(c => c.Id); Dynamic.Config.SetKeyName<IOrder>(o => o.Id); Dynamic.Config.LoadCollection<ICustomer, IOrder>(customer => customer.Orders, order => order.IdCustomer); Dynamic.Config.LoadFK<IOrder, ICustomer>(order => order.Customer, order => order.IdCustomer); var customers = Dynamic.GetCollection<ICustomer>(); var orders = Dynamic.GetCollection<IOrder>(); customers.RemoveAll(true); orders.RemoveAll(true); } [TestMethod] public void TestLoadOrderByCustomerAuto() { var customers = Dynamic.GetCollection<ICustomer>(); var orders = Dynamic.GetCollection<IOrder>(); var cust = customers.New(); cust.Name = "X"; customers.Upsert(cust); var check = customers.GetFirstOrDefault(); var o1 = orders.New(); o1.IdCustomer = check.Id; orders.Upsert(o1); var o2 = orders.New(); o2.IdCustomer = check.Id; orders.Upsert(o2); var verify = customers.GetFirstOrDefault(); Assert.IsNotNull(verify.Orders); Assert.IsTrue(verify.Orders.Count() == 2); } 

In a couple of days I will publish this project. Can't wait to share with the community.

EDIT: The interfaces referenced by the code above:

 public interface ICustomer { int Id { get; set; } string Name { get; set; } IEnumerable<IOrder> Orders { get; set; } } public interface IOrder { int Id { get; set; } int IdCustomer { get; set; } ICustomer Customer { get; set; } } 
+5
source

Source: https://habr.com/ru/post/1402115/


All Articles