0

我有以下代码

SmtpSection smtpsection = 
       (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

EmailSettings emailSettings = new EmailSettings
        {
            WriteAsFile = bool.Parse(ConfigurationManager.AppSettings[""] ?? "false"),
            ServerPort = smtpsection.Network.Port,
            Username = smtpsection.Network.UserName,
            Password = smtpsection.Network.Password,
            UseSsl = smtpsection.Network.EnableSsl
        };

kernel.Bind<IEmailRepository>().To<EmailRepository>().WithConstructorArgument("settings", emailSettings);

public class EmailSettings 
{
        public string MailToAddress = "test@Test.com";
        public string MailFromAddress = "test@test.com";
        public bool UseSsl = true;
        public string Username = "";
        public string Password = "";
        public string ServerName = "";
        public int ServerPort = 587;
        public bool WriteAsFile = false;
        public string FileLocation = @"C:\sports_store_emails";
}

public class EmailController : Controller
{
        private IEmailRepository emailRepository;
        private EmailSettings emailSettings;

        public EmailController(IEmailRepository emailRepository, EmailSettings settings)
        {
            this.emailRepository = emailRepository;
            this.emailSettings = settings;
        }
}

public class EmailRepository : RepositoryBase<EmailEnquiry>, IEmailRepository
{
    public EmailRepository(IDatabaseFactory databaseFactory)
        : base(databaseFactory)
    {
    }
}

public interface IEmailRepository : IRepository<EmailEnquiry>
{
}

public interface IRepository<T> where T : class
{
        void Add(T entity);
        void Update(T entity);
        void Delete(T entity);      
        void Delete(Expression<Func<T, bool>> where);
        T GetById(long Id);
        T GetById(string Id);
        T Get(Expression<Func<T, bool>> where);
        IEnumerable<T> GetAll();
        IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
        IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
}

由于某种原因,NInject 的参数设置没有被传入。有谁知道这是为什么。settings 使用的是内部的所有值,EmailSettings而不是 NInject 传入的值?

4

1 回答 1

0

如果您已经为您配置了绑定,IDatabaseFactory则可以创建EmailRepository

var emailRepository = kernel.Get<EmailRepository>();

更新

要初始化您的实例,EmailController您可以使用

var emailController =
          new EmailController(kernel.Get<EmailRepository>(), settings);
于 2013-10-20T20:56:13.120 回答