S
shapper
Hello
I am using Entity Framework to model my database SQL tables and
relationships:
Model Name: MyApp
Object Context: MyAppContext
Namespace: MyApp.Domain.Model
I am trying to implement UnitOfWork pattern.
I know the model generated by EF is already an UnitOfWork pattern.
However I would like to create my own UnitOfWork to use it as follows:
IUnitOfWork unitOfWork = new MyAppContext();
CustomerRepository customerRepository = new CustomerRepository
(unitOfWork);
Customer customer = customerRepository.GetCustomerById(2);
ProductRepository productRepository = new ProductRepository
(unitOfWork);
Product product = productRepository.GetById(1);
OrderRepository orderRepository = new OrderRepository(unitOfWork);
Order order = new Order(customer);
order.AddNewOrderDetail(product, 1);
orderRepository.AddOrder(order);
unitOfWork.Save();
So I created my UnitOfWork as follows:
namespace MyApp.Infrastructure {
public interface IUnitOfWork {
void Commit();
void Rollback();
}
}
And the context as follows:
namespace MyApp.Domain.Model {
public class MyAppContext : ObjectContext, IUnitOfWork {
public void Commit() {
SaveChanges();
} // Commit
public void Rollback() {
Dispose();
} // Rollback
}
And the repository (Example) as follows:
namespace MyApp.Domain.Repositories {
public class ProductRepository : IProductRepository {
private MyAppContext context;
public ProductRepository(IUnitOfWork unit) {
if (unit == null)
throw new ArgumentNullException("unit");
context = unit as MyAppContext;
}
public Asset GetById(Guid id) {
return context.AssetSet.Where(a => a.Id.Equals
(id)).SingleOrDefault();
} // GetById
// Other repository methods
}
I get the error:
Missing partial modifier on declaration of type
'MyApp.Domain.Model.MyAppContext'; another partial declaration of this
type exists
So I added Partial to MyAppContext and I get:
'App.Domain.Repositories.ProductRepository.Delete(System.Guid)' must
declare a body because it is not marked abstract, extern, or partial
I think I am interpreting all this wrong.
Maybe MyAppContext should have nothing to do with MyAppContext created
by EF.
Maybe I should name MyAppEntities to the one created by EF and then
name this MyAppContext.
But I am not sure how both relate ... If they relate.
Anyway, I am a little bit confused about this.
I appreciate if someone could advice me on this.
Thanks,
Miguel
I am using Entity Framework to model my database SQL tables and
relationships:
Model Name: MyApp
Object Context: MyAppContext
Namespace: MyApp.Domain.Model
I am trying to implement UnitOfWork pattern.
I know the model generated by EF is already an UnitOfWork pattern.
However I would like to create my own UnitOfWork to use it as follows:
IUnitOfWork unitOfWork = new MyAppContext();
CustomerRepository customerRepository = new CustomerRepository
(unitOfWork);
Customer customer = customerRepository.GetCustomerById(2);
ProductRepository productRepository = new ProductRepository
(unitOfWork);
Product product = productRepository.GetById(1);
OrderRepository orderRepository = new OrderRepository(unitOfWork);
Order order = new Order(customer);
order.AddNewOrderDetail(product, 1);
orderRepository.AddOrder(order);
unitOfWork.Save();
So I created my UnitOfWork as follows:
namespace MyApp.Infrastructure {
public interface IUnitOfWork {
void Commit();
void Rollback();
}
}
And the context as follows:
namespace MyApp.Domain.Model {
public class MyAppContext : ObjectContext, IUnitOfWork {
public void Commit() {
SaveChanges();
} // Commit
public void Rollback() {
Dispose();
} // Rollback
}
And the repository (Example) as follows:
namespace MyApp.Domain.Repositories {
public class ProductRepository : IProductRepository {
private MyAppContext context;
public ProductRepository(IUnitOfWork unit) {
if (unit == null)
throw new ArgumentNullException("unit");
context = unit as MyAppContext;
}
public Asset GetById(Guid id) {
return context.AssetSet.Where(a => a.Id.Equals
(id)).SingleOrDefault();
} // GetById
// Other repository methods
}
I get the error:
Missing partial modifier on declaration of type
'MyApp.Domain.Model.MyAppContext'; another partial declaration of this
type exists
So I added Partial to MyAppContext and I get:
'App.Domain.Repositories.ProductRepository.Delete(System.Guid)' must
declare a body because it is not marked abstract, extern, or partial
I think I am interpreting all this wrong.
Maybe MyAppContext should have nothing to do with MyAppContext created
by EF.
Maybe I should name MyAppEntities to the one created by EF and then
name this MyAppContext.
But I am not sure how both relate ... If they relate.
Anyway, I am a little bit confused about this.
I appreciate if someone could advice me on this.
Thanks,
Miguel