首页 \ 问答 \ 如何动态实现代理模式?(How do I dynamically implement the Proxy Pattern?)

如何动态实现代理模式?(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
更新时间:2021-10-06 21:10

最满意答案

文档说:

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") ...
  • 这里是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 ...
  • 尝试这个。 在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 ...
  • 文档说: AVAudioSessionCategoryRecord录制音频的类别; 此类别使播放音频静音。 这是您正在使用的唯一音频会话类别,并且您在开始尝试播放AVAudioPlayer时就设置它,因此您自然无法听到任何声音。 你需要考虑更多关于灵活和正确的音频会话。 如果您想播放声音然后开始录制,请使用播放类别播放声音,并且在被告知(通过AVAudioPlayerDelegate)声音结束之前不要开始录制。 The docs say: AVAudioSessionCategoryRecord The c ...
  • 自从您调用audioPlayer?.play()以来,代码中的countAudio函数具有异步播放声音的audioPlayer?.play() 。 audioCounter变量仅在audioPlayer为nil时递增。 试试这个版本 var soundFileURLRef: NSURL! var audioPlayer: AVAudioPlayer? var audioCounter = 0 override func viewDidLoad() { super.viewDidLoad() ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)