我對Autofac非常陌生,無法理解註冊的語法。我有以下calsses /接口:Autofac - 註冊以接口爲參數的實例類型
//Interface
public interface ISender
{
void Send();
}
//implementations
public class Post : ISender
{
public void Send()
{
//Post implementation
}
}
public class Email : ISender
{
public void Send()
{
//Email implementation
}
}
以及調用這些實現現在
public class Consumer
{
ISender Sender;
public Consumer(ISender sender)
{
Sender = sender
}
public void Act()
{
Sender.Send();
}
}
一類,它實現調用是在一個控制器來決定,所以我嘗試使用IIndex從this page像:
public calss PostController
{
Consumer ConsumerObject;
public PostController(IIndex<string, Consumer> consumer)
{
ConsumerObject = consumer["post"];
}
}
public class EmailController
{
Consumer ConsumerObject;
public PostController(IIndex<string, Consumer> consumer)
{
ConsumerObject = consumer["email"];
}
}
首先,它是正確的還是可行的?現在的問題是我不明白如何註冊Autofac。那麼,我們如何在Autofac中註冊Consumer和ISender?請建議是否有更好的/替代方法。