相关疑难解决方法(0)

使实体类关闭以进行更改

我有一个数据库关系,如下所示.域对象是基于LINQ to SQL ORM创建的.

付款包括现金付款和礼品券付款.假设购买总额为550.可以按以下组件支付

1 Gift Coupon Valued 300

1 Gift Coupon Valued 200

I Cash Currency Valued 50
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我正在使用ORM的"InsertOnSubmit"功能插入新的付款记录.以下代码工作正常.但是,如果我公司使用信用卡引入新的支付组件,我需要更改我的"付款"域类.如何使支付类打开以进行扩展并关闭仍在使用ORM的更改

注意:Payment类具有行为(例如GetTotalAmountCollected).我正在努力使"付款"类满足OCP.

注意:优惠券类型有特定的行为.优惠券发行日期是否小于2000年1月1日,它不应用于计算总金额(即,CouponValue应为零).请参阅使用策略模式重构代码.

注意:我使用的是.Net 4.0

参考:

  1. 将ObjectContext.AddObject与Entity Framework一起使用时出错
  2. 使用策略模式重构代码
  3. 喜欢构成而不是继承?
  4. 代码优先与模型/数据库优先
  5. 使用Unity的策略模式和依赖注入
  6. 委托与OOP的C#策略设计模式
  7. 如何在C#中使用策略模式?
  8. 继承与EF代码第一:第2部分-每个类型表(TPT) http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first -ctp5部分-2-表每类型tpt.aspx

C#代码:

public class PaymentAppService
{
    public RepositoryLayer.ILijosPaymentRepository Repository { get; set; }

    public void MakePayment()
    {
        DBML_Project.Payment paymentEntity = new DBML_Project.Payment();
        paymentEntity.PaymentID = 1;
        paymentEntity.PaymentType = "PurchaseP";

        DBML_Project.CashPayment cashObj = new …
Run Code Online (Sandbox Code Playgroud)

.net c# domain-driven-design entity-framework linq-to-sql

7
推荐指数
1
解决办法
851
查看次数

视图模型最佳实践中的ASP.NET MVC自定义验证

我正在尝试将域驱动设计与测试驱动开发的组合用于我在ASP.NET MVC 3中构建的此应用程序.我的架构设置了存储库,域模型,视图模型,控制器和视图.所有验证都将在视图模型中处理.我将我的视图模型设置为继承自"IValidatableObject",以便我的验证属性和我在"Validate"方法中设置的自定义验证都在我的控制器方法调用"ModelState.IsValid"时执行.我遇到的问题是在我的视图模型的Validate方法中访问我的存储库.我需要访问存储库以检查数据库中的重复记录.似乎最好的想法是创建一个IRepository类型的属性,并通过将我的存储库注入视图模型的构造函数来设置该属性.例如:

public class UserViewModel : IValidatableObject
{
       public UserViewModel(User user, IUserRepository userRepository)
       {
              FirstName = user.FirstName;
              LastName = user.LastName;
              UserRepository = userRepository;
              UserName = user.UserName;
       }
       public string UserName { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public IUserRepository UserRepository { get; set; }
       public IEnumerable<ValidationResult> Validate()
       {
           UserCriteria criteria = new UserCriteria { UserName = this.UserName };
           IList<User> users = UserRepository.SearchUsers(criteria);

           if (users != null && users.count() …
Run Code Online (Sandbox Code Playgroud)

validation asp.net-mvc viewmodel

5
推荐指数
1
解决办法
3689
查看次数

我应该在DDD域项目中进行验证吗?

我想使用FluentValidation验证我的域模型实体。我已经阅读了有关DDD验证的答案,该答案已使用FluentValidation验证其实体。这是他如何验证其实体的方法:

public class ParticipantValidator : AbstractValidator<Participant>
{
    public ParticipantValidator(DateTime today, int ageLimit, List<string> validCompanyCodes, /*any other stuff you need*/)
    {...}

public void BuildRules()
{
         RuleFor(participant => participant.DateOfBirth)
                .NotNull()
                .LessThan(m_today.AddYears(m_ageLimit*-1))
                .WithMessage(string.Format("Participant must be older than {0} years of age.", m_ageLimit));

        RuleFor(participant => participant.Address)
            .NotNull()
            .SetValidator(new AddressValidator());

        RuleFor(participant => participant.Email)
            .NotEmpty()
            .EmailAddress();
        ...
}

}
Run Code Online (Sandbox Code Playgroud)

因此,我的域项目依赖FluentValidation库。

但是我认为我的域项目依赖第三方库是一个不好的主意。我如何预防这个问题?

c# validation design-patterns domain-driven-design fluentvalidation

5
推荐指数
1
解决办法
1410
查看次数