dimanche 4 juin 2017

How to avoid circular dependency in code first entity framework design

I am trying to create modular (component based) application.

My scenario is so simple, I have a SalesManagement component which contains Invoice, and I have PartyManagement component which contains Customer,

so far so good, Then I defined a class for Customer as

public class Customer {
   public int ID {get;set;}
   public string Name {get;set;}
}

and Invoice as

public class Invoice {
   public int ID {get;set;}
   public int CustomerID {get;set;}
   public Customer Customer {get;set;}
}

in order to have Customer reference in Invoice I referenced PartyManagement in SalesManagement component

then I tried to add a collection of Invoice under customer as

public class Customer {
   public int ID {get;set;}
   public string Name {get;set;}
   public virtual ICollection<Invoice> Invoices {get;set;}
}

to make it happen I have to reference InvoiceManagement in PartyManagement which makes a circular dependency

Any idea how to resolve this design issue?

p.s If I keep everything in 1 assembly there is no problem, I want to keep them in separate assemblies

Aucun commentaire:

Enregistrer un commentaire