1
私はインターフェースICrudServiceと、インターフェースCrudServiceを実装したサービスを持っています。私は依存性注入目的のためにUnityを使用しようとしていますが、タイプを登録する方法を理解することはできません。ここでUnityコンテナ依存性注入 - これはどのように登録しますか?
は私ICrudServiceです:ここでは
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);
}
は私CrudServiceです:
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);
}
}
そして私も構築するために取得することはできません最後に、私の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.csで何が間違っていますか?
素晴らしいです。ありがとうございました。 – halterdev