实体框架和业务对象

发布于 2024-10-20 10:31:41 字数 173 浏览 2 评论 0原文

我以前从未使用过实体框架,我想尝试一些实现它的个人项目来熟悉一下。

我看到实体可以暴露给表示层。 但我不希望公开某些字段,例如修改日期和创建日期等字段以及各种其他数据库字段。

我如何实现业务对象并仅公开我需要的属性,但仍保持对象可序列化?

与 LinqToSql 相比,它有什么优势?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无法言说的痛 2024-10-27 10:31:41

当您在 EDMX 模型中定义实体时,您可以指定每个属性的 setter 和 getter 的可见性,因此如果您不希望 ModifiedDate 在其他层中可见,只需将其指定为内部即可。

在此处输入图像描述

如果您的要求更复杂,例如 ModifiedDate 应该可以在实体程序集和业务逻辑程序集中访问,但是不在 UI 程序集中,那么您需要创建另一个对象,该对象将在业务逻辑层和 UI 逻辑层之间进行交换。

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.

enter image description here

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.

与之呼应 2024-10-27 10:31:41

个人在实体上使用包装类并公开或隐藏我需要的内容。

// 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; }
}

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; }
}
我乃一代侩神 2024-10-27 10:31:41

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

You only bind the properties you want to the presentation layer, this can be done through declaration, a Business Logic layer (with it's own level of object abstraction) or your ViewModel.

你没皮卡萌 2024-10-27 10:31:41
      // this is your edmx
        Asset5Entities conx = new Asset5Entities();

// 认为这是一个新的联系人对象列表,它是数据库中的一个表
//使用实体框架,这个数据库表被映射到一个对象供你处理

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

//使用大量的LINQ,你现在可以选择或查询数据库中的任何表,并且你有
// 在这里访问该表示例(电子邮件)中的列

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

// 而不是

        string sqlcommand = "select email from Contacts";
        Contact con = new Contact();
        con.Email= "[email protected]";
        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
      // 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

            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

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

// rather than

        string sqlcommand = "select email from Contacts";
        Contact con = new Contact();
        con.Email= "[email protected]";
        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文