【发布时间】:2015-10-31 20:03:37
【问题描述】:
我想用 IOC 容器 autofac 调用 MessageStore 类。
如何在最新版本的 autofac 中注册这个装饰器和复合模式?
这段代码结合了复合和装饰器模式,注意SOLID Principal。
我对此感到困惑。我的问题是,这段代码如何用 autofac 定义
var fileStore = new FileStore(new DirectoryInfo(HostingEnvironment.MapPath("~/file"))); var cacheStore = new CacheStore(fileStore, fileStore); var logStore = new LogStore(cacheStore, cacheStore); var messageStore = new MessageStore(logStore, logStore); messageStore.Save(12, "Hello");
public class FileStore : IFileLocator, IStoreWriter,IStoreReader
{
private readonly DirectoryInfo _workingDirectory;
public FileStore(DirectoryInfo workDirectory)
{
if (workDirectory == null)
throw new ArgumentNullException("workDirectory");
if (!workDirectory.Exists)
throw new ArgumentException("Directory not found", "workDirectory");
_workingDirectory = workDirectory;
}
public virtual void Save(int id, string message)
{
var path = GetFileInfo(id).FullName;
File.WriteAllText(path, message);
}
public virtual Maybe<string> Read(int id)
{
var file = GetFileInfo(id);
if (!file.Exists)
return new Maybe<string>();
var path = file.FullName;
return new Maybe<string>(File.ReadAllText(path));
}
public virtual FileInfo GetFileInfo(int id)
{
return new FileInfo(
Path.Combine(_workingDirectory.FullName, id + ".txt"));
}
}
public class CacheStore : IStoreWriter, IStoreReader
{
private readonly ConcurrentDictionary<int, Maybe<string>> _cache;
private readonly IStoreWriter _writer;
private readonly IStoreReader _reader;
public CacheStore(IStoreWriter writer, IStoreReader reader)
{
_cache = new ConcurrentDictionary<int, Maybe<string>>();
_writer = writer;
_reader = reader;
}
public virtual void Save(int id, string message)
{
_writer.Save(id, message);
var m = new Maybe<string>(message);
_cache.AddOrUpdate(id, m, (i, s) => m);
}
public virtual Maybe<string> Read(int id)
{
Maybe<string> retVal;
if (_cache.TryGetValue(id, out retVal))
return retVal;
retVal = _reader.Read(id);
if (retVal.Any())
_cache.AddOrUpdate(id, retVal, (i, s) => retVal);
return retVal;
}
}
public class LogStore : IStoreWriter, IStoreReader
{
private readonly IStoreWriter _writer;
private readonly IStoreReader _reader;
public LogStore(IStoreWriter writer, IStoreReader reader)
{
_writer = writer;
_reader = reader;
}
public void Save(int id, string message)
{
Log.Information("Saving message {id}.", id);
_writer.Save(id, message);
Log.Information("Saved message {id}.", id);
}
public Maybe<string> Read(int id)
{
Log.Debug("Reading message {id}.", id);
var retVal = _reader.Read(id);
if (retVal.Any())
Log.Debug("Returning message {id}.", id);
else
Log.Debug("No message {id} found.", id);
return retVal;
}
}
public class MessageStore
{
private readonly IStoreWriter _writer;
private readonly IStoreReader _reader;
public MessageStore(IStoreWriter writer, IStoreReader reader)
{
if (writer == null)
throw new ArgumentNullException("writer");
if (reader == null)
throw new ArgumentNullException("reader");
_writer = writer;
_reader = reader;
}
public void Save(int id, string message)
{
_writer.Save(id, message);
}
public Maybe<string> Read(int id)
{
return _reader.Read(id);
}
}
【问题讨论】:
-
您是否尝试过自己实现此功能?你被什么困住了?在 Autofac 文档站点 (docs.autofac.org/en/latest/advanced/adapters-decorators.html) 和这篇博文 (nblumhardt.com/2010/04/…) 上有一些关于装饰器的好信息。我也认为你需要更清楚你想要达到的目标。
-
我不知道。但我对此感到困惑。我的问题是,这段代码如何用 autofac
var fileStore = new FileStore(new DirectoryInfo(HostingEnvironment.MapPath("~/file"))); var cacheStore = new CacheStore(fileStore, fileStore); var logStore = new LogStore(cacheStore, cacheStore); var messageStore = new MessageStore(logStore, logStore); messageStore.Save(12, "Hello");定义 -
您能否更新您的问题以包含此信息,而不是将其作为评论留在此处?这将有助于了解实际询问的内容。
标签: c# decorator autofac composite