WebServices and Value of Projections

Posts   
 
    
mshe
User
Posts: 167
Joined: 02-Feb-2006
# Posted on: 25-Oct-2007 09:01:35   

Hi all,

I'm creating DTOs in my webservices and projections seem to be the method recommended for transferring entities into the DTO.

However, I don't get what the value of a projection is - wouldn't it be easier (more straightforward) to maually assign the value of each field in the DTO?

What I'm doing now is creating DTOs with a constructor which accepts an entity, within the constructor I'm assigning the property fields i.e.:


Public Class CustomerDTO

    Private _CustomerID As Guid = Guid.Empty
    Private _Title As String
    Private _FirstName As String
    Private _LastName As String
    Private _Company As String
    Private _Gender As String
    Private _Email As String
    Private _PasswordHint As String
    Private _Password As String
    Private _Address1 As String
    Private _Address2 As String
    Private _City As String
    Private _State As String
    Private _Country As String
    Private _ZIP As String
    Private _Phone As String
    Private _Status As Integer
    Private _CreationTime As DateTime
    Private _ModifiedTime As DateTime
    Private _IPAddress As String

    Public Enum GenderEnum
        Male
        Female
        Unknown
    End Enum

    Public Enum StatusEnum
        Inactive = 0
        AwaitingConfirmation = 100
        Suspended = 200
        Active = 300
    End Enum

    Public Sub New()

    End Sub

    Public Sub New(ByVal Customer As CustomerEntity)
        With Customer
            _CustomerID = .CustomerId
            _Title = .Title
            _FirstName = .FirstName
            _LastName = .LastName
            _Company = .Company
            _Gender = .Gender
            _Email = .Email
            _Password = .Password
            _Address1 = .Address1
            _Address2 = .Address2
            _City = .City
            _State = .State
            _Country = .Country
            _ZIP = .Zip
            _Phone = .Phone
            _Status = .Status
            _CreationTime = .CreationTime
            _ModifiedTime = .ModifiedTime
            _IPAddress = .Ipaddress
        End With
    End Sub

    Public Property CustomerID() As Guid
        Get
            Return _CustomerID
        End Get
        Set(ByVal value As Guid)
            _CustomerID = value
        End Set
    End Property

    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property

    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

    Public Property Company() As String
        Get
            Return _Company
        End Get
        Set(ByVal value As String)
            _Company = value
        End Set
    End Property

    Public Property Gender() As GenderEnum
        Get
            Select Case _Gender.ToUpper
                Case "M"
                    Return GenderEnum.Male
                Case "F"
                    Return GenderEnum.Female
                Case Else
                    Return GenderEnum.Unknown
            End Select
            Return _Gender
        End Get
        Set(ByVal value As GenderEnum)
            Select Case value
                Case GenderEnum.Male
                    _Gender = "M"
                Case GenderEnum.Female
                    _Gender = "F"
                Case GenderEnum.Unknown
                    _Gender = "U"
            End Select
        End Set
    End Property

    Public Property Email() As String
        Get
            Return _Email
        End Get
        Set(ByVal value As String)
            _Email = value
        End Set
    End Property

    Public Property PasswordHint() As String
        Get
            Return _PasswordHint
        End Get
        Set(ByVal value As String)
            _PasswordHint = value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property

    Public Property Address1() As String
        Get
            Return _Address1
        End Get
        Set(ByVal value As String)
            _Address1 = value
        End Set
    End Property

    Public Property Address2() As String
        Get
            Return _Address2
        End Get
        Set(ByVal value As String)
            _Address2 = value
        End Set
    End Property

    Public Property City() As String
        Get
            Return _City
        End Get
        Set(ByVal value As String)
            _City = value
        End Set
    End Property

    Public Property State() As String
        Get
            Return _State
        End Get
        Set(ByVal value As String)
            _State = value
        End Set
    End Property

    Public Property Country() As String
        Get
            Return _Country
        End Get
        Set(ByVal value As String)
            _Country = value
        End Set
    End Property

    Public Property ZIP() As String
        Get
            Return _ZIP
        End Get
        Set(ByVal value As String)
            _ZIP = value
        End Set
    End Property

    Public Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
        End Set
    End Property

    Public Property Status() As StatusEnum
        Get
            Return _Status
        End Get
        Set(ByVal value As StatusEnum)
            _Status = value
        End Set
    End Property

    Public Property CreationTime() As DateTime
        Get
            Return _CreationTime
        End Get
        Set(ByVal value As DateTime)
            _CreationTime = value
        End Set
    End Property

    Public Property ModifiedTime() As DateTime
        Get
            Return _ModifiedTime
        End Get
        Set(ByVal value As DateTime)
            _ModifiedTime = value
        End Set
    End Property

    Public Property IPAddress() As String
        Get
            Return _IPAddress
        End Get
        Set(ByVal value As String)
            _IPAddress = value
        End Set
    End Property
End Class

In my fetch code I merely iterate through a CustomEntityCollection and output a list(of CustomerDTO).

Would projections help me in this case?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2007 11:34:15   

However, I don't get what the value of a projection is

What do you mean here? I don't understand the question

  • wouldn't it be easier (more straightforward) to maually assign the value of each field in the DTO?

It might be but projection will save you the effort of looping.

The following is an example from the docs:

.NET 2.0: projection to custom classes This code is .NET 2.0 or higher, due to the generics used in the DataProjectorToCustomClass projector engine. With some reflection, it is possible to create such a class for .NET 1.x, though the class itself has to be setup a little different. The code below also shows how to use the projectors in .NET 2.0. It uses the class TestCustomer which is given below the projection example code (in C#). The projection also shows how to project a property of an entity which isn't an entity field, namely IsDirty, using the EntityProperty class.

// C#, .NET 2.0 EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(new CustomerEntityFactory()); adapter.FetchEntityCollection(customers, null); EntityView2<CustomerEntity> allCustomersView = customers.DefaultView; List<TestCustomer> customCustomers = new List<TestCustomer>(); DataProjectorToCustomClass<TestCustomer> customClassProjector = new DataProjectorToCustomClass<TestCustomer>( customCustomers ); List<IEntityPropertyProjector> propertyProjectors = new List<IEntityPropertyProjector>(); propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.CustomerId, "CustomerID" ) ); propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.City, "City" ) ); propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.CompanyName, "CompanyName" ) ); propertyProjectors.Add( new EntityPropertyProjector( CustomerFields.Country, "Country" ) ); propertyProjectors.Add( new EntityPropertyProjector( new EntityProperty("IsDirty"), "IsDirty" ) ); // create the projection allCustomersView.CreateProjection( propertyProjectors, customClassProjector );

The custom class, TestCustomer:

/// /// Test class for projection of fetched entities onto custom classes using a custom projector. /// public class TestCustomer { #region Class Member Declarations private string _customerID, _companyName, _city, _country; private bool _isDirty; #endregion

public TestCustomer()
{
    _city = string.Empty;
    _companyName = string.Empty;
    _customerID = string.Empty;
    _country = string.Empty;
    _isDirty = false;
}

#region Class Property Declarations
public string CustomerID
{
    get { return _customerID; }
    set { _customerID = value; }
}

public string City
{
    get { return _city; }
    set { _city = value; }
}

public string CompanyName
{
    get { return _companyName; }
    set { _companyName = value; }
}

public string Country
{
    get { return _country; }
    set { _country = value; }
}

public bool IsDirty
{
    get { return _isDirty; }
    set { _isDirty = value; }
}

#endregion  

}