Reputation: 4733
I'm doing 3 tier application using asp.net mvc and I want to do everything as recommended.
So I've done MvcSample.Bll
for business logic, MvcSample.Data
for data and MvcSample.Web
for website.
In Data
I've my edmx
file (I'm using database first approach) and my repositories. And in Bll
I'm doing services which will called in web.
So my question is that:
Should I write other models in Bll
or use that ones which are generated in edmx file?
Upvotes: 4
Views: 1965
Reputation: 36
It heavily depends on the type of problem that your application is trying to solve.
From my experience, it is very rare that the business logic returns model objects directly from Entity Framework. Also, accepting these as arguments may not be the best idea.
Entity Framework model represents your relational database. Because of that, its definition contains many things that your business logic should not expose, for example navigation properties, computed properties etc. When accepting your model object as an argument, you may notice that many properties are not used by the particular business logic method. In many cases it confuses the developer and is the source of bugs.
All in all, if your application is a quick prototype, proof of concept or a simple CRUD software than it might be sufficient to use EF model classes. However, from practical point of view consider bespoke business logic model/dto classes.
Upvotes: 1
Reputation: 1914
You can use 3 tier architecture in asp.net in this way
MvcSample.BLL
- business logic layerMvcSample.DAL
- Data access layerMvcSample.Domain
- Domain layerMvcSample.web
- websiteAll your repository classes are including in .BLL
layer.That means your logics are stored here.
Usually .DAL
is used for storing .edmx
classes. .Domain
is using for recreate database objects that are useful for server side.That means if you are passing a json
object from client to server,Then that object should be create on the server side.So those classes can be implement in the .domain
Upvotes: 0
Reputation: 2032
It depends on your view about software design and how you want to take advantage of it. by separating BLL model, you will have your freedom to put story specific validation and calculation. By using only DLL model, it is sometimes tough as it is going to take effect in DB.
Upvotes: 0
Reputation: 442
My approach will be
MvcSample.Data
-- Model Classes
-- EDMX attach to model
MvcSample.Bll
-- Model Inheriting MvcSample.Data.Model
-- Business Logic Class - Using MvcSample.Bll.Model
MvcSample.Web
-- Controller using MvcSample.Bll.Model
Upvotes: 0
Reputation: 3259
I think there is no right or wrong answer for your question.
In my experience, I used both. Let's see at below example:
I have an User
table
public class User
{
public int Id{get;set;}
public string First_Name{get;set;}
public string Last_Name{get;set;}
public int Age{get;set;}
public string Password{get;set;} //let's use this for demonstration
}
I have a Method call DisplayAll()
in Bll
. This method should list down all users in my database by Full Names (FirstName + LastName) and their Ages.
I should not return User
class because it will expose the Password, but rather, I create a new Class UserDto
public class UserDto
{
public string FullName{get;set;}
public int Age{get;set;}
}
So here is my DisplayAll()
:
public List<UserDto> DisplayAll()
{
List<UserDto> result = ctx.User //my DbContext
.Select(x => new UserDto()
{
FullName = x.First_Name + " " +Last_Name,
Age = x.Age
}
return result;
}
So as you can see, my method DisplayAll()
uses both User
and UserDto
Upvotes: 0
Reputation: 9849
From my point of view you need another model for your Bll
.
That would encapsulate your Bll
completely.
Upvotes: 0