C# Unity容器依赖注入-如何注册?
我有一个接口ICrudService和一个实现该接口的服务CrudService。我试图使用Unity进行依赖注入,但我看不出如何注册该类型 以下是我的ICrudService:C# Unity容器依赖注入-如何注册?,c#,dependency-injection,unity-container,C#,Dependency Injection,Unity Container,我有一个接口ICrudService和一个实现该接口的服务CrudService。我试图使用Unity进行依赖注入,但我看不出如何注册该类型 以下是我的ICrudService: public interface ICrudService<T> where T: DelEntity, new() { int Create(T item); void Save(); T Get(int id); IEnumerable<T> GetAll(
public interface ICrudService<T> where T: DelEntity, new()
{
int Create(T item);
void Save();
T Get(int id);
IEnumerable<T> GetAll();
IEnumerable<T> Where(Expression<Func<T, bool>> func, bool showDeleted = false);
}
public class CrudService<T> : ICrudService<T> where T : DelEntity, new ()
{
protected IRepo<T> repo;
public CrudService(IRepo<T> repo)
{
this.repo = repo;
}
public IEnumerable<T> GetAll()
{
return repo.GetAll();
}
public T Get(int id)
{
return repo.Get(id);
}
public virtual int Create(T item)
{
var newItem = repo.Insert(item);
repo.Save();
return newItem.Id;
}
public void Save()
{
repo.Save();
}
public virtual void Delete(int id)
{
repo.Delete(repo.Get(id));
repo.Save();
}
public void Restore(int id)
{
repo.Restore(repo.Get(id));
repo.Save();
}
public void BatchDelete(int[] ids)
{
foreach (var id in ids)
{
repo.Get(id).IsDeleted = true;
}
repo.Save();
}
public void BatchRestore(int[] ids)
{
foreach (var id in ids)
{
repo.Get(id).IsDeleted = false;
}
repo.Save();
}
public IEnumerable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false)
{
return repo.Where(predicate, showDeleted);
}
}
公共接口ICrudService,其中T:deletity,new()
{
int创建(T项);
作废保存();
T Get(int-id);
IEnumerable GetAll();
IEnumerable,其中(表达式func,bool showdeled=false);
}
这是我的CrudService:
public interface ICrudService<T> where T: DelEntity, new()
{
int Create(T item);
void Save();
T Get(int id);
IEnumerable<T> GetAll();
IEnumerable<T> Where(Expression<Func<T, bool>> func, bool showDeleted = false);
}
public class CrudService<T> : ICrudService<T> where T : DelEntity, new ()
{
protected IRepo<T> repo;
public CrudService(IRepo<T> repo)
{
this.repo = repo;
}
public IEnumerable<T> GetAll()
{
return repo.GetAll();
}
public T Get(int id)
{
return repo.Get(id);
}
public virtual int Create(T item)
{
var newItem = repo.Insert(item);
repo.Save();
return newItem.Id;
}
public void Save()
{
repo.Save();
}
public virtual void Delete(int id)
{
repo.Delete(repo.Get(id));
repo.Save();
}
public void Restore(int id)
{
repo.Restore(repo.Get(id));
repo.Save();
}
public void BatchDelete(int[] ids)
{
foreach (var id in ids)
{
repo.Get(id).IsDeleted = true;
}
repo.Save();
}
public void BatchRestore(int[] ids)
{
foreach (var id in ids)
{
repo.Get(id).IsDeleted = false;
}
repo.Save();
}
public IEnumerable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false)
{
return repo.Where(predicate, showDeleted);
}
}
公共类CrudService:ICrudService,其中T:deletity,new()
{
受保护的IRepo回购;
公共CrudService(IRepo回购)
{
this.repo=回购;
}
公共IEnumerable GetAll()
{
返回repo.GetAll();
}
公共T获取(int id)
{
返回回购获取(id);
}
公共虚拟整数创建(T项)
{
var newItem=回购插入(项目);
repo.Save();
返回newItem.Id;
}
公共作废保存()
{
repo.Save();
}
公共虚拟无效删除(int-id)
{
回购删除(回购获取(id));
repo.Save();
}
公共无效还原(int-id)
{
回购还原(repo.Get(id));
repo.Save();
}
public void BatchDelete(int[]id)
{
foreach(id中的变量id)
{
repo.Get(id).IsDeleted=true;
}
repo.Save();
}
公共void批还原(int[]id)
{
foreach(id中的变量id)
{
repo.Get(id).IsDeleted=false;
}
repo.Save();
}
public IEnumerable,其中(表达式谓词,bool showdeled=false)
{
返回回购协议,其中(谓词,已删除);
}
}
最后,我的UnitConfig.cs,我甚至无法构建它
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();
// TODO: Register your types here
container.RegisterType<ICrudService<T>, CrudService<T>>();
}
}
公共类UnityConfig
{
#区域统一容器
私有静态惰性容器=新惰性(()=>
{
var container=new UnityContainer();
注册类型(容器);
返回容器;
});
///
///获取已配置的Unity容器。
///
公共静态IUnityContainer GetConfiguredContainer()
{
返回容器。值;
}
#端区
///向Unity容器注册类型映射。
///要配置的unity容器。
///不需要注册控制器或API控制器等具体类型(除非您想注册)
///更改默认值),因为Unity允许解析具体类型,即使它以前没有注册。
公共静态无效注册表类型(IUnityContainer容器)
{
//注意:若要从web.config加载,请取消对以下行的注释。请确保将Microsoft.Practices.Unity.Configuration添加到using语句中。
//container.LoadConfiguration();
//TODO:在此处注册您的类型
container.RegisterType();
}
}
我在UnityConfig.cs中做错了什么?您得到的是一个C#编译器错误,表明
T
未知。这是在Unity中注册泛型类型的方法:
container.RegisterType(typeof(ICrudService<>), typeof(CrudService<>));
container.RegisterType(typeof(ICrudService),typeof(CrudService));