如何使用autofac

时间:2015-10-31 20:03:38

标签: c# decorator autofac composite

我想用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);
    }
}

2 个答案:

答案 0 :(得分:0)

鉴于您的所有商店都实现并使用IStoreReaderIStoreWriter,您可以通过将它们组合到单个IStore界面中来简化操作。这可以是定义SaveRead方法的单个接口,也可以根据需要从现有的IStoreReaderIStoreWriter接口继承。

然后,您可以将FileStore - 这似乎是主要实施 - 注册为关键服务。然后使用RegisterDecorator注册缓存和日志装饰器。最后,注册MessageStore并解析没有密钥的服务,这将是最外面的装饰器。

builder.Register(f => new FileStore(new DirectoryInfo(HostingEnvironment.MapPath("~/file"))))
    .Keyed<IStore>("file");

builder.RegisterDecorator<IStore>(
    (c, inner) => new CacheStore(inner), fromKey: "file", toKey: "cache");

builder.RegisterDecorator<IStore>(
    (c, inner) => new LogStore(inner), fromKey: "cache", toKey: null);

builder.Register(c => new MessageStore(c.Resolve<IStore>()));

我确信装饰器周围的API可以改进,这绝对是我打算很快看到的。

答案 1 :(得分:0)

谢谢Alex。但我用下面的代码解决了,因为我不想创建y = x2.reshape(2, -2) 。你觉得我的讨论?

IStore