Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created April 21, 2012 08:02
Show Gist options
  • Select an option

  • Save JeffreyZhao/2435473 to your computer and use it in GitHub Desktop.

Select an option

Save JeffreyZhao/2435473 to your computer and use it in GitHub Desktop.
public class Order
{
public int OrderID { get; set; }
public string Name { get; set; }
public DateTime CreateAt { get; set; }
}
public interface IOrderDao
{
Order Get(int orderId);
void Update(Order order);
}
public interface IIocContainer
{
T Resolve<T>();
}
public class DefaultIocContainer : IIocContainer
{
public readonly static DefaultIocContainer Instance = new DefaultIocContainer();
public T Resolve<T>()
{
throw new NotImplementedException();
}
}
public static class OrderLogicExtension
{
private static IIocContainer _iocContainer;
public static IIocContainer IocContainer
{
get { return _iocContainer ?? DefaultIocContainer.Instance; }
set { _iocContainer = value; }
}
public static void Resubmit(this Order order)
{
// do something
var dao = IocContainer.Resolve<IOrderDao>();
dao.Update(order);
}
}
class Program
{
static void Main(string[] args)
{
// use
var order = new Order();
order.Resubmit();
}
public void ResubmitTest()
{
var mockDao = new Mock<IOrderDao>();
// setup dao mock
// setup IOC container
var mockIocContainer = new Mock<IIocContainer>();
mockIocContainer.Setup(c => c.Resolve<IOrderDao>()).Returns(mockDao.Object);
OrderLogicExtension.IocContainer = mockIocContainer.Object;
// do
var order = new Order();
order.Resubmit();
// verify
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment