Sergey Rybalkin
Sergey Rybalkin

Reputation: 3026

Unified data access layer for MongoDB and SQL Server

Our ASP.NET MVC project uses MS SQL Server (for most of the data) and MongoDB (least important stuff like audit logs, internal messaging system, notifications etc.) at the same time. So the question is what should data access layer architecture look like in my case? We are using Entity Framework POCO generator to access SQL Server as unit testing is important and ideally I would prefer to extend an IEntities interface generated by Entity Framework so that business logic and UI developers won't even know where the actual object is stored:

[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 entities are stored in the MongoDB while the rest of them work through SQL Server.

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

Upvotes: 4

Views: 3465

Answers (1)

Jone Polvora
Jone Polvora

Reputation: 2338

The problem with MongoRepository is that your entities must derive from its Entity base class or implements IEntity in every POCO. I'm working on a solo project that I've named "MongoDB.Dynamic" that will be compatible with POCO entity framework classes. With MongoDB.Dynamic will allow you to use just interfaces to persist 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'll publish this project. Can't wait to share with community.

EDIT: The interfaces referenced by 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; }
}

Upvotes: 5

Related Questions