简体   繁体   English

设计模式-胖适配器

[英]Design Pattern - Fat adapter

We've implemented the adapter design pattern whose job is the following: 我们已经实现了适配器设计模式,其工作如下:

  1. Act as a liaison between service and data access layers. 充当服务和数据访问层之间的联络人。
  2. Convert raw data (from data source, internal or external) to domain specific data. 将原始数据(从内部或外部数据源)转换为特定于域的数据。 Do necessary validation and massaging. 做必要的验证和按摩。
  3. Sometimes, making the DAO calls may depend on data not readily available from input parameters or additional service calls may need to be made based on input data. 有时,进行DAO调用可能取决于输入参数中不易获得的数据,或者可能需要基于输入数据进行其他服务调用。 In other words, the adapter can't always do a 1:1 mapping between the service and the DAO. 换句话说,适配器不能总是在服务和DAO之间进行1:1映射。 It may map the same call from service to different DAO calls based on the input parameters. 它可以基于输入参数将来自服务的同一呼叫映射到不同的DAO呼叫。

Item #3 is starting to worry me as the adapters are becoming more complicated than I'd originally imagined. 随着适配器变得比我最初想象的要复杂,第3项开始让我担心。 I'm not aware of a design pattern to trim down an adapter. 我不知道用于精简适配器的设计模式。 Is there one? 有一个吗? Suggestions? 有什么建议吗?

You've used what I like to call the "swiss army knife" pattern. 您使用了我喜欢的“瑞士军刀”模式。

Best practice says you should break up your class into at least 3 classes, one for each concern. 最佳做法是,您应该将您的课程至少分为3个班级,每个问题一个班级。

Instead of using an adapter or a full Repository(CRUD operations), i would use an IReader interface for reading and visitor pattern for insert update delete, so you can separate domain logic from infraestructure(persistance) details, Here is the idea: 而不是使用适配器或完整的存储库(CRUD操作),我将使用IReader接口进行读取,并使用访问者模式进行插入更新删除,因此您可以将域逻辑与基础结构(持久性)详细信息分开,这是这样的想法:

public class MyBusinessObject : IAcceptBusinessVisitor, IAcceptMyBusinessIdVisitor
{
    private readonly string _id;

    private string MyPrivateProp { get; set; }
    //Fully encapsulated object
    public MyBusinessObject(string id, string myPrivateProp)
    {
        _id = id;
        MyPrivateProp = myPrivateProp;
    }

    public void UpdateMyProp(string newProp)
    {            
        if (string.IsNullOrWhiteSpace(newProp)) throw new ArgumentNullException(nameof(newProp));
        //Business rules ...
        MyPrivateProp = newProp;
    }

    public void Accept(IMyBusinessObjectVisitor visitor)
    {
        if (visitor == null) throw new ArgumentNullException(nameof(visitor));
        visitor.Visit(_id, MyPrivateProp);
    }

    public void Accept(IMyBusinessIdVisitor visitor)
    {
        if (visitor == null) throw new ArgumentNullException(nameof(visitor));
        visitor.Visit(_id);
    }
}

public interface IAcceptBusinessVisitor
{
    void Accept(IMyBusinessObjectVisitor visitor);
}

public interface IAcceptMyBusinessIdVisitor
{
    void Accept(IMyBusinessIdVisitor visitor);
}

public interface IMyBusinessObjectVisitor
{
    void Visit(string id, string prop);
}

public interface IMyBusinessIdVisitor
{
    void Visit(string id);
}

public class SavePersistanceVitor : IMyBusinessObjectVisitor
{
    public void Visit(string id, string prop)
    {
        //Save to Database
    }
}

public class UpdatePersistanceVitor : IMyBusinessObjectVisitor
{
    public void Visit(string id, string prop)
    {
        //Update to Database
    }
}

public class DeleteVitor : IMyBusinessIdVisitor
{
    public void Visit(string id)
    {
        //Delete in Database
    }
}

Here for Reading: 在这里阅读:

public interface IMyBusinessObjectReader
{
    MyBusinessObject Read(string id);
}

class MyBusinessObjectReaderFromDb : IMyBusinessObjectReader
{
    public MyBusinessObject Read(string id)
    {
        //Read from database
        string myPrivateProp = "";
        return new MyBusinessObject(id, myPrivateProp);
    }
}

the next step could be adding generics for reading and the visitors. 下一步可能是添加阅读和访问者的泛型。 In this case you end up having little tiny classes and gain flexibility and the benefits of solid principles like single responsability, interface segregation, etc. So you can create a rich encapsulated domain and extend its functionality with some desing principles. 在这种情况下,您最终只剩下很少的类,并且获得了灵活性以及诸如单一职责,接口隔离等坚实原则的好处。因此,您可以创建一个丰富的封装域并使用某些设计原则来扩展其功能。 Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这是什么设计模式? 适配器? - What design pattern is this? Adapter? Java-Adapter设计模式实现的变体 - Java- Adapter design pattern implmentation variations 如何在 Java 中实现 Pluggable Adapter 设计模式 - How to implement Pluggable Adapter design pattern in Java 适配器设计模式 - 方法调用或构造函数中的依赖项? - Adapter design pattern - dependencies in method calls or constructors? 适配器设计模式| 适配器,将迭代器转换为枚举| ConcurrentModificationException - Adapter Design Pattern | adapter that converts an Iterator to an Enumeration |ConcurrentModificationException java中特定集合的迭代器的实现是适配器设计模式的一个例子吗? - Is implementation of Iterator for a specific collection in java is an example of Adapter design pattern? 类型转换可以成为适配器设计模式的示例吗? - type-casting can be an example of adapter design pattern? 适配器设计模式-角色之间的关系类型 - Adapter Design Pattern- Relationship Types Between Roles 我们是否在每次扩展类并实现其接口方法时都使用适配器设计模式? - Are we using the adapter design pattern everytime we extend a class and implements its interfaces methods? 为什么 Class 适配器设计模式不能使用一个接口代替多个 Inheritance? - Why the Class Adapter Design Pattern Can't Use an Interface Instead of Multiple Inheritance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM