2

I’m following the suggested architectural path put forward in “Programming Entity Framework Code First”.

There is a DataAccess layer and a Model layer which both form separate projects in VS.

The Model layer holds my classes for business objects. The DataAccess layer has a reference to the Model project so that it can create a context and DbSets for each of my business objects.

The issue is that some classes in the Model need to access the data layer to perform calculations, however I cannot reference the DataAccess layer in my Model project as it will create a circular reference. The DataAccess layer must reference the Model layer so that it can create the DbSets. Please also note the calculations are read only – only getters, which are not persisted to the database.

I’ve been searching for hours on this and have found useful information but I think I’m missing something simple? POCO classes are meant to be simple, but my classes represent things that have some very related but more complicated calculations.

As a simple concrete example I have a Transaction class and an AccountBalance class. The Transaction class needs to know the AccountBalance on specific dates for display purposes – such as percentage change (this is just a simple example):

public class Transaction

{
    public DateTime Date { get; set; }
    public string Description { get; set; }    
        ... etc    
    public double PercentageChange
    {
        get
        {
            // return TransactionAmount / AccountBalance on TransactionDate    
            //  however Transaction has no knowledge of AccountBalance... 
         }
    }
}

Thanks

1 Answer 1

2

Personally I would NOT place the business logic in my POCOs. Imho you need to create a separate service layer (or whatever you call it) and place the business logic there. The POCOs should really be simple objects without any business logic.

So your architecture will look like this:

GUI -> Service Layer -> Data Access Layer (Repositories?) -> Database.

Now all your projects can reference your model layer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Leon. Sounds good but I am missing something still. That alleviates the access issue, but I want to have a class that has all of the required properties in it (such as PercentageChange above). I can do that by making public class TransactionService : Transaction and adding my properties in... But the data is still tied to Transaction isn't it? If I use _Context.Transactions.ToList() I won't have access to my new properties etc. If I make additional logic to get a list of Transactions in my TransactionsService class I'm defeating the purpose of using EF... Im confused, thanks for help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.