如何动态实现代理模式?(How do I dynamically implement the Proxy Pattern?)
我正在重新审视我去年写的班级跟踪(脏逻辑)。 目前我有一个处理所有状态跟踪的超级基类,但是我需要跟踪的每个属性需要遵循标准
get { return _x; } set { _isDirty = true; _x = value; }
get { return _x; } set { _isDirty = true; _x = value; }
工作方式。在使用Entity Framework并阅读代理模式之后 ,我希望有更好的方法来实现我的IsDIrty Logic,同时能够利用自动实现的属性?
说实话,我不知道我在说什么。 有没有办法可以做如下的事情:
public class Customer : ITrackable { [TrackState(true)] // My own attribute public virtual string Name { get;set;} [TrackState(true)] public virtual int Age { get;set;} // From ITrackable public bool IsDirty { get; protected set; } }
然后实现一个动态代理,它将使用反射(或另一个神奇的解决方案)在使用
TrackState
属性在属性上设置值之前先调用另一个方法。显然,我可以通过创建一个phyiscal代理类并使用IoC来轻松完成这项工作:
public class CustomerProxy : Customer { Customer _customer; public override string Name { get { return _customer.Name; } set { IsDirty = true; return _customer.Name; } } // Other properties }
但我不想为每个对象做这个,否则我现有的解决方案没有任何好处。 希望有人能满足我的好奇心,或者至少告诉我EF如何实现它。
I'm revisiting my class tracking (dirty logic), which I wrote last year. Currently I have an uber base class that deals with all the state tracking, but each property whos values I need to track needs to stick to the standard
get { return _x; } set { _isDirty = true; _x = value; }
way of working.After playing with Entity Framework and reading up on the Proxy Pattern, I was hoping there was a nicer way to implement my IsDIrty Logic whilst being able to make use of auto implemented properties?
To be completely honest, I haven't a clue of what I'm talking about. Is there a way I can do something like the following:
public class Customer : ITrackable { [TrackState(true)] // My own attribute public virtual string Name { get;set;} [TrackState(true)] public virtual int Age { get;set;} // From ITrackable public bool IsDirty { get; protected set; } }
And then implement a dynamic proxy which will use reflection (or another magical solution) to call another method first before setting the values on the properties with the
TrackState
attribute.Obviously I could easily do this by creating a phyiscal proxy class and use IoC:
public class CustomerProxy : Customer { Customer _customer; public override string Name { get { return _customer.Name; } set { IsDirty = true; return _customer.Name; } } // Other properties }
But I don't fancy having to do this for every object, otherwise there's no benefit from my existing solution. Hope someone can satisfy my curiosity, or at least tell me how EF achieves it.
原文:https://stackoverflow.com/questions/3702512
最满意答案
文档说:
AVAudioSessionCategoryRecord录制音频的类别; 此类别使播放音频静音。
这是您正在使用的唯一音频会话类别,并且您在开始尝试播放AVAudioPlayer时就设置它,因此您自然无法听到任何声音。 你需要考虑更多关于灵活和正确的音频会话。 如果您想播放声音然后开始录制,请使用播放类别播放声音,并且在被告知(通过AVAudioPlayerDelegate)声音结束之前不要开始录制。
The docs say:
AVAudioSessionCategoryRecord The category for recording audio; this category silences playback audio.
That is the only audio session category you are using, and you are setting it as soon as you start trying to play your AVAudioPlayer, so naturally you cannot hear anything. You need to think some more about being nimble and correct with your audio session. If you want to play a sound and then start recording, use a Playback category for playing the sound, and don't start recording until you are told (through the AVAudioPlayerDelegate) that the sound is finished.
相关问答
更多-
您需要设置您的应用功能背景模式(音频和AirPlay)并将您的AVAudioSession类别设置为AVAudioSessionCategoryPlayback并将其设置为活动 从Xcode 8.2.1•Swift 3.0.2开始 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers) print("Playback OK") ...
-
在Swift中录制音频(Recording audio in Swift)[2023-09-11]
这里是code.You可以很容易地记录。在IBAction上写这个代码。它将通过名称recordTest.caf保存在Documents的记录 //declare instance variable var audioRecorder:AVAudioRecorder! func record(){ var audioSession:AVAudioSession = AVAudioSession.sharedInstance() audioSession.setCategor ... -
我通过改变两件事情来让你的代码工作: 预设名称:从AVAssetExportPresetPassthrough到AVAssetExportPresetAppleM4A 输出文件类型:从AVFileTypeWAVE到AVFileTypeAppleM4A 像这样修改您的assetExport声明: var assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) assetExp ...
-
UPADTE /解决方案: 所以问题在于audioPlayer在播放声音之前会被释放,为了解决这个问题,我必须在类中创建一个属性,而不是在函数中创建它的一个实例。 更新后的代码如下所示: 该类的属性声明中的可选引用: var audioPlayer : AVAudioPlayer? 利用audioPlayer的功能: private func playFinishedSound(){ if let pathResource = NSBundle.mainBundle().pathForRe ...
-
您可以使用AVAssetResourceLoader播放没有扩展名的音频。 这是一个例子。 首先,配置resourceloader加载器的委托 var playerAsset: AVAsset! if fileURL.pathExtension.count == 0 { var components = URLComponents(url: fileURL, resolvingAgainstBaseURL: false)! components.scheme = "fake" // ma ...
-
我不明白为什么这不起作用: var audioPlayer: AVAudioPlayer! let string = "name" if let path = Bundle.main.path(forResource: string, ofType: "mp3") { let url = URL(fileURLWithPath: path) do { audioPlayer = try AVAudioPlayer(contentsOf: url) audioP ...
-
播放/暂停音频按钮Swift(Play/Pause Audio Button Swift)[2023-08-02]
尝试这个。 在viewDidLoad中执行此操作 let path = Bundle.main.path(forResource: "Hat 017-1.aif", ofType:nil)! let url = URL(fileURLWithPath: path) do { hihat16 = try AVAudioPlayer(contentsOf: url) hihat16.numberOfLoops = -1 } catch {} 然后在你的@IBAction中你可以做到这一点。 if !hihat ... -
你为什么用viewController初始化你的玩家? 你不需要那个。 将Player类更改为: import Foundation import AVFoundation class Player { // Variables var audioFile: AVAudioPlayer! // Initializer init() { // Set path for the attack sound let audioSnd = NSB ...
-
Swift 3音频无法播放(Swift 3 audio won't play)[2024-02-19]
文档说: AVAudioSessionCategoryRecord录制音频的类别; 此类别使播放音频静音。 这是您正在使用的唯一音频会话类别,并且您在开始尝试播放AVAudioPlayer时就设置它,因此您自然无法听到任何声音。 你需要考虑更多关于灵活和正确的音频会话。 如果您想播放声音然后开始录制,请使用播放类别播放声音,并且在被告知(通过AVAudioPlayerDelegate)声音结束之前不要开始录制。 The docs say: AVAudioSessionCategoryRecord The c ... -
使用Swift中的AV Audio Player播放条件音频(Conditional audio playing using AV Audio Player in Swift)[2022-03-24]
自从您调用audioPlayer?.play()以来,代码中的countAudio函数具有异步播放声音的audioPlayer?.play() 。 audioCounter变量仅在audioPlayer为nil时递增。 试试这个版本 var soundFileURLRef: NSURL! var audioPlayer: AVAudioPlayer? var audioCounter = 0 override func viewDidLoad() { super.viewDidLoad() ...