How should UI layer pass user input to BL layer?

I’m building an n-tier application, I have UI, BL, DAL & Entities (built from POCO) projects. (All projects have a reference to the Entities).

My question is – how should I pass user input from the UI to the BL, as a bunch of strings passed to the BL method and the BL will build the object from the parameters, or should I build the objects inside the UI submit_function and send objects as parameters?

EDIT: I wrote n-tier application, but what I actually meant was just layers.

how should I pass user input from the UI to the BL, as a bunch of strings passed to the BL method and the BL will build the object from the parameters, or should I build the objects inside the UI submit_function and send objects as parameters?

Sending Objects as parameters are preferable by me. Advantages:

  1. In case if we need more parameters in future, to be used by SL/BL layer we can include them without changing the method signatures in Service, Business and DAO layers.
  2. Having a object with data can be reused by layers. If BL returns the same object type we can reuse the initialized object by changing its attributes.
  3. More readable code. Suppose you have to pass 10-20 parameters, rather passing all those in separate fields I recommend a single Object as parameter.

Disadvantages:

  1. We need to initialize a object that takes load time and memory.
  2. We need to use lines of code to prepare and fetch object attributes.

In an ideal (service oriented multi-tier) world UI should communicate to a business layer through data contracts and a service facade. The UI should not need to know anything about the actual business layer or the entities and methods the business layer works with. So a UI and service facade would share a data contract definition (these can be simple container classes or XSD’s). The UI would call methods on an exposed service facade using the data contract to pass the business data back and forth. It’s then up to the service facade to translate the data contract into BL entities and call the BL methods necessary to perform the service.

Why?

  1. This creates a clean interface between your UI and BL based on purposely and explicitly exposed services and contracts. Changes in one layer do not need to impact another.
  2. It aids in creation of automated unit tests. You can even test the UI with mock implementations of the service facade.
  3. It pushes for a more service focused approach. It is services that are exposed rather than a series of individual method calls. “Hopefully” limiting the number of calls between a UI and BL.

4

Here’s how I typically build my n-tier applications, in this case, let’s use an MVC web application project.

A class library, typically called: ProjectFoo.Domain:

Here’s where I create my abstract repositories for accessing data. For example:

public interface IAccountRepository
{
    IEnumerable<Account> FindAll();
}

I also create my concrete implementations here, one accesses the real database, the other just returns mock information:

public class DbAccountRepository : IAccountRepository
{
    public IEnumerable<Account> FindAll()
    {
        DbConnector db = new DbConnector();
        return db.Accounts;
    }
}

public class MockAccountRepository : IAccountRepository
{
    public IEnumerable<Account> FindAll()
    {
        return new List<Account>(){
            new Account { Name = "Sergio", Age = 22 },
            new Account { Name = "Brad", Age = 42 },
            new Account { Name = "Chelios", Age = 32 },
        }
    }
}

Next, in my UI project, typically called ProjectFoo.WebUI:

Inside a controller I use dependency injection to sort out what implementation I’m working against:

public class AccountController 
{
    IAccountRepository _accountRepository = new IAccountRepository();

    public AccountController(IAccountRepository accountRepository)
    { 
        _accountRepository = accountRepository;
    }

    public ActionResult Index()
    {
        // Magic! We don't need to know what exactly we're working against.
        var accounts = accountRepository.FindAll();
    }
}

Now onto your question, How to pass user input from the UI to the BL?.

I have my interface written like this:

public class DbAccountRepository : IAccountRepository
{
    // Notice this is the entity object type.
    public void AddAccount(Account account)
    {
        db.AddToAccounts(account);
        db.SaveChanges();
    }
}

Grab user input, shape it, validate it, fit it inside a simple POCO object – once you’re sure it’s sanitized and functioning correctly, use something like AutoMapper to map to your entity object and pass fully loaded object along the chain towards your repository.

If you have any question or don’t quite understand something let me know.

1

A service is an interface. A method/function is an interface. A service should be for overcoming changes to the physical layers, not logical layers.

You could even have a single logical layer that is split over several physical networks and communicates with itself. So services should not be a hard-line for splitting logical layers. They are for physical splits.

Now if you want your UI and BLL to have the flexibility to work whether on the same box or different, then go with a service. Otherwise it’s not a sin to have a direct method call to the bll.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật