Please see the DDL below:
create table Person (ID identity not null, varchar(name), Gender char(1), dateofbirth datetime, primary key (id))
create table PersonOffer (PersonID int not null references Person(ID), OfferID int not null references Offer(ID))
create table Offer (ID identity int not null, description varchar(20), primary key(id))
There is a many to many relationship between Person and Offer. PersonOffer is the junction table.
The Person class contains a List of offers because there are true invariants between the Person class and the offer class e.g. if the gender or date of birth changes, then the eligibility for offers changes.
I am trying to decide whether to use the UnitOfWork pattern here. As there is only one aggregate root (Person), then their should be one respoistory (PersonRepository). Therefore I do not believe I need to use the UnitOfWork pattern.
Q1) Should the PersonOffer persistence be done in the PersonRepository? Something like this:
foreach (var offer in Person.Offers) db.dbOffers.Attach(offer);
Q2) Do I need a Unit Of Work here?
The code in the repository creates a new Person with a List of offers as shown below:
public int Create(Application businessApplication)
{
dbApplication dataApplication = Mapper.Map<Application, dbApplication>(businessApplication);
foreach (var offer in dataApplication.dbOffers)
db.dbOffers.Attach(offer);
db.dbApplications.Add(dataApplication);
return db.SaveChanges();
}