Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm planning to use EntityFramework as my back end layer. I have also defined a ServiceLayer (not WCF), which has method which closely mimics application use cases.

I'm trying to figure out the role of Business Logic Layer, and DAL layers and how Service layer interacts with them. Take this below scenario.

I have Several entities
1. Phone(PhoneId, PhoneNumber, CreatedBy, CreateDate)
2. PersonPhone(PhoneId, PersonId, PhoneType)
3. UserAccount(UserId, AccountId) -- contains user to account mapping.
4. PersonAccount(PersonId, AccountId) -- contains person to account mapping.
5. SequenceMaster(EntityName, NextSeqNum) -- Keeps a track of next id to be generated for a row.

Use Case:
A user can attach a phone to a person through a UI. Hence, I have a service layer method called AddPhoneToPerson(loggedInUserId, acctId, personId, phonenumber, phonetype) which UI can call.

Now for the above call, I have to do certain validation and generated ids from Sequence master table. Given below are the steps.

1. Validate if the logged User Id is mapped to Account.
2. Validate if the person Id is mapped to Account.
3. Generated the next sequence number for the Phone entity.
4. Insert Phone row.
5. Insert PersonPhone row.

I'm able to achieve that by putting all the logic inside the service layer method but wondering how to achieve this using BLL, UOW, and Repository layer pattern.
if(db.AppUserAccounts.Count(x=>x.Account_Id == input.AccountId && x.AppUserID == userId)<=0)
              {
                  throw new Exception("You donot have access to this account");
              }

              if (db.BusinessEntities.Count(x => x.AccountId == input.AccountId && x.BusinessEntityID == input.BusinessEntityId) > 0)
              {
                  input.Phone.PhoneId = phoneId;
                  input.Phone.CreatedBy = Convert.ToInt32(msg.UserId);
                  input.Phone.CreatedDate = DateTime.Now;
                  db.Phones.Attach(input.Phone);
                  db.Entry(input.Phone).State = EntityState.Added;
                  db.BusinessEntityPhones.Add(new BusinessEntityPhone() { BusinessEntityID = input.BusinessEntityId, PhoneId = phoneId, PhoneTypeCd = input.PhoneTypeCd });
                  db.SaveChanges();
              }
              else
              {
                  throw new Exception("You are accessing invalid business entity");
              }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900