如何将映射添加到私有支持字段 [英] How to add a mapping to a private backing field

查看:76
本文介绍了如何将映射添加到私有支持字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的域模型中使用私有支持字段。这是因为我想通过控制一些业务规则的添加和删除操作来限制对公共集合的访问。



公共类帐户

{



私人IList<客户> _customers = new List< Customer>();



public IEnumerable< Customer>客户

{

得到{return _customers; }
}



$
public void AddCustomer(客户客户)

{



&NBSP;&NBSP;&NBSP;&NBSP; //执行一些BIZ规则

&NBSP;&NBSP;&NBSP;&NBSP; _customers.Add(客户)

}

}



$


以上是可能的使用功能NHibernate的映射构建体:



的hasMany(X => x.Customers).Access.CamelCaseField(Prefix.Underscore)//这会寻找名为_customers的支持字段





如何在EF4.0中实现相同的功能?例如,配置关系是什么?





公共类AccountConfiguration:EntityConfiguration< Account> <登记/>
&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;公共AccountConfiguration()

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; HasKey(X => x.Id);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;性能(X => x.Id).IsIdentity();&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;




             //关系??? &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;




        }

     ; }





$
使用公共ICollection<客户>客户完全破坏优秀的OO设计原则,因为客户可以通过调用ICollection上的添加操作直接访问。



$


NHibernate似乎完美地处理了这种常见情况,非常希望看到EF支持这种情况。







由于

解决方案

Th3Fix3r您好,


EF4目前不支持字段级映射,但这是我们正在强烈考虑实施的东西。


然而,您可以做的一件事是在对象中使用私有属性。但是,使用Code First配置私有属性有些问题。目前,最好的方法是使用"嵌套配置类"。图案。即您的Account类中的Nest AccountConfiguration
,以便您可以在配置表达式中引用私有属性。您可以在分部类中声明此嵌套类,以在域对象和映射配置类之间实现某种程度的分离。


干杯,


安德鲁。


I would like to use private backing fields in my domain models. The reason for this is because I want to limit access to the public collection by controlling the adding and removing operations with some business rules.

public class Account
{

private IList<Customer> _customers = new List<Customer>();

public IEnumerable<Customer> Customers
{
get { return _customers ; }
}


public void AddCustomer(Customer customer)
{

    //Perform some biz rules
    _customers.Add(customer)
}
}



The above is possible using fluent nhibernate's mapping construct:

HasMany(x => x.Customers).Access.CamelCaseField(Prefix.Underscore) //This would look for backing field called _customers


How can i achieve the same function in EF4.0? For example what would the configuration Relationship be?



public class AccountConfiguration : EntityConfiguration<Account>
    {
        public AccountConfiguration()
        {
            HasKey(x => x.Id);
            Property(x => x.Id).IsIdentity();        

            //Relationship ???        

        }
    }



Using a public ICollection<Customer> Customers completely destroys good OO design principals as clients could directly access by calling the Add Operation on ICollection.



NHibernate seems to handle this common scenario perfectly and would very much like to see this supported in the EF.



Thanks

解决方案

Hi Th3Fix3r,

EF4 does not currently support field-level mapping but it is something we're strongly considering implementing going forward.

One thing you can do however is to use private properties in your objects. Configuring private properties using Code First is somewhat problematic though. Currently, the best way to do it is to use the "nested configuration class" pattern. i.e. Nest AccountConfiguration inside your Account class so you can reference the private properties in your configuration expressions. You can declare this nested class in a partial class to achieve some level of separation between your domain objects and the mapping configuration classes.

Cheers,

Andrew.


这篇关于如何将映射添加到私有支持字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆