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. 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.
Recent Comments