使用Entity Framework在C#中编码合同不变违规

时间:2014-12-25 21:25:41

标签: c# entity-framework asp.net-mvc-5 code-contracts object-oriented-analysis

我是EF和代码合同的初学者,因为我正在使用EF6和代码合同做项目。

正如您在DB第一种方法中所知,EF自动生成与数据库实体相对应的类,我将合同添加到我手动定义的部分类中。

EF生成的课程:

public partial class Article
{
    public Article()
    {
        this.Comment = new HashSet<Comment>();
    }

    public int articleId { get; set; }
    public string title { get; set; }
    public string contentText { get; set; }
    public System.DateTime creationDate { get; set; }
    public System.DateTime modificationDate { get; set; }
    public int numOfVisits { get; set; }
    public bool enabled { get; set; }
    public int creatorId { get; set; }
    public int categoryId { get; set; }
    public string img { get; set; }

    public virtual ICollection<Comment> Comment { get; set; }
    public virtual Category Category { get; set; }
    public virtual Administrator Administrator { get; set; }
}

和手动定义的类:

public partial class Article
{
    [ContractInvariantMethod]
    private void ObjectInvariant()
    {
        Contract.Invariant(contentText != null);
    }

    public Article(string title, string img, int catid, string content)
    { 
        Contract.Ensures(this.title.Equals(title));
        Contract.Ensures(this.img.Equals(img));
        Contract.Ensures(categoryId.Equals(catid));
        Contract.Ensures(Enumerable.SequenceEqual(Contract.OldValue(content), content));
        this.title = title;
        this.img = img;
        categoryId = catid;
        contentText = content;
        creationDate = DateTime.Now;
        modificationDate = DateTime.Now;
        numOfVisits = 0;
        enabled = true;
        DBConnection.addObject<Article>(this);
    }

    public Article editArticle(int id,string title, string img, int catid, string content)
    {
        using (var c = new SabaDataEntity())
        {
            var art = c.Article.FirstOrDefault(i => i.articleId == id);

            if(art != null)
            {
                art.title = title;
                art.img = img;
                art.categoryId = catid;
                art.contentText = content;
            }

            c.SaveChanges();
            return art;
        }
    }

    public Article deleteById(int id)
    {
        using (var c = new SabaDataEntity())
        {
            var obj = c.Article.FirstOrDefault(i => i.articleId == id);

            if (obj != null)
            {
                c.Article.Remove(obj);
                c.SaveChanges();
                return obj;
            }

            return obj;
        }
    }

    public  List<Article> getAll()
    {
        using (var c = new SabaDataEntity())
        {
            return c.Article.ToList();
        }
    }

    public string getCategoryName()
    {
        using (var c = new SabaDataEntity())
        {
            var cat = c.Category.FirstOrDefault(i => i.categoryId == this.categoryId);

            if (cat != null)
                return cat.persianTitle;

            return "";
        }
    }

    public Article get(int id)
    {
        using (var c = new SabaDataEntity())
        {
           return c.Article.FirstOrDefault(i => i.articleId == id);
        }
    }
}

当我在项目中调用此类的方法时发生的问题违反了不变量,实际上是这个错误:

  

不变失败:contentText!= null

现在,是否有两个构造函数的可用性?

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以创建另一个用于数据访问的类(使用EF)作为存储库,并避免此类合同错误; e.g:

public class GenericRepository<T>: IDisposable  where T : class
{
    private SabaDataEntity db = null;
    private DbSet<T> table = null;
    static readonly GenericRepository<T> instance = new GenericRepository<T>();
    public static readonly GenericRepository<T> Instance
    {
        get
        {
            return instance;
        }
    }

    public GenericRepository()
    {
        this.db = new SabaDataEntity();
        table = db.Set<T>();
    }

    public List<T> getAll()
    {
        return table.ToList();
    }

    public T getById(int id)
    {
        return table.Find(id);
    }
}

来自here