0

我在我的项目中使用 StructureMap 来使用 DepencyInjection 。我的解决方案中有 5 个项目。

我有IUnitOfWork接口 inDAL和我的定义函数IUnitOfWorkin ApplicationDbContext

应用程序上下文:

public class ApplicationDbContext : DbContext, IUnitOfWork
{
    public ApplicationDbContext()
        : base("ApplicationDBContext")
    {
    }

    public virtual DbSet<User> Users { get; set; }
    public void ForceDatabaseInitialize()
    {
        Database.Initialize(true);
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    #region IUnitOfWork Members

    public void MarkAsDeleted<TEntity>(TEntity entity) where TEntity : class
    {
        Entry(entity).State = EntityState.Deleted;
    }
    public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        return base.Set<TEntity>();
    }

.
.
.

现在当我想IUnitOfWork在主项目中注册时:

 public static class StructureMapDefnation
{
    private static readonly Lazy<Container> _containerBuilder =
        new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication);
    public static IContainer Container
    {
        get { return _containerBuilder.Value; }
    }
    private static Container defaultContainer()
    {
        var container = new Container(ioc =>
        {
            // map same interface to different concrete classes
            ioc.For<IUser>().Use<EfUserService>();
            ioc.For<IUnitOfWork>().Use(() => new ApplicationDbContext())();
        });
        container.AssertConfigurationIsValid();

        return container;
    }
}

它告诉我这个错误:

严重性代码 描述 项目文件行抑制状态错误 CS0149 预期方法名称 BimehKosarFinal E:\myproject\BimehKosarFinal\BimehKosarFinal\StructureMap\StructureMapDefnation.cs 28 活动

在这一行:

ioc.For<IUnitOfWork>().Use(() => new ApplicationDbContext())();

有什么问题 ?我怎么解决这个问题 ?

4

1 回答 1

1

删除最后一个(),然后写

ioc.For<IUnitOfWork>().Use(() => new ApplicationDbContext());

或者

ioc.For<IUnitOfWork>().Use<ApplicationDbContext>();
于 2017-07-28T16:10:52.333 回答