c# - 基于类型变量实例化不同存储库的策略模式

标签 c# asp.net-mvc-3 entity-framework-4 ef-code-first wiki

我正在从事一个 wiki 项目。 MVC3 + EF4 代码优先。

我模型中的所有类都继承自主“EditBase”类。这确保它们都具有 ID、用户名和创建日期。它还帮助我为基本 CRUD 创建一个通用存储库。

我希望定义一个名为 AbuseReport 的新实体来处理不正确/不准确内容的标记:

public class AbuseReport : EditBase
{
    public DateTime DateReported { get; set; }
    public string EntityType { get; set; }
    public int EntityId { get; set; }
    public int ReportTypeId { get; set; }
    public ReportType ReportType 
    {
        get { return (ReportType) this.ReportTypeId; }
        set { this.ReportTypeId = (int) value; }
    }
    public string Description { get; set; }
}

从 AbuseReport 对象的信息开始检索存储在数据库中的实体的最简单方法是什么?我不能使用外键,因为报告可以引用项目中的任何实体。

我需要这样调用:

Type modelType;
entityDictionary.TryGetValue(entityName, out modelType);
var rep = new Repository<modelType>();

最佳答案

我会将所有模型类型存储在同一个存储库中。存储库 key 必须包含模型的类型及其 ID。为此,我使用本地类 RepositoryKey:

public class EditBase
{
    public int ID { get; set; }
    public string Username { get; set; }
    public DateTime CreatedDate { get; set; }
}

public class Repository
{
    private Dictionary<RepositoryKey, EditBase> _store = new Dictionary<RepositoryKey, EditBase>();

    public void Add(EditBase model)
    {
        _store.Add(new RepositoryKey(model), model);
    }

    public void Remove(EditBase model)
    {
        _store.Remove(new RepositoryKey(model));
    }

    public void Remove<T>(int id)
        where T : EditBase
    {
        _store.Remove(new RepositoryKey(typeof(T), id));
    }

    public bool TryGet<T>(int id, out T value)
        where T : EditBase
    {
        EditBase editBase;
        if (!_store.TryGetValue(new RepositoryKey(typeof(T), id), out editBase)) {
            value = null;
            return false;
        }
        value = (T)editBase;
        return true;
    }

    private class RepositoryKey : IEquatable<RepositoryKey>
    {
        private Type _modelType;
        private int _id;

        public RepositoryKey(EditBase model)
        {
            _modelType = model.GetType();
            _id = model.ID;
        }

        public RepositoryKey(Type modelType, int id)
        {
            _modelType = modelType;
            _id = id;
        }

        #region IEquatable<IdentityKey> Members

        public bool Equals(RepositoryKey other)
        {
            if (_modelType != other._modelType) {
                return false;
            }
            return _id == other._id;
        }

        #endregion

        public override int GetHashCode()
        {
            return _modelType.GetHashCode() ^ _id.GetHashCode();
        }
    }
}

关于c# - 基于类型变量实例化不同存储库的策略模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8313935/

相关文章:

c# - HttpListener 忽略请求中#(哈希)之后的所有内容

c# - 在 global.asax 中需要 UrlHelper

c# - 如何在不同的 PARENT 文件夹内正确路由 mvc Controller ?

c# - 您如何在 Web 应用程序中集中 Entity Framework 数据上下文?

c# - Entity Framework 虚拟属性

c# - 重命名文件时重命名 XAML 文件类的更好方法?

c# - C# 属性的最具创新性的用法

c# - 在 C# 中从 List<string> 构建字符串

c# - 为 Orchard CMS 构建一个简单的交互式 FizzBu​​zz 模块?

entity-framework-4 - Entity Framework :在哪里扩展CSDL/MSL?