首页 新闻 会员 周边 捐助

这个程序的repository为什么这么设计的

0
悬赏园豆:40 [已关闭问题] 关闭于 2016-03-19 19:32

IRepository2.cs里面定义了两个interface

// interface1 To be used
public interface IRepository2
{
T Find<T>(params object[] keys);
void Save<T>(T domainObject);
IQueryable<T> Query<T>();
}

// interface2 To be implemented so that it will be used by the above IRepository2
public interface IRepository2<T>
{
T Find(params object[] keys);
void Save(T domainObject);
IQueryable<T> Query();
}

----------------------------------------

Repository2.cs里面实现了interface1

public class Repository2:IRepository2
{
private Dictionary<Type, object> _repositories;

public Repository2(IDictionary<Type, object> repositories = null)
{
if (repositories == null)
{
_repositories = new Dictionary<Type, object>();
}
else
{
if (!repositories.All(EnsureRepositoryValid)) throw new ArgumentException("Invalid repository found.", "repositories");
_repositories = new Dictionary<Type, object>(repositories);
}
}

private bool EnsureRepositoryValid(KeyValuePair<Type, object> repositoryItem)
{
var expectedRepositoryType = typeof(IRepository2<>).MakeGenericType(repositoryItem.Key);
var repository = repositoryItem.Value;
return repository.GetType()
.GetInterfaces()
.Any(type => type == expectedRepositoryType);
}

public void Register<T>(IRepository2<T> repository)
{
_repositories[typeof(T)] = repository;
}

public T Find<T>(params object[] keys)
{
IRepository2<T> repository = GetRepository<T>();
return repository.Find(keys);
}

private IRepository2<T> GetRepository<T>()
{
object repoObj;
_repositories.TryGetValue(typeof(T), out repoObj);
var repository = repoObj as IRepository2<T>;
if (repository == null) throw new NotSupportedException($"Repository does not support type of {typeof(T)}");
return repository;
}

public void Save<T>(T domainObject)
{
IRepository2<T> repository = GetRepository<T>();
repository.Save(domainObject);
}

IQueryable<T> IRepository2.Query<T>()
{
IRepository2<T> repository = GetRepository<T>();
return repository.Query();
}
}

------------------------------

startup.cs 里面DI了repository2

services.AddTransient<IRepository2>(s =>
{
var repository2 = new Repository2();

var subscriberRepoObject = ActivatorUtilities.CreateInstance(s, typeof(SubscriberRepository));
var deliveryStatusRepoObject = ActivatorUtilities.CreateInstance(s, typeof(DeliveryStatusRepository));

repository2.Register((SubscriberRepository)subscriberRepoObject);
repository2.Register((DeliveryStatusRepository)deliveryStatusRepoObject);

return repository2;
});

--------------------------------

controller.cs里面用interface1


private readonly IRepository2  _repository2;
public MailingListsController(IRepository2 repository2)
{
_repository2 = repository2;
}

[HttpGet("{id}/Subscribers/{subscriberId}")]
public IActionResult GetSubscriber(Guid id, Guid subscriberId)
{
Subscriber subscriber = _repository2.Find<Subscriber>(subscriberId);
if (subscriber != null)
{
return Ok(subscriber);
}
return HttpNotFound();
}

[HttpGet("{id}/Subscribers")]
public IActionResult GetSubscribers(Guid id)
{
var subscribers = _repository2.Query<Subscriber>().Where(s => s.MailingListId == id && s.IsDeleted == false);
return this.Ok(subscribers.ToList());
}

[HttpPost("{id}/Subscribers")]
public IActionResult PostSubscriber(Guid id, [FromBody]Subscriber subscriber)
{

if (subscriber.Id == Guid.Empty)
{
subscriber.MailingListId = id;
subscriber.IsActivated = true;
_repository2.Save(subscriber);
return Ok(subscriber);
}
else
{
return HttpBadRequest();
}

}

 

我的问题是没看到interface2 在哪里用到,谁能解释一下为什么这么设计程序。

zbh333022的主页 zbh333022 | 菜鸟二级 | 园豆:202
提问于:2016-03-19 12:11
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册