我们说我有Address
个对象:
public class Address_Model{
public int Id {get; set;}
public string Street{get; set;}
public string City {get; set;}
public string State {get; set;}
public string PostalCode{get; set;}
}
此属性用于其他几个对象(即office
对象,home
对象等):
public class Office_Model{
public int Id {get; set;}
public Address_Model Address{get; set;}
//. . . (other properties specific to the office model)
}
public class Home_Model{
public int Id {get; set;}
public Address_Model Address{get; set;}
//. . . (other properties specific to the home model)
}
这些模型存在于我的BLL中。在我的构造函数中,它使用来自我的DAL的数据,我当前手动构造Address
属性:
public Office_Model GetOffice(int id){
var office = DAL.SomeClass.GettOfficeById(id);
return new Office_Model
{
Id = office.Id,
Address = new Address_Model{
Id = office.Address.Id,
Street = office.Address.Street,
City = office.Address.City,
State = office.Address.State,
PostalCode = office.Address.PostalCode
}
}
}
public Home_Model GetHome(int id){
var home = DAL.SomeClass.GettHomeById(id);
return new Home_Model
{
Id = home.Id,
Address = new Address_Model{
Id = home.Address.Id,
Street = home.Address.Street,
City = home.Address.City,
State = home.Address.State,
PostalCode = home.Address.PostalCode
}
}
}
为了增加代码的可维护性(例如,不必将Street2
这样的新属性添加到构造Address_Model
的所有位置),我想创建一个构造地址的通用方法模型:
public Address_Model GetAddressModel(DAL.SomeClass.Address address){
return new Address_Model{
Id = address.Address.Id,
Street = address.Address.Street,
City = address.Address.City,
State = address.Address.State,
PostalCode = address.Address.PostalCode
}
}
现在我的BLL getter方法看起来更像是这样:
public Office_Model GetOffice(int id){
var office = DAL.SomeClass.GettOfficeById(id);
return new Office_Model
{
Id = office.Id,
Address = GetAddressModel(office.Address);
}
}
public Home_Model GetHome(int id){
var home = DAL.SomeClass.GettHomeById(id);
return new Home_Model
{
Id = home.Id,
Address = GetAddressModel(home.Address);
}
}
委派此施工作业是否会产生负面影响?我是性能分析的新手,刚刚了解了boxing
和unboxing
;我不确定这种性能下降是否适用于我在这里所做的事情。
显然,我在很大程度上简化了这个例子。实际上,还有许多需要构建的嵌套对象。
答案 0 :(得分:2)
Boxing
:
拳击是将值类型转换为类型对象或的过程 到此值类型实现的任何接口类型。
由于您的Address
对象是引用类型(标记为class
而不是struct
),并且您希望收到Address
类型作为参数,你的代码中不会发生拳击。更重要的是,JIT编译器可能决定内联您的方法调用,这将消除为该调用一起创建新的堆栈帧。