Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular reference issue in 3-tier architecture C#

I want to build a web application with 3-tier architecture in ASP.NET. But I am getting a problem of circular referencing.

I have 3 layer:

  1. Application layer containing UI.
  2. Business layer containing all the logic and domain classes.
  3. Data layer containing all database interaction methods.

I am using the data layer methods in business layer to perform database operations and in these methods I need to pass domain class object to data layer but it can not be done due to circular referencing.

For example I have a Person domain class containing some properties and methods. Now I want to insert that Person into database. I have a method in Person class, named as InsertPerson(). In this method body, I have to call the function of Data layer to insert into database. But I am not able to pass the whole person object into data layer method as data layer reference is added to business layer and vice-versa is not possible.

So how can I avoid this problem? Please suggest.

like image 406
atul Avatar asked Apr 02 '13 05:04

atul


People also ask

How do you resolve circular dependency issues?

There are a couple of options to get rid of circular dependencies. For a longer chain, A -> B -> C -> D -> A , if one of the references is removed (for instance, the D -> A reference), the cyclic reference pattern is broken, as well. For simpler patterns, such as A -> B -> A , refactoring may be necessary.

What is a circular reference C#?

Circular reference occurs when two or more interdependent resources cause lock condition. This makes the resource unusable. To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references.

What is the example of 3 tier architecture?

This can be a relational database management system such as PostgreSQL, MySQL, MariaDB, Oracle, DB2, Informix or Microsoft SQL Server, or in a NoSQL Database server such as Cassandra, CouchDB or MongoDB. In a three-tier application, all communication goes through the application tier.


1 Answers

Is it possible to split the domain objects from the business logic for manipulating them? So you have classes describing the data, containing relatively primitive operations, and then your business layer is more actions on the data - often using several different classes in one action.

You then end up having four assemblies:

  UI              /
  Business logic  | Domain classes
  Data layer      \

So all three layers use the domain classes as common terminology, effectively.

I've seen this work pretty well - it does mean that your domain classes typically become slightly "dumb", although they can still contain relevant logic around some validation etc for aspects which are independent of other classes.

Of course, there are plenty of alternative approaches :)

like image 123
Jon Skeet Avatar answered Sep 22 '22 16:09

Jon Skeet