2015-06-25 37 views
0

我正在實施鬆散耦合的架構。 MVC作爲表示層(ProjectName.Web),所有業務邏輯將在單獨的C#項目ProjectName.BL中處理。我如何在BL中使用模型到控制器MVC

我將從我的BL層(ProjectName.BL)中使用Web服務。所以,請求參數類對象對於BL來說是可見的,因爲我正在進行服務引用。

1)我面臨的問題是如何將我的請求參數從控制器發送到BL層。 2)接下來的問題是如何在BL中映射ViewModel對象,因爲視圖模型在我的Web項目中。

請求您的幫助,我無法做到這一點。

回答

0

這就像關於問題分層和分離的任何其他問題。

使用DTO。在業務層中,引入用於執行要執行的操作的類型,並在類型之間執行映射。

你的問題不是很具體,所以我會去美孚:

服務層:

public class ServiceFooRequest 
{ 
    public int ID { get; set; } 
} 

public class ServiceFooResponse 
{ 
    public string Bar { get; set; } 
} 

public ServiceFooResponse GetFoo(ServiceFooRequest request) 
{ 
    return new ServiceFooResponse 
    { 
     Bar = "Baz" 
    }; 
} 

業務層:

public class BLFooResponse 
{ 
    public string Bar { get; set; } 
} 

public class BLL 
{ 
    public BLFooResponse GetFoo(int id) 
    { 
     var serviceResponse = _serviceReferenceClient.GetFoo(new ServiceFooRequest 
     { 
      ID = id 
     }); 

     return new BLFooResponse 
     { 
      Bar = serviceResponse.Bar 
     }; 
    } 
} 

MVC:

public class FooViewModel 
{ 
    public string Bar { get; set; } 
} 

public ActionResult GetFoo(int id) 
{ 
    var businessFooResponse = _bll.GetFoo(id); 
    var fooViewModel = new FooViewModel 
    { 
     Bar = businessFooResponse.Bar 
    };  
    return View(fooViewModel); 
} 
+0

謝謝您的答覆,上述方案解決了我的第二個問題。我將如何將控制器的請求參數作爲對象發送給BL,而不是像Int id,string'vin',decimal 12.00。因此,該對象將映射到服務請求參數以調用服務 – user3334074

0

你需要單獨的pro ject(DLL)模型。它將存儲層模型之間共享。每個控制器應該有BL對象或BLL工廠的參考。 提示 - 使用Automapper將DAO對象複製到BDO。

Web項目

public class FoodController : BaseController 
{ 
    private IFoodBll _foodBll = null; 

    public FoodController(IFoodBll foodBll) 
    { 
     // Make DI of your BLL 
     _foodBll = foodBll; 
    } 

    [HttpPost] 
    public ActionResult Edit(FoodEditModel model) 
    { 
     _foodBll.Save(model); 
    } 

你Edit.cshtml應該水木清華這樣

@model MyProjects.Web.Models.Foods.FoodEditModel 

@Html.HiddenFor(x => Model.Id) 
@Html.EditorFor(x => Model.Name) 
+0

請您告訴我一個示例或鏈接,其中將瞭解清晰的圖片。我目前架構​​中的約束是,我無法引入任何圖層 – user3334074

相關問題