在域类约束字符串的长度 [英] Constraining string length in domain classes

查看:98
本文介绍了在域类约束字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用抽象库加载域对象持久性无知域模型。
中的具体实现我的存储库(数据访问层(DAL))使用实体框架从SQL Server数据库获取数据。
数据库有很多的VARCHAR列的长度的限制。
现在,假设我有以下领域类:

I have a persistence ignorant domain model that uses abstract repositories to load domain objects. The concrete implementation of my repositories (the data access layer (DAL)) uses entity framework to fetch data from a sql server database. The database has length constraints on a lot of its varchar columns. Now imagine that I have the following domain class:

public class Case
{
    public Case(int id, string text)
    {
         this.Id = id;
         this.Text = text;
    }

    public int Id { get; private set; }
    public string Text { get; set; }
}

和定义为抽象库如下:

public abstract class CaseRepository
{
    public abstract void CreateCase(Case item);
    public abstract Case GetCaseById(int id);
}



[文] 在SQLSERVER的表列定义为为nvarchar(100)

The [text] column of the table in sqlserver is defined as nvarchar(100)

现在我知道,我提到了我域类(案例)为持续性无知,但我觉得这是不对的,它允许
值文本不能最终由我具体的仓库实现保存,因为分配文本属性所产生的实体框架时,实体框架
将抛出一个异常参数当它是类超过100个字符。
所以我决定,我要检查的域模型这一限制,因为这可以让我检查数据的有效性试图
之前,把它传递给DAL,从而使错误报告更中心,以域对象。我猜你可能会说,我可以查一下我的构造函数和属性setter的
约束,但因为我有几百个类,所有有类似的限制,我想一个
更通用的方式来解决问题

Now I know that I mentioned that my domain class (Case) was persistence ignorant, nevertheless I feel that it is wrong that it allows for values of the text parameter that cannot ultimately be saved by my concrete repository implementation because the entity framework will throw an exception when assigning the text property to the entity framework generated class when it is longer than 100 characters. So I have decided that I wish to check this constraint in the domain model, because this allows me to check data validity before attempting to pass it on to the DAL, and thus making error reporting more centric to the domain object. I guess you could argue that I could just check the constraint in my constructor and in the property setter, but since I have hundreds of classes that all have similar constraints I wanted a more generic way to solve the problem

现在,我已经拿出的是一类称为 ConstrainedString ,定义如下:

Now, the thing that I've come up with is a class called ConstrainedString, defined as follows:

public abstract class ConstrainedString
{
    private string textValue;

    public ConstrainedString(uint maxLength, string textValue)
    {
        if (textValue == null) throw new ArgumentNullException("textValue");
        if (textValue.Length > maxLength) 
            throw new ArgumentException("textValue may not be longer than maxLength", "textValue");

        this.textValue = textValue;
        this.MaxLength = maxLength;
    }

    public uint MaxLength { get; private set; }

    public string Value 
    { 
        get 
        {
            return this.textValue;
        } 

        set 
        {
            if (value == null)
                throw new ArgumentNullException("value");
            if (value.Length > this.MaxLength) throw new ArgumentException("value cannot be longer than MaxLength", "value");
            this.textValue = value;
        } 
    }
}



此外,我有一个实现 ConstrainedString 名为 String100

public class String100 : ConstrainedString
{
    public String100(string textValue) : base(100, textValue) { }
}

这样就导致了不同的实施案例的,将是这样的:

Thus leading to a different implementation of Case that would look like this:

public class Case
{
    public Case(int id, String100 text)
    {
         this.Id = id;
         this.Text = text;
    }

    public int Id { get; private set; }
    public String100 Text { get; set; }
}

现在,我的问题是;我俯瞰一些内置类或其他一些方法,我可以改用?或者这是一个合理的方法?

Now, my question is; Am I overlooking some built-in classes or some other approach that I could use instead? Or is this a reasonable approach?

任何意见和建议者居多。

Any comments and suggestions are most welcome.

感谢您提前

推荐答案

我相信您的验证应该驻留在你的领域模型。在你的领域的限制,直接代表了一些业务逻辑。最终你必须坚持你反正之前验证。

I believe your validation should reside in your domain model. The constraints on your fields directly represent some business logic. Ultimately you have to validate before you persist anyway.

这篇关于在域类约束字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆