1

我目前在一个解决方案中有以下内容:

  1. 核心项目(数据访问、商业逻辑、数据访问的 petapoco、管道等)
  2. 模型项目(仅用于属性的模型和 petapoco 装饰)
  3. Web 项目(用于演示的 MVC 项目

我想让我的模型和核心分开,但我不能在两个地方都有 PetaPoco.cs。我将如何将它分开并且仍然能够使用 PetaPoco 属性装饰我的模型项目中的 POCO?

我不希望 Models 项目依赖于 Core 项目。

我确实创建了这个单独的类,只在 Models 项目中,所以我可以装饰 POCO,但是 Core PetaPoco 项目没有正确获取这些属性。它过于依赖 PocoData。

建议?

// Poco's marked [Explicit] require all column properties to be marked
[AttributeUsage(AttributeTargets.Class)]
public class ExplicitColumnsAttribute : Attribute
{
}
// For non-explicit pocos, causes a property to be ignored
[AttributeUsage(AttributeTargets.Property)]
public class IgnoreAttribute : Attribute
{
}

// For explicit pocos, marks property as a column and optionally supplies column name
[AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute : Attribute
{
    public ColumnAttribute() { }
    public ColumnAttribute(string name) { Name = name; }
    public string Name { get; set; }
}

// For explicit pocos, marks property as a result column and optionally supplies column name
[AttributeUsage(AttributeTargets.Property)]
public class ResultColumnAttribute : ColumnAttribute
{
    public ResultColumnAttribute() { }
    public ResultColumnAttribute(string name) : base(name) { }
}

// Specify the table name of a poco
[AttributeUsage(AttributeTargets.Class)]
public class TableNameAttribute : Attribute
{
    public TableNameAttribute(string tableName)
    {
        Value = tableName;
    }
    public string Value { get; private set; }
}

// Specific the primary key of a poco class (and optional sequence name for Oracle)
[AttributeUsage(AttributeTargets.Class)]
public class PrimaryKeyAttribute : Attribute
{
    public PrimaryKeyAttribute(string primaryKey)
    {
        Value = primaryKey;
        autoIncrement = true;
    }

    public string Value { get; private set; }
    public string sequenceName { get; set; }
    public bool autoIncrement { get; set; }
}

[AttributeUsage(AttributeTargets.Property)]
public class AutoJoinAttribute : Attribute
{
    public AutoJoinAttribute() { }
}
4

1 回答 1

0

不过,我认为一个明显的解决方案(我不确定它是否更好)是将 PetaPoco 移动到它自己的项目中,然后在您的 Core 和 Models 项目中引用它。但是,您的模型仍然具有外部依赖关系,而不是整个核心程序集。

另一种选择是将您的装饰模型放在您的核心项目中供内部使用,然后在您的模型程序集中有一组未装饰的类。您可以使用自动映射组件轻松地在两者之间进行映射。所以基本上你会使用 PetaPoco 将数据提取到你的内部模型中,然后将其映射到你的“外部”模型,它只是没有依赖关系的裸类。

当然,这听起来像是很多额外的工作。我想这一切都取决于您的模型程序集没有其他依赖项的重要性。

于 2012-03-24T10:23:48.177 回答