域建模 - 实现属性或POCO的接口?

Nei*_*l M 6 c# design-patterns repository

我正在构建一个工具,它将通过SOAP api将文件导入基于Web的应用程序,并模拟了我想通过C#接口导入的内容,这样我就可以将Web应用程序的模型数据包装在我可以处理的内容中.

public interface IBankAccount
{
    string AccountNumber { get; set; }
    ICurrency Currency { get; set; }
    IEntity Entity { get; set; }
    BankAccountType Type { get; set; }
}

internal class BankAccount
{
    private readonly SomeExternalImplementation bankAccount;

    BankAccount(SomeExternalImplementation bankAccount)
    {
        this.bankAccount = bankAccount;
    }

    // Property implementations
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个存储库,它返回IBankAccount或其他的集合以及工厂类,以便在需要时为我创建BankAccounts.

我的问题是,这种方法会给我带来很大的痛苦,创建POCO会更好吗?我想将所有这些放在一个单独的程序集中,并完全分离数据访问和业务逻辑,这只是因为我在这里处理一个关于数据在线存储位置的移动目标.

Mat*_*vey 5

这正是我使用的方法,而且我从未遇到过任何问题。在我的设计中,来自数据访问层的任何内容都被抽象为接口(我将它们称为数据传输契约)。在我的域模型中,我使用静态方法从这些数据传输对象创建业务实体。

interface IFooData
{
    int FooId { get; set; }
}

public class FooEntity
{
    static public FooEntity FromDataTransport(IFooData data)
    {
        return new FooEntity(data.FooId, ...);
    }
}
Run Code Online (Sandbox Code Playgroud)

当您的领域模型实体从多个数据契约收集数据时,它非常方便:

public class CompositeEntity
{
    static public CompositeEntity FromDataTransport(IFooData fooData, IBarData barData)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

与您的设计相反,我不提供工厂来创建数据传输契约的具体实现,而是提供委托来写入值并让存储库担心创建具体对象

public class FooDataRepository
{
    public IFooData Insert(Action<IFooData> insertSequence)
    {
        var record = new ConcreteFoo();

        insertSequence.Invoke(record as IFooData);

        this.DataContext.Foos.InsertOnSubmit(record); // Assuming LinqSql in this case..

        return record as IFooData;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

IFooData newFoo = FooRepository.Insert(f =>
    {
        f.Name = "New Foo";
    });
Run Code Online (Sandbox Code Playgroud)

尽管在我看来工厂实现是一个同样优雅的解决方案。为了回答你的问题,根据我对非常类似方法的经验,我从未遇到过任何重大问题,我认为你在这里走在正确的轨道上:)