How to convert a Business Base to a Read Only Base ?

How to convert a Business Base to a Read Only Base ?

Old forum URL:

dg78 posted on Friday, February 17, 2012


I have a Business Base object and I need to have the same in a Read Only Base but without to fetch data.

I have the two classes BB and ROB. I want to keep them but I need to convert a BB object to a ROB object. 

How to do that ?

Thanks in advance


RockfordLhotka replied on Friday, February 17, 2012

Write a method in the read-only class something like this:

public static YourReadOnlyType CreateFromBB(YourEditableType obj)
  this.Id = obj.Id;
  this.Name = obj.Name;
  // etc

Then you can easily get a read-only object based on the editable object without going to the server or database.

dg78 replied on Friday, February 17, 2012

Thanks, Rocky, for your answer.

Yes I could use inside your method all properties I have in the DataPortal_Fetch of the read-only class but there is a problem because it is not possible to refer instance member inside a shared method.

I did (sorry I use VB) :

  Public Shared Function CreateFromBB(ByVal pTravEntete As TravEntete) As TravEnteteInfo

    _Id = pTravEntete.Id

    Return Me

  End Function


With DataPortal, the factory method (static or shared method) call DataPortal.Fech that call code in Csla which create an instance object on which we work in DataPortal_Fetch. It is not the case with only a static method. So there is an error on _Id = pTravEntete.Id

dg78 replied on Saturday, February 18, 2012

It is OK if I use a Public constructor in the read-only class.

  Public Sub New(ByVal pTravEntete As TravEntete)

    _Id = pTravEntete.Id

    _DateTrav = pTravEntete.DateTrav


  End Sub

Is it the good solution ?

RockfordLhotka replied on Saturday, February 18, 2012

That's the problem with using a browser as a code editor - no checking for validity :)

You don't need a public constructor, the private one is fine. Just use the Shared method like I suggested, but create the object:

Public Shared Function GetReadOnlyTrav(obj As TravEntete)
  Dim result = new ReadOnlyTrav
  result.Id = obj.Id
End Function

This works because the shared method is inside the read-only class, so it has access to all the private members of the read-only class - including the private constructor and the private setters.

Copyright (c) Marimer LLC