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; } }
source share