简体   繁体   English

实体框架和业务对象

[英]Entity Framework And Business Objects

I have never used the entity framework before and i would like to try some personal projects implementing it to get my feet wet.我以前从未使用过实体框架,我想尝试一些实现它的个人项目来让我的脚湿透。

I see that entities can be exposed to the presentation layer.我看到实体可以暴露给表示层。 But i don't want certain fields exposed, fields like modified dates and created dates and various other database fields.但我不希望暴露某些字段,如修改日期和创建日期以及各种其他数据库字段。

how could i implement Business objects and just expose the properties i need but still keep the objects serializable?我如何实现业务对象并只公开我需要的属性但仍保持对象可序列化?

Also what advantages does this have over LinqToSql?这与 LinqToSql 相比有什么优势?

When you define an entity in the EDMX model you can specify the visibility of each property's setter and getter, so if you don't want the ModifiedDate to be visible in other layers, you can simply specify it as internal.当您在 EDMX 模型中定义实体时,您可以指定每个属性的 setter 和 getter 的可见性,因此如果您不希望 ModifiedDate 在其他层中可见,您可以简单地将其指定为内部。

在此处输入图片说明

If your requirements are more complicated like the ModifiedDate should be accessible in the entities assembly and the business logic assembly but not in the UI assembly, then you need to create another object which will be exchanged between the business logic and the UI logic layers.如果您的要求更复杂,例如 ModifiedDate 应该可以在实体程序集和业务逻辑程序集中访问,但不能在 UI 程序集中访问,那么您需要创建另一个对象,该对象将在业务逻辑和 UI 逻辑层之间交换。

Personally use a wrapper class over entity and expose or shadow what I need.个人在实体上使用包装类并公开或隐藏我需要的内容。

// instead of below property in your BLL:

private int m_someVariable;

public int SomeVariable
{
    get { return m_someVariable; }
    set { m_someVariable = value; }
}

// You can use the entity object:

private readonly EntityClass _entityObject = new EntityClass();

public int SomeVariable
{
    get { return _entityObject.SomeVariable; }
    set { _entityObject.SomeVariable = value; }
}

// or make it read-only at your BLL

public int SomeVariable
{
    get { return entityObject.SomeVariable; }
    // set { entityObject.SomeVariable = value; }
}

您只需将所需的属性绑定到表示层,这可以通过声明、业务逻辑层(具有自己的对象抽象级别)或您的 ViewModel 来完成。

      // this is your edmx
        Asset5Entities conx = new Asset5Entities();

// consider this is a new object list of Contact that is a table in the database //using entity framework this database table is mapped to an object for u to handle // 认为这是一个新的 Contact 对象列表,它是数据库中的一个表 // 使用实体框架将此数据库表映射到一个对象以供您处理

            List$gt;Contact$lt; s = new List$gt;Contact$lt;();

//using a big of LINQ u can now select or query over any table in ur database and u have // access to the columns in that table example (Email) here // 使用大量 LINQ,您现在可以选择或查询您数据库中的任何表,并且您可以 // 在此处访问该表示例(电子邮件)中的列

        var result = from q in conx.Contacts select q.Email;

// rather than // 而不是

        string sqlcommand = "select email from Contacts";
        Contact con = new Contact();
        con.Email= "xxxx@gmail.com";
        con.FirstName="nader";

        //etc etc... 



        conx.Contacts.AddObject(con);

        //rather than   " insert into Contact values ......................"

        //having your queries within ur c# code rather than strings that are not parsed //for errors but on runtime was alot helpful for me

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM