c# - 实体管理器 : 3 Tier/Layer Application Question

标签 c# architecture three-tier

我正在尝试创建一个简单的三层项目,并且正在按计划实现业务逻辑层,现在,这就是我的 BL 的样子

//The Entity/BO
public Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public UserAccount UserAccount { get; set; }
    public List<Subscription> Subscriptions { get; set; }
 }

//The BO Manager
public class CustomerManager
{

     private CustomerDAL _dal = new CustomerDAL();

    private Customer _customer;

    public void Load(int customerID)
    {
        _customer = GetCustomerByID(customerID);
    }


    public Customer GetCustomer()
    {
        return _customer;
    }


    public Customer GetCustomerByID(int customerID)
    {

        return _dal.GetCustomerByID(customerID);
    }


    public Customer GetCustomer()
    {
        return _customer;
    }

    public UserAccount GetUsersAccount()
    {

        return _dal.GetUsersAccount(_customer.customerID);
    }

    public List<Subscription> GetSubscriptions()
    {
         // I load the subscriptions in the contained customer object, is this ok??
        _customer.Subscriptions = _customer.Subscriptions ?? _dal.GetCustomerSubscriptions(_customer.CustomerID);

        return _customer.Subscriptions;
    }

您可能会注意到,我的对象管理器实际上只是我的真实对象 (Customer) 的容器,这就是我放置业务逻辑的地方,这是一种将业务实体与业务逻辑解耦的方式,这就是我的方式通常使用它

        int customerID1 = 1;
        int customerID2 = 2;

        customerManager.Load(1);

        //get customer1 object
        var customer = customerManager.GetCustomer();

        //get customer1 subscriptions
        var customerSubscriptions = customerManager.GetSubscriptions();

        //or this way
        //customerManager.GetSubscriptions();
        //var customerSubscriptions = customer.Subscriptions;


        customerManager.Load(2);

        //get customer2
        var newCustomer = customerManager.GetCustomer();

        //get customer2 subscriptions
        var customerSubscriptions = customerManager.GetSubscriptions();

如您所见,它一次只包含 1 个对象,如果我需要管理客户列表,我可能必须创建另一个管理器,如 CustomerListManager

我的问题是,这是实现三层/层设计 BL 的正确方法吗? 或者关于如何实现它的任何建议。谢谢。

最佳答案

正如其他人之前提到的,您应该看看 Repository 模式。我还建议检查工作单元模式以及领域驱动设计,以了解您应用程序的总体架构。

您还可以查看 .NET 的对象关系映射 (ORM) 框架,例如 Entity FrameworkNHibernate如果这是您的选择。

为了让您抢先一步,这里有一些重要的资源可以帮助您走上正确的道路:

书籍

在线引用资料

希望对您有所帮助。

关于c# - 实体管理器 : 3 Tier/Layer Application Question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318462/

相关文章:

c# - 基于 bool 值取反数值的优雅方式

c# - 使用c#读取Excel文件中的行

c# - WPF : How do I make a editable path

docker - 如何容器化依赖数据库的服务?

.net - 为什么不在大型项目中使用 EF 生成的类?

c# - 数字列表总和的linq列表

architecture - 可以使用 Keycloak 作为用户数据库吗?

c# - 使用 WCF 的三层架构

asp.net-mvc - DataAnnotations 或在服务中手动验证?