CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

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

rated by 0 users
Not Answered This post has 0 verified answers | 4 Replies | 2 Followers

Top 150 Contributor
56 Posts
dg78 posted on Fri, Feb 17 2012 7:15 AM

Hello,

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

Dominique

All Replies

Top 10 Contributor
9,270 Posts

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.

Rocky

Top 150 Contributor
56 Posts
dg78 replied on Fri, Feb 17 2012 5:18 PM

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

Top 150 Contributor
56 Posts
dg78 replied on Sat, Feb 18 2012 2:41 AM

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 ?

Top 10 Contributor
9,270 Posts

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.

Rocky

Page 1 of 1 (5 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems