Reputation: 56
So I'm stuck with this project that I'm working on which I'm trying to get the architecture done properly. I'm using MVC and Entity Framework, I've decided to lay out my application into layers using the n-tier model. So I have the following:
Data Layer -> This is where Entity Framework resides and I'm using code first approach so I have models that are mapped to tables.
I also have repositories that return these models (Data access) etc.For example:
MyModel -> Contains properties that map to the database table
MyRepo -> Contains methods that does things like Add(MyModel model), Save(MyModel model).
Service Layer -> This is where I intend to keep all business logic, this layer references the Data layer and I could pass in models here.
For example: MakePurchase(MyModel model), it's alright to take the MyModel object here because the service layer references/depends on the Data layer
UI Layer -> This is the frontend using MVC, so my problem is how do I get the models from Data Layer to here without referencing it or depending on it? Ideally, I only want it to depend on the Service layer, cause if the UI Layer depends on the Data Layer as well, wouldn't that be tight coupling? Does this mean I have to maintain the same set of models from the Data layer in the UI? Also, thing is the models in the front end is also different then the ones in the data layer for example:
Data layer model would be something like:
MyModel
{
public int Id;
public int Name;
public int CategoryId;
}
Whereas my model in the front end would be something like:
MyModel
{
public int Id;
public int Name;
public int CategoryId;
public List<String> Categories; // Which I could use to populate a dropdown box
}
I hope this makes sense? So I'm stuck between the different models on the Data Layer and UI layer, because I would have to map the UI layer model to the Data Layer model before I could pass it on to the service layer in my controllers right? But at the same thing I don't want to introduce too much code in my controllers, and I do not want to tightly couple it back to the Data Layer.
Thanks
Upvotes: 2
Views: 2025
Reputation: 18569
My approach is to create a business model in the service layer, that mimic your database model.
For example:
In your data layer you have class
class MyModel
{
public int Id;
public int Name;
public int CategoryId;
}
Then in your business layer, create a class that mimic the MyModel
.
class MyModelB
{
public int Id;
public int Name;
public int CategoryId;
public List<String> Categories;
}
When you add a new data, UI layer will create MyModelB
object. Then in your service layer transform it to MyModel
object.
// service layer
public void Add(MyModelB model)
{
MyModel obj = new MyModel();
obj.Id = model.Id;
obj.Name = model.Name;
obj.CategoryId = model.CategoryId;
MyModelRepository r = new MyModelRepository ();
r.Add(obj);
}
When you get data from database, transform it to business model before you return the object from service layer to UI layer.
// service layer
public MyModelB GetMyModelById(int id)
{
MyModelRepository r = new MyModelRepository ();
MyModel model = r.GetMyModelById(id);
if(model != null)
{
MyModelB obj = new MyModelB();
obj.Id = model.Id;
obj.Name = model.Name;
obj.CategoryId = model.CategoryId;
}
else return null;
}
Anyway, you can use AutoMapper to handle mapping object properties from data layer and business layer or vica versa.
Upvotes: 3