4

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?

1

1 Answer 1

5

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; }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Looks similar to the Code First approach in Entity Framework. What is the return type of Dynamic.GetCollection<ICustomer>()?
The return type is an object that encapsulates an internal mongoCollection and it exposes CRUD methods to query and persist data. The New() method creates an autoimplemented instance of the interface
I've just submited the MongoDB.Dynamic (my baby) to a public free SVN repository. Take a look at assembla.com/spaces/mongodb-dynamic/wiki
There's some doc and nuget package at MongoDB.Dynamic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.