我無法讓我的靜態主要方法與我的後端代碼很好地玩。使用靜態主要方法的非靜態代碼
這裏的後端的東西:
public interface ITicketGenerationService
{
string CreateTicket(DateTime begin, DateTime end);
}
public class TicketGenerationService : ITicketGenerationService
{
public static IRepository<Ticket> Repository { get; set; }
public TicketGenerationService(IRepository<Ticket> repository)
{
Repository = repository;
}
public string CreateTicket(DateTime begin, DateTime end)
{
//do stuff with Repository
//return status string to Main method
}
}
這裏就是我的控制檯應用程序開始:如果我改變CreateTicket靜態
An object reference is required for the non-static field, method, or property 'TicketMain.TicketGenerationService'
:
public class TicketMain
{
public ITicketGenerationService TicketGenerationService { get; set; }
static void Main(string[] args)
{
var priorityOneTickets = TicketGenerationService.CreateTicket(begin, end);
}
}
所以我最初得到這個錯誤,我得到另一個錯誤:
TicketGenerationService does not implment interface member ITicketGenerationService.CreateTicket(DateTime, DateTime). ITicketGenerationService.CreateTicket(DateTime, DateTime) cannot implement an interface member because it is static.
如果我嘗試補充一點:
var ticketingService = new TicketGenerationService();
的主要方法,恐怕會惹我的依賴注入(autofac)。
我明白這些錯誤,但我不能讓所有的東西都是靜態的,我害怕如果我開始實例化,我將失去通過依賴注入獲得的所有數據。
反正有這個嗎?
謝謝!