2012-04-23 8 views
6

Hiện tại tôi đang tạo giao diện web cho FreeRADIUS. Nó chỉ là một ứng dụng nhỏ, để đơn giản hóa các đột biến cho các đồng nghiệp lười biếng Shell-và SQL. Tôi đã tạo ra một mô hình khung thực thể cho cơ sở dữ liệu và muốn đóng gói nó bằng cách sử dụng mẫu mặt tiền. Vì vậy, tôi đã tạo một lớp DTO có tên là Tài khoản. Nó lưu trữ dữ liệu tổng hợp từ ba bảng khác nhau. Đây là những gì Account.cs trông giống như:Luôn DRY với DTO's

public class Account 
{ 
    public int? Id { get; set; } 
    public string UserName { get; set; } 
    public string Password { get; set; } 
    public string GroupName { get; set; } 
    public string IpAddress { get; set; } 
    public string Route { get; set; } 
} 

Đây là phương pháp tôi tập hợp và trả về một Tài khoản-DTO.

Account Get(string userName) 
{ 
    // Get the values from the database. 
    var check = _entities.Checks.Single(x => x.UserName == userName); 
    var userGroup = _entities.UserGroups.Single(x => x.UserName == userName); 
    var ipReply = _entities.Replies.Single(x => x.UserName == userName && x.Attribute == "Framed-IP-Address"); 
    var routeReply = _entities.Replies.Single(x => x.UserName == userName && x.Attribute == "Framed-Route"); 

    // Populate the DTO 
    var account = new Account 
    { 
     UserName = check.UserName, 
     Password = check.Value, 
     GroupName = userGroup.GroupName 
    }; 

    if (ipReply != null) account.IpAddress = ipReply.Value; 
    if (routeReply != null) account.Route = routeReply.Value; 

    return account; 
} 

Và đây là phương pháp để cập nhật cơ sở dữ liệu bởi một người dùng gửi Account-DTO

void Update(Account account) 
{ 
    // Get the values from the database. Again. 
    var check = _entities.Checks.Single(x => x.UserName == account.UserName); 
    var userGroup = _entities.UserGroups.Single(x => x.UserName == account.UserName); 
    var ipReply = _entities.Replies.Single(x => x.UserName == account.UserName && x.Attribute == "Framed-IP-Address"); 
    var routeReply = _entities.Replies.Single(x => x.UserName == account.UserName && x.Attribute == "Framed-Route"); 

    // Update the possible attributes 
    check.Value = account.Password; 
    userGroup.GroupName = account.GroupName; 
    ipReply.Value = account.IpAddress; 
    routeReply.Value = account.Route; 

    _entities.SaveChanges(); 
} 

Như bạn thấy, tôi sử dụng mã chính xác cùng để lấy dữ liệu từ cơ sở dữ liệu. Làm thế nào tôi có thể DRY mã này lên?

+0

'Factory :: GetCheck (String UserName) ',' Factory :: GetUserGroup (String username) ', ...? –

+0

Làm thế nào sâu thông qua các lớp của bạn không DTO này đi du lịch? Liệu nó đi từ lớp giao diện người dùng tất cả các con đường xuống lớp DB? –

+0

Đó là một ứng dụng web MVC (mỗi khung nhìn có một mô hình xem) DTO được điền bởi một mô hình khung nhìn (trong trường hợp này, nó trông giống như Account.cs) và được chuyển đến mặt tiền (có chứa các phương thức trên). – Sandro

Trả lời

1

Tại sao không chỉ đơn giản là giải nén mã được chia sẻ đến lớp địa phương

class AcccountFieldsByName { 
// check, userGroup, ipReply, routeReply 

    static AcccountFieldsByName Read(... _entities, string userName)  
    { 
    return new AcccountFieldsByName { 
     check = _entities.Checks.Single(x => x.UserName == userName), 
     userGroup = _entities.UserGroups.Single(x => x.UserName == userName), 
     ipReply = _entities.Replies.Single(x => x.UserName == userName && x.Attribute == "Framed-IP-Address"), 
     routeReply = _entities.Replies.Single(x => x.UserName == userName && x.Attribute == "Framed-Route"), 
     } 
    } 
}