Archive for category CSLA

Migrate project from CSLA 3.7 to CSLA 4.0 (how to)

We’ve recently managed to migrate all our product lines from CSLA 3.7 to CSLA 4.0 and here are problem/solutions scenarios.
  • Problem:
    Class contains backend field, but is not registered
    If you don’t do it, exception Csla.PropertyLoadException will be raised with message
  • Property load or set failed for property MyField  (Properties with private backing fields must be marked as  RelationshipTypes.PrivateField)
Solution:
Register backend field:
private int _my_field;
private static readonly PropertyInfo<int> MyFieldProperty = RegisterProperty<int>(o => o.MyField, "My field", RelationshipTypes.PrivateField);
public int MyField
{
       get { return GetProperty(MyFieldProperty, _my_field); }
       set { SetProperty(MyFieldProperty, ref _my_field, value); }
}
Problem:
Method LoadProperty was used like this:
<p>LoadProperty(MyFieldProperty, 10);</p>
exception Csla.PropertyLoadException will be raised with message “Csla.PropertyLoadException: Property load or set failed for property MyField (Attempt to read/load private field property in managed properties.) —> System.InvalidOperationException: Attempt to read/load private field property in managed properties”
In this case, you have to initialize backend field :
_my_field = 10;
  • Problem :
Class contains readonly field ,
private readonly int _my_field;
and you try to set the value using binding, runtime exception raised with message:
“Expression must be writeable Parameter name: left”“Expression must be writeable Parameter name: left”
Solution:
Solution:

Make field static

private static readonly int _my_field;
  • Problem:
Class is inherited from base class :

[Serializable]
public class MyBaseClass<T> : BusinessBase<T> where T : MyBaseClass<T>
{
}

[Serializable]
public class MyConcreteClass : MyBaseClass<MyConcreteClass>
{
   protected static PropertyInfo<string> ConcretePropProperty = RegisterProperty(typeof(MyConcreteClass), new PropertyInfo<string>("ConcretePropProperty"));
   public string ConcreteProp
   {
      get { return GetProperty(ConcretePropProperty); }
      set { SetProperty(ConcretePropProperty, value); }
    }
}

and if properties in MyBaseClass are not registered exception raised with message:

The type initializer for ‘MyConcreteClass’ threw an exception.System.TypeInitializationException: The type initializer for ‘ MyConcreteClass’ threw an exception. —> System.InvalidOperationException: Cannot register property ConcretePropr after containing type (MyBaseClass) has been instantiated
Solution:
You have to register every property in concrete class.
  • Problem:

    Class is inherited from BusinessListBase or BusinessBindingListBase. Adding new items in collection cannot be done.

Solution:
All items that will be added to collection must be created like child.
Did you have issues and would you like to share with community ?

Tags: , , ,

CSLA .NET 3.8.2 released

 

Rockford Lhotka announces update to his CSLA application Framework.

This is a bug-fix release, and has been in beta for several months.

Can be downloaded here.

Tags: ,

CSLA – Factory methods (part II)

 

This is continued from my first CSLA tutorial post (introduction)

In this post we will see some of the basic steps to create new CSLA object using factory methods.

Factory methods are method that we use to create new instance of the type.

If this method have same name like type (class), that method is called constructor.

In CSLA framework, default constructors are declared as private, so the users of the class

(usually developer) is "forced" to call factory methods to make new instance.

We can create new object using different criteria, in some scenarios we need to create new instance using simple types like string, int etc…, and in some we have to use complex type like Company, Salary etc… For that purpose, we can use SingleCriteria class or make our custom criteria class.

So, let’s see some different scenarios :

 

  1. 1. In our first example we will create declare type Employee with default create method.

 

#region using.statements
using Csla;
using System;
#endregion
namespace CslaTutorial.FactoryMethods
{
   [Serializable]
   public class Employee : BusinessBase<Employee>
  {
       #region Factory Methods
       public static Employee CreateEmployee()
       {
           return DataPortal.Fetch<Employee>();
       }

       protected override void DataPortal_Create()
      {
         // Add some initialization here.
      }
       private Employee() { }
       #endregion
   }
}

2. Factory method with use of criteria (SingleCriteria).
I prefer to use this criteria class when I don’t have 2 criteria with same type.
(ie. CreateEmployee(int id), CreateEmployee(int internalIdentificationNumber).

   #region using.statements
    using Csla;
    using System;
   #endregion

   namespace CslaTutorial.FactoryMethods
   {
      [Serializable]
     public class Employee : BusinessBase<Employee>
    {
            #region Factory Methods
            // Create Employee with SingleCritera (csla class).
           public static Employee CreateEmployee(Company company)
          {
              return DataPortal.Create<Employee>(new SingleCriteria<Employee, Company>(company));
           }

           private void DataPortal_Create(SingleCriteria<Employee, Company> criteria)
           {
              // this method will be called when CreateEmployee(Company company) is called.
            }

            private Employee() { }
           #endregion
     }
}

3. Factory method with custom criteria class EmployeeIdNumberCriteria.

#region using.statements

using Csla;
using System;
#endregion

namespace CslaTutorial.FactoryMethods
{
    [Serializable]
    public class Employee : BusinessBase<Employee>
    {

        #region Factory Methods

        // Create Employee with custom criteria.
        public static Employee CreateEmployeeWithCustomCriteria(string employIdNumber)
        {
            return DataPortal.Create<Employee>(new EmployeeIdNumberCriteria(employIdNumber));
        }

        private void DataPortal_Create(EmployeeIdNumberCriteria criteria)
        {
            // this method will be called by CSLA when method CreateEmployeeWithCustomCriteria(string employIdNumber) is called.
        }

       [Serializable]
        private class EmployeeIdNumberCriteria
        {
            public string EmployIdNumber { get; private set; }

            public EmployeeIdNumberCriteria(string employIdNumber)
            {
                EmployIdNumber = employIdNumber;
            }
        }

        private Employee()
        {   }

        #endregion

    }
}

So, those are basics steps to declare and define factory methods which will be called by CSLA framework.

Tags:

CSLA Introduction

 

This is first post about CSLA like I was announced in my post here, that I will give some basics about CSLA

Well, instead of writing application I decide that best introduction is given by his author Rocky Lhotka at DNRTV shows.

Here I will provide links to the shows so you can see from the first hand
(links are given in order that is recommended to watch):

    1. CSLA.NET 2.0 Part 1 of Many
    2. CSLA.NET 2.0 Part 2 of Many
    3. CSLA.NET and ASP.NET
    4. CSLA.NET in Web Services
    5. CSLA.NET with a Data Access Layer
    6. CSLA .NET 3.5 Part 1
    7. CSLA .NET 3.5 Part 2
    8. MVVM Pattern in CSLA .NET 3.8

Podcast shows can be listened at next links
(links are given in order that is recommended to listen):

    1. Rocky Lhotka discusses CSLA.NET
    2. CSLA.NET 2.0 with Rocky Lhotka
    3. CSLA with Rockford Lhotka
    4. Rocky Lhotka on CSLA.NET 3.5
    5. Rocky Lhotka on CSLA.NET for Silverlight!
    6. Catching up with Rocky Lhotka
    7. Rocky Lhotka on Data Access Mania, LINQ and CSLA.NET

See you in next post with more on CSLA.

Tags: ,

CSLA tutorial series

Because I’m using CSLA on daily basis, I decide to "spread the word" and introduce with this very useful framework.

This series will contain following parts:

  1. Introduction to CSLA
  2. Factory methods
  3. Data portal
  4. Validation and business rules
  5. Authentication and Authorization
  6. Parent-Child relationships

See you @ introduction :)

Tags: ,