Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用一种中间模型_C#_.net_Hierarchy - Fatal编程技术网

C# 使用一种中间模型

C# 使用一种中间模型,c#,.net,hierarchy,C#,.net,Hierarchy,我正在编写一个程序,在不同的流媒体服务之间同步播放列表,问题是每个服务使用不同的结构和功能。 我想让它“模块化”,这样我就可以添加新的服务并与其他服务同步,而无需为我已经在应用程序中使用的每一项服务编程,我想到的最好办法是使用一种中间语言(或中间模型)来实现它,例如 通过这种方式,我实现的每个服务都接受作为其功能参数的中间模型,并输出自己的可转换模型 Service1Album album = service1.GetAllAlbums()[0]; IntermediateAlbum inter

我正在编写一个程序,在不同的流媒体服务之间同步播放列表,问题是每个服务使用不同的结构和功能。 我想让它“模块化”,这样我就可以添加新的服务并与其他服务同步,而无需为我已经在应用程序中使用的每一项服务编程,我想到的最好办法是使用一种中间语言(或中间模型)来实现它,例如

通过这种方式,我实现的每个服务都接受作为其功能参数的中间模型,并输出自己的可转换模型

Service1Album album = service1.GetAllAlbums()[0];
IntermediateAlbum intermediateAlbum = album.ToIntermediate();
service2.AddAlbum(intermediateAlbum);
service3.AddAlbum(intermediateAlbum);
有什么方法可以让我更优雅地实现这一点吗?如果是这样的话,有没有一种方法可以从像ServiceContainer这样的东西继承每一个服务,像这样抽象每一个服务

var sList = new List<ServiceContainer>{};
sList.Add(new Service1());
sList.Add(new Service2());
foreach (var service in sList)
{
    service.addAlbum(new IntermediateAlbum()
                          {
                             //properties
                          });
}
var sList=新列表{};
添加(新服务1());
添加(新服务2());
foreach(sList中的var服务)
{
service.addAlbum(新的IntermediateAlbum()
{
//性质
});
}

卢卡-欢迎!您正在发现代码中需要一些设计模式。这真是太棒了——这意味着事情变得足够复杂,你可以开始依赖其他人花了数年时间在构建你的解决方案时改进的模式

#1-切换到使用接口+设计模式 如果我是你,我会从将所有东西抽象到接口开始(例如,你会根据IAlbum和IArtist而不是具体的实现在每个服务中运行你的工作)

然后,一旦接口就位,请查看.net代码以了解如何布局工作

#2-基础 有很多代码需要编写,但是骨架可能最终看起来像这样

namespace MusicService
{
    public interface IAlbum {
        //common album properties/methods
    }
    public interface IArtist {
        //common artist properties/methods
    }
    internal abstract class AlbumBase:IAlbum {
        // implement common functions and properties if you have them...
        // otherwise, skip this
    }
    internal abstract class ArtistBase:IAlbum {
        // implement common functions and properties if you have them...
        // otherwise, skip this
    }   
}

namespace MusicService.AppleMusic 
{
    internal class Album:AlbumBase 
    {  //OR Album:IAlbum if you skipped that AlbumBase thing
        //code apple music specific stuff here ... 
        // think of this as a "mask" that the apple album is 
        // wearing so that it can pretend to be an iAlbum
    }
    internal class Artist:ArtistBase 
    {  
        //same comments apply here
    }   
}
namespace MusicService.Spotify 
{
    internal class Album:AlbumBase 
    {  
        //GO and do liekwise with Spotify
    }
    internal class Artist:ArtistBase 
    {  
        //GO and do liekwise with Spotify
    }
}
#3-实现实际同步 现在,您已经拥有了各种音乐服务所需的代码(排列整齐,这样您的程序员同事就不必通过您的大脑来了解您的想法),您可以为您的SyncService编写代码

我不想破坏这种乐趣(同步服务可以使用,也可以是一种——不确定同步代码需要做什么),但最终的同步代码可以很简单:

var myService = new SyncService();
myService.AddAppleMusic();
myService.AddSpotify();
myService.Sync();
破坏乐趣 嗯。好的这是我要做的

namespace MusicService {
    public interface IService {
        //propably a List<IAlbum> and List<IA> somewhere in here
        void Sync();
    }

    public class SyncService {
        internal List<IService> _services;

        public void AddService(IService musicService) {
            if(_services==null){_services=new List<IService>();}
            _services.Add(musicService);
        }

        public void Sync() {
            foreach(IService ms in _services) {
                ms.Sync();
            }
        }
    }
}

namespace MusicService.AppleMusic {
    internal AppleSyncService:IService {
        public AppleSyncService() {
            //Do your apple-specific initializations here
        }
        public void Sync() {
            //apple-sync
        }
    }

    internal class ExtendService(){
        public static void AddAppleMusic(this SyncService syncAgent) {
           syncAgent.AddService(new AppleSyncService());
        }            
    }
}
名称空间音乐服务{
公共接口设备{
//可能是一个列表,在这里的某个地方
void Sync();
}
公共类同步服务{
内部列表服务;
公共音乐服务(IService musicService){
如果(_services==null){_services=new List();}
_服务。添加(音乐服务);
}
公共void Sync(){
foreach(IService ms in_services){
Sync女士();
}
}
}
}
命名空间MusicService.AppleMusic{
内部AppleSyncService:iSeries设备{
公共应用程序同步服务(){
//请在此处执行特定于苹果的初始化
}
公共void Sync(){
//苹果同步
}
}
内部类ExtendService(){
公共静态void addAppleMasic(此SyncService syncAgent){
syncAgent.AddService(新的AppleSyncService());
}            
}
}
显然,这些代码都无法编译,用记事本编码可能是个坏主意。但是,它为您提供了一个基于模式的替代方案,以替代上面的示例代码。而且,如果你添加了第三个音乐服务,你就不会冒着破坏苹果和spotify的风险,仅仅为了插入新的音乐服务

祝你好运。听起来是个有趣的项目

namespace MusicService {
    public interface IService {
        //propably a List<IAlbum> and List<IA> somewhere in here
        void Sync();
    }

    public class SyncService {
        internal List<IService> _services;

        public void AddService(IService musicService) {
            if(_services==null){_services=new List<IService>();}
            _services.Add(musicService);
        }

        public void Sync() {
            foreach(IService ms in _services) {
                ms.Sync();
            }
        }
    }
}

namespace MusicService.AppleMusic {
    internal AppleSyncService:IService {
        public AppleSyncService() {
            //Do your apple-specific initializations here
        }
        public void Sync() {
            //apple-sync
        }
    }

    internal class ExtendService(){
        public static void AddAppleMusic(this SyncService syncAgent) {
           syncAgent.AddService(new AppleSyncService());
        }            
    }
}