2012-04-23 18 views
6

Obecnie tworzę interfejs internetowy dla FreeRADIUS. To tylko mała aplikacja, aby uprościć mutacje dla leniwych współpracowników Shell i SQL. Stworzyłem model Entity Framework dla bazy danych i chcę go enkapsulować za pomocą wzoru fasady. Stworzyłem więc klasę DTO o nazwie Konto. Przechowuje dane zagregowane z trzech różnych tabel. Oto jak wygląda konto Account.cs:Bądź suchy dzięki DTO

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; } 
} 

Jest to metoda, w której składam i zwracam pojedyncze konto-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; 
} 

I jest to metoda do aktualizacji bazy danych przez użytkownika składać 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(); 
} 

Jak widać, używam dokładnie ten sam kod, aby pobrać dane z bazy danych. Jak mogę DRY ten kod?

+0

'Fabryka :: GetCheck (String nazwa_użytkownika)', 'Fabryka :: GetUserGroup (String nazwa_użytkownika)', ...? –

+0

Jak głęboko przez twoje warstwy robi to DTO podróżować? Czy przechodzi od warstwy interfejsu użytkownika do warstwy DB? –

+0

Jest to aplikacja internetowa MVC (każdy widok ma model widoku) DTO jest zapełniany przez taki model widoku (w tym przypadku wygląda tak jak Account.cs) i jest przenoszony na elewację (która zawiera powyższe metody). – Sandro

Odpowiedz

1

Dlaczego nie po prostu wyodrębnić kod współdzielony do lokalnej klasy

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"), 
     } 
    } 
}