tags:

views:

140

answers:

2

Please review my code. For every my Entity i've created a service class, where i put all the access methods for this entity.This method are doing the transformation from the Entities to my DTO classes. This methods are called from the Web layer or a bussines method. Am I doing this righth? Or should I do it differently ?

The service method:

public static IEnumerable<OsobaDto> GetNakupyByOsoba(Guid guid)
    {
        using (FinanceEntities finance = new FinanceEntities())
        {

            var osoby = from o in finance.OsobaSet
                        where o.Nakupy.Any(n => n.idnakupu == guid)
                        select new OsobaDto
                                   {
                                       Id = o.idosoba,
                                       Meno = o.meno,
                                       Priezvisko = o.priezvisko,
                                       Prijem = o.prijem,
                                       Nakupy = o.Nakupy.Select(n => new NakupDto
                                                                         {
                                                                             IdNakupu = n.idnakupu,
                                                                             Cena = n.cena,
                                                                             Datum = n.datum
                                                                         })
                                   };



            return osoby;
        }
    }

And the DTO class

  public class NakupDto
{
    public Guid? IdNakupu
    {
        get; 
        set;
    }
    public Decimal Cena
    {
        get;
        set;
    }
    public DateTime Datum
    {
        get;
        set;
    }

    public IEnumerable<OsobaDto> Osoby
    {
        get;
        set;
    }

    public OsobaDto Platil
    {
        get;
        set;
    }
A: 

Keep in mind the query wont actually hit the database until you actually use the IEnumerable you are are returning (because of deferred execution). I'm not sure how that will work since you are disposing the FinanceEntities before you actually execute the query. Assuming that works, it looks fine to me.

NotDan
A: 

Everything is good, but I have one suggestion. If you have a lot of DTO objects you shoud think about writing a general converter. It can be done using reflection and explicit conversion operators.

Restuta