English 中文(简体)
执行N-Tier实体框架 4.0 和DTO
原标题:implement N-Tier Entity Framework 4.0 with DTOs

I m目前正在建立一个网络系统,并试图在SOA架构中实施N-Tier实体框架4.0和DTOs。 我对我如何实施数据获取、商业逻辑和列报方式有问题的理解。

让我们相信,我有一个“用户账户”实体,其内容如下:

Id

FirstName

尼亚美

审计领域——InsertDate

审计领域——最新情况

在“数字”中,我设立了“UserAccountsData.cs”类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrderSystemDAL
{
  public static class UserAccountsData
  {
    public static int Insert(string firstName, string lastName, DateTime insertDate)
    {
      using (OrderSystemEntities db = new OrderSystemEntities())
      {
        return Insert(db, firstName, lastName, insertDate);
      }
    }

    public static int Insert(OrderSystemEntities db, string firstName,
                 string lastName, DateTime insertDate)
    {
      return db.UserAccounts_Insert(firstName, lastName, insertDate, insertDate).ElementAt(0).Value;
    }

    public static void Update(int id, string firstName, string lastName,
                 DateTime updateDate)
    {
      using (OrderSystemEntities db = new OrderSystemEntities())
      {
        Update(db, id, firstName, lastName, updateDate);
      }
    }

    public static void Update(OrderSystemEntities db, int id, string firstName,
                 string lastName, DateTime updateDate)
    {
      db.UserAccounts_Update(id, firstName, lastName, updateDate);
    }

    public static void Delete(int id)
    {
      using (OrderSystemEntities db = new OrderSystemEntities())
      {
        Delete(db, id);
      }
    }

    public static void Delete(OrderSystemEntities db, int id)
    {
      db.UserAccounts_Delete(id);
    }

    public static UserAccount SelectById(int id)
    {
      using (OrderSystemEntities db = new OrderSystemEntities())
      {
        return SelectById(db, id);
      }
    }

    public static UserAccount SelectById(OrderSystemEntities db, int id)
    {
      return db.UserAccounts_SelectById(id).ElementAtOrDefault(0);
    }

    public static List<UserAccount> SelectAll()
    {
      using (OrderSystemEntities db = new OrderSystemEntities())
      {
        return SelectAll(db);
      }
    }

    public static List<UserAccount> SelectAll(OrderSystemEntities db)
    {
      return db.UserAccounts_SelectAll().ToList();
    }
  }
}

在《刑法》中,我设立了“UserAccountEO.cs”类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using OrderSystemDAL;

namespace OrderSystemBLL
{
  public class UserAccountEO
  {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string 尼亚美 { get; set; }
    public DateTime InsertDate { get; set; }
    public DateTime UpdateDate { get; set; }

    public string FullName
    {
      get
      {
        return 尼亚美 + ", " + FirstName;
      }
    }
    public bool Save(ref ArrayList validationErrors)
    {
      ValidateSave(ref validationErrors);

      if (validationErrors.Count == 0)
      {
        if (Id == 0)
        {
          Id = UserAccountsData.Insert(FirstName, 尼亚美, DateTime.Now);
        }
        else
        {
          UserAccountsData.Update(Id, FirstName, 尼亚美, DateTime.Now);
        }
        return true;
      }
      else
      {
        return false;
      }
    }

    private void ValidateSave(ref ArrayList validationErrors)
    {
      if (FirstName.Trim() == "")
      {
        validationErrors.Add("The First Name is required.");
      }

      if (尼亚美.Trim() == "")
      {
        validationErrors.Add("The Last Name is required.");
      }
    }
    public void Delete(ref ArrayList validationErrors)
    {
      ValidateDelete(ref validationErrors);

      if (validationErrors.Count == 0)
      {
        UserAccountsData.Delete(Id);
      }
    }

    private void ValidateDelete(ref ArrayList validationErrors)
    {
      //Check for referential integrity.
    }
    public bool Select(int id)
    {
      UserAccount userAccount = UserAccountsData.SelectById(id);

      if (userAccount != null)
      {
        MapData(userAccount);
        return true;
      }
      else
      {
        return false;
      }
    }

    internal void MapData(UserAccount userAccount)
    {
      Id = userAccount.Id;
      FirstName = userAccount.FristName;
      尼亚美 = userAccount.尼亚美;
      InsertDate = userAccount.审计领域——InsertDate;
      UpdateDate = userAccount.审计领域——最新情况;
    }
    public static List<UserAccountEO> SelectAll()
    {
      List<UserAccountEO> userAccounts = new List<UserAccountEO>();

      List<UserAccount> userAccountDTOs = UserAccountsData.SelectAll();

      foreach (UserAccount userAccountDTO in userAccountDTOs)
      {
        UserAccountEO userAccountEO = new UserAccountEO();
        userAccountEO.MapData(userAccountDTO);
        userAccounts.Add(userAccountEO);
      }

      return userAccounts;
    }
  }
}

在《残疾人权利公约》中,我设立了以下网页:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrderSystemBLL;
using System.Collections;

namespace OrderSystemUI
{
  public partial class Users : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        LoadUserDropDownList();
      }
    }
    private void LoadUserDropDownList()
    {
      ddlUsers.DataSource = UserAccountEO.SelectAll();

      ddlUsers.DataTextField = "FullName";
      ddlUsers.DataValueField = "Id";
      ddlUsers.DataBind();
    }

  }
}

采用EF4在一层建筑中采用多功能模型的正确方法吗?

I would appreciate your help Thanks.

问题回答

DTOs只应拥有财产,而不是用户AccountEO。 你们应当将《国际热带木材组织法》与将实体绘制到国际交易日志的逻辑分开。

其它一切似乎都是正确的。

BTW:是从实体框架EDMX档案中产生DTO的一个工具,可以帮助你节省发展时间和精力。





相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签