CSLA
BusinessObject
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# region File Information
//**
//* Type : Source Code
//* Module : SqlDataReaderWrapper
//* Description : Use to solve the problem on closing connection, the DB connection will be closed when it is disposed.
//* Creator : William
//* Date : Release 1
# endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTTracker.Model;
using Csla;
using Csla.Data;
using Csla.Security;
using PTTracker.LinqDAL;
namespace PTTracker.BusinessObject
{
[Serializable()]
publicclass AddressBook : BusinessBase<AddressBook>
{
#region Business Methods
//国家名
privatestatic PropertyInfo<string> CountryProperty = RegisterProperty<string>(p => p.CountryName, "Country Name");
publicstring CountryName
{
get { return GetProperty(CountryProperty); }
set { SetProperty(CountryProperty, value); }
}
//省名
privatestatic PropertyInfo<string> ProvinceProperty = RegisterProperty<string>(p => p.ProvinceName, "Province Name");
publicstring ProvinceName
{
get { return GetProperty(ProvinceProperty); }
set { SetProperty(ProvinceProperty, value); }
}
//城市名
privatestatic PropertyInfo<string> CityProperty = RegisterProperty<string>(p => p.CityName, "Province Name");
publicstring CityName
{
get { return GetProperty(CityProperty); }
set { SetProperty(CityProperty, value); }
}
#endregion
#region Factory Methods
//取得信息
publicstatic AddressBook GetAdddress(int id)
{
return DataPortal.Fetch<AddressBook>(new SingleCriteria<AddressBook, int>(id));
}
//初始化业务对象
publicstatic AddressBook NewAddress()
{
return DataPortal.Create<AddressBook>();
}
#endregion
#region Business Rules(业务和验证规则的标准实现)
protectedoverridevoid AddBusinessRules()
{
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,
new Csla.Validation.RuleArgs(ProvinceProperty));
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs(ProvinceProperty, 100));
ValidationRules.AddRule<AddressBook>(TestBusinessRules, ProvinceName);
ValidationRules.AddRule<AddressBook>(TestBusinessRules, CityName);
}
privatestaticbool TestBusinessRules<T>(
T target, Csla.Validation.RuleArgs e) where T : AddressBook
{
if (target.ReadProperty(ProvinceProperty) != target.ReadProperty(CityProperty))
{
e.Description ="Hello World!";
returnfalse;
}
else
{
returntrue;
}
}
#endregion
#region Authorization Rules(授权规则)
//属性级别的授权规则
protectedoverridevoid AddAuthorizationRules()
{
//AuthorizationRules.AllowWrite(CityProperty, "ProjectManager");
}
//对象级别的授权规则
protectedstaticvoid AddObjectAuthorizationRules()
{
//string UserGroups = "Softtek GDC Wuxi|STK|Interwise|Softtek GDC China|";
//AuthorizationRules.AllowGet(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowEdit(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowDelete(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowDelete(typeof(AddressBook), "Administrator");
AuthorizationRules.AllowCreate(typeof(AddressBook), "1");
}
#endregion
#region Data Access
//抽取数据
privatevoid DataPortal_Fetch(SingleCriteria<AddressBook, int> criteria)
{
List<Address> data =new AddressDal().GetAddressList(criteria.Value);
this.CountryName = data[0].Country;
this.CityName = data[0].City;
this.ProvinceName = data[0].Province;
//调用业务和验证规则的标准实现
//ValidationRules.CheckRules();
}
//更新
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Update()
{
new AddressDal().UpdateAddress(this);
// 更新所有的子对象
FieldManager.UpdateChildren(this);
}
//插入
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Insert()
{
//base.DataPortal_Insert();
}
//手动删除
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Delete(object criteria)
{
//base.DataPortal_Delete(criteria);
}
//自动删除
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_DeleteSelf()
{
//base.DataPortal_DeleteSelf();
}
//创建业务对象
[RunLocal()]
protectedoverridevoid DataPortal_Create()
{
using (BypassPropertyChecks)
{
CountryName ="China";
//LoadProperty(CountryProperty, "China");
ValidationRules.CheckRules();
}
}
#endregion
}
}
//**
//* Type : Source Code
//* Module : SqlDataReaderWrapper
//* Description : Use to solve the problem on closing connection, the DB connection will be closed when it is disposed.
//* Creator : William
//* Date : Release 1
# endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTTracker.Model;
using Csla;
using Csla.Data;
using Csla.Security;
using PTTracker.LinqDAL;
namespace PTTracker.BusinessObject
{
[Serializable()]
publicclass AddressBook : BusinessBase<AddressBook>
{
#region Business Methods
//国家名
privatestatic PropertyInfo<string> CountryProperty = RegisterProperty<string>(p => p.CountryName, "Country Name");
publicstring CountryName
{
get { return GetProperty(CountryProperty); }
set { SetProperty(CountryProperty, value); }
}
//省名
privatestatic PropertyInfo<string> ProvinceProperty = RegisterProperty<string>(p => p.ProvinceName, "Province Name");
publicstring ProvinceName
{
get { return GetProperty(ProvinceProperty); }
set { SetProperty(ProvinceProperty, value); }
}
//城市名
privatestatic PropertyInfo<string> CityProperty = RegisterProperty<string>(p => p.CityName, "Province Name");
publicstring CityName
{
get { return GetProperty(CityProperty); }
set { SetProperty(CityProperty, value); }
}
#endregion
#region Factory Methods
//取得信息
publicstatic AddressBook GetAdddress(int id)
{
return DataPortal.Fetch<AddressBook>(new SingleCriteria<AddressBook, int>(id));
}
//初始化业务对象
publicstatic AddressBook NewAddress()
{
return DataPortal.Create<AddressBook>();
}
#endregion
#region Business Rules(业务和验证规则的标准实现)
protectedoverridevoid AddBusinessRules()
{
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,
new Csla.Validation.RuleArgs(ProvinceProperty));
ValidationRules.AddRule(
Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs(ProvinceProperty, 100));
ValidationRules.AddRule<AddressBook>(TestBusinessRules, ProvinceName);
ValidationRules.AddRule<AddressBook>(TestBusinessRules, CityName);
}
privatestaticbool TestBusinessRules<T>(
T target, Csla.Validation.RuleArgs e) where T : AddressBook
{
if (target.ReadProperty(ProvinceProperty) != target.ReadProperty(CityProperty))
{
e.Description ="Hello World!";
returnfalse;
}
else
{
returntrue;
}
}
#endregion
#region Authorization Rules(授权规则)
//属性级别的授权规则
protectedoverridevoid AddAuthorizationRules()
{
//AuthorizationRules.AllowWrite(CityProperty, "ProjectManager");
}
//对象级别的授权规则
protectedstaticvoid AddObjectAuthorizationRules()
{
//string UserGroups = "Softtek GDC Wuxi|STK|Interwise|Softtek GDC China|";
//AuthorizationRules.AllowGet(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowEdit(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowDelete(typeof(AddressBook), "ProjectManager");
//AuthorizationRules.AllowDelete(typeof(AddressBook), "Administrator");
AuthorizationRules.AllowCreate(typeof(AddressBook), "1");
}
#endregion
#region Data Access
//抽取数据
privatevoid DataPortal_Fetch(SingleCriteria<AddressBook, int> criteria)
{
List<Address> data =new AddressDal().GetAddressList(criteria.Value);
this.CountryName = data[0].Country;
this.CityName = data[0].City;
this.ProvinceName = data[0].Province;
//调用业务和验证规则的标准实现
//ValidationRules.CheckRules();
}
//更新
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Update()
{
new AddressDal().UpdateAddress(this);
// 更新所有的子对象
FieldManager.UpdateChildren(this);
}
//插入
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Insert()
{
//base.DataPortal_Insert();
}
//手动删除
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_Delete(object criteria)
{
//base.DataPortal_Delete(criteria);
}
//自动删除
[Transactional(TransactionalTypes.TransactionScope)]
protectedoverridevoid DataPortal_DeleteSelf()
{
//base.DataPortal_DeleteSelf();
}
//创建业务对象
[RunLocal()]
protectedoverridevoid DataPortal_Create()
{
using (BypassPropertyChecks)
{
CountryName ="China";
//LoadProperty(CountryProperty, "China");
ValidationRules.CheckRules();
}
}
#endregion
}
}
Be the change you want to see in the world.