使用 POCO 时,行为逻辑应该放在哪里?

发布于 2024-10-15 01:18:36 字数 224 浏览 5 评论 0原文

我从未使用过 POCO,因此我习惯将大量逻辑放入业务对象类中。因此,我相信我错过了一些关于类布局的重要概念,以及这里所需的思考过程。

因此,我希望有一些想法可以将我的大脑引向正确的方向;

假设你有两节课;公司和员工。您能否举一些例子,说明您将“围绕”这些类构建哪些类来处理各种行为/验证等? (例如一些类名称及其用途的简短描述)

(或者我想任何其他示例也同样有用。)

I never used POCOs, so I have the habit of putting a lot of logic in my business object classes. Hence I believe I'm missing some important concepts about class-layouts, and the thought-process that is needed here.

So I would appreciate some ideas to point my brain in the right direction;

Say if you have two classes; Company and Employee. Could you give some examples of what classes you would build "around" these that take care of various behavior/validation etc.? (Like some class names, and a brief description of their purpose)

(Or any other examples would be just as useful I guess.)

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

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

发布评论

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

评论(2

音盲 2024-10-22 01:18:36

您可以通过为该业务类创建元数据类来放置验证和业务逻辑
像这样

[MetadataType(typeof(EmployeeMetadata))]
    public partial class Employee
    {
        public class EmployeeMetadata
        {
            [Required(ErrorMessage="Employee Name is Required")]
            [StringLength(50, ErrorMessage="Must be under 30 Characters")]
            public string Name { get; set; }
         }
    }

这些类被称为“好友类
请参阅

you can put validation and business logic by creating a metadata class for that business class
like this

[MetadataType(typeof(EmployeeMetadata))]
    public partial class Employee
    {
        public class EmployeeMetadata
        {
            [Required(ErrorMessage="Employee Name is Required")]
            [StringLength(50, ErrorMessage="Must be under 30 Characters")]
            public string Name { get; set; }
         }
    }

these classes are called 'buddy classes'
Refer this

望喜 2024-10-22 01:18:36

我通常为系统中的任何逻辑代码分组创建一个外观层。例如,假设用户拥有管理员凭据,则只需处理公司数据和员工数据。因此,在这种情况下,我将创建一个 AdminFacade 类,如下所示:

public class AdminFacade 
{  
   public Company GetCompanyByEmployee( Employee employee )
   {
        // open a db session / web service call etc.
        // run a query to find the company by employe id.
        // populate the company object 
        // close db session
        // return the populated customer object
   } 
}

我通常让 GUI 实例化所有 Facade 类,这就是 GUI 处理的全部内容。您还可以在 GetCompanyByEmployee 方法的开头添加验证,并在假设 employee.startdate 小于一年时抛出异常。

希望有帮助。

I usually create a facade layer for any logical code groupings in my system. For instance let's say a user will only need to deal with company data and employee data if the user has Admin Credentials. So in that case i would create an AdminFacade class as follows:

public class AdminFacade 
{  
   public Company GetCompanyByEmployee( Employee employee )
   {
        // open a db session / web service call etc.
        // run a query to find the company by employe id.
        // populate the company object 
        // close db session
        // return the populated customer object
   } 
}

I usually have my GUI instantiate all the Facade classes and that is all what GUI deals with. You can also add validation at the start of GetCompanyByEmployee method and throw an exception if lets say employee.startdate is less than a year.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文