4

Change the custom type from PL to BLL to DAL to insert a record?

 2 years ago
source link: https://www.codesd.com/item/change-the-custom-type-from-pl-to-bll-to-dal-to-insert-a-record.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Change the custom type from PL to BLL to DAL to insert a record?

advertisements

I am using Entity framworke in my project. I am using 3 layer architecture ( Presentation layer(PL), Business logic layer(BLL) and Data Access Layer(BAL)). Entity framework define all entity as well as CRUD operation to BD.

I have encountered one basic problem. Suppose i need to Insert customer in DB. I want to do is as follow

---------------PL-------------
Customer ObjCustomer=new Customer();

//init object
ObjCustomer.Name="";
--------------
------------

BLL.InsertCustomer(ObjCustomer)

------------------------------------------------

-------------------BLL---------------

DAL.InsertCustomer(ObjCustomer)

------------------------------------

-------------------DAL---------------

CustomerReporitory.InsertCustomer(ObjCustomer)

------------------------------------

Now problem is that customer is defined in DAL as part of EF. It is not advisable to take DAL ref in PL. I want to pass custom class type like customer as parameter. How to do that. Please me some sample code.


The Customer in your presentation layer shouldn't be the Customer from your business or data layer. The Customer in your presentation layer should be something similar to a ViewModel, so it should only carry attributes and be declared in the business layer. That is the type of object that should be sent to the business layer for CreateCustomer, which in turn creates a business entity or DAO from it and passes it for persistence.

Presentation layer

UserViewModel theUser=new UserViewModel(userNameField,passwordField);
userController.CreateUser(theUser);

Business layer

public class UserController
{
    public void CreateUser(UserViewModel user)
    {
        bool isUserValid=ValidateUser(user);
        if(isUserValid)
        {
            UserEntity theEntity=new UserEntity(user);
            _userRepository.Create(theEntity);
        }
        else
        {
            throw new InvalidUserException("This user isn't valid");
        }
    }
}

Data layer

public class UserRepository
{
    public void Create(UserEntity user)
    {
        /* store user to database or whatever */
    }
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK