Confused by UnitOfWork

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
 
P

Peter Duniho

[...]
I get the error:
Missing partial modifier on declaration of type
'MyApp.Domain.Model.MyAppContext'; another partial declaration of this
type exists

You've only shown one declaration of MyAppContext. With only one
declaration, there should be no need for "partial".

A concise-but-complete code example would show two MyAppContext
declarations, if the "partial" keyword is relevant to the question.
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

There's no Delete() method in your ProductRepository class, never mind one
missing a method body.

A concise-but-complete code example would show an actual Delete() method.
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.

If "EF" is an abbreviation that is required knowledge for understanding
your question, and is supposed to be known by anyone who might answer your
question, you've posted to the wrong newsgroup.
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.

If you can present your question strictly in terms of C#, and include a
concise-but-complete code example that reliably demonstrates the problem,
then do so. Otherwise, find a forum that is more appropriate for your
question.

Pete
 
S

shapper

If you can present your question strictly in terms of C#, and include a  
concise-but-complete code example that reliably demonstrates the problem, 
then do so.

Of course. The EF generates the following:

public partial class MyAppContext :
global::System.Data.Objects.ObjectContext
{
public MyAppContext() :
base("name=MyAppContext", "MyAppContext")
{
this.OnContextCreated();
}
public MyAppContext(string connectionString) :
base(connectionString, "MyAppContext")
{
this.OnContextCreated();
}
// Other methods
}

Then I created the following:

public partial class MyAppContext : ObjectContext, IUnitOfWork {
public void Commit() {
SaveChanges();
} // Commit
public void Rollback() {
Dispose();
} // Rollback
}

public interface IUnitOfWork {
void Commit();
void Rollback();
}

And in my repository I have:

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

The moment I added partial I stopped having errors.

The other errors I got were because I forgot to implement some of the
methods on my repository.

I am not completely sure about partial because on a code example I saw
they do not use it ...
.... But I think it was some type of mistake.

Now it compiles. I think it is correct. I am just testing things.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top