ソリューションに同じデータにアクセスするプロジェクトがいくつかあるので、後で別のプロジェクトでデータアクセスを実装しています。現在、EF4、汎用リポジトリ、および作業単位パターンを使用しています。依存性注入をサポートするようにデータアクセスを設計しましたが、Ninjectを使用したいと思います。これが私がこれまでに持っているもののサンプルです
public class Account
{
public int Id { get; set; }
public Guid WebId { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Mobile { get; set; }
}
public interface IRepository<T>
{
IEnumerable<T> Get(Expression<Func<T, bool>> filter, Func<IQueryable<T>);
T GetById(int id);
void Update(T dinner);
void Insert(T dinner);
void Delete(int id);
void Save();
}
リポジトリの実装もありますが、ここではスペースとして投稿しません。
私のUnitOfWorkは次のようになります
public class UnitOfWork
{
private Repository<Account> _accountRepository;
public IRepository<Account> AccountRepository
{
get
{
if (this._accountRepository == null)
{
_accountRepository = new Repository<Account>();
}
return _accountRepository;
}
}
}
リポジトリを自動解決するようにninjectを設定する方法と場所。これにより、インターフェイスを使用でき、作業単位でインスタンス化する必要がなくなります。これは正しいことですか、それとも私はDIのポイントをすべて間違っていますか?これが私のワーククラスのユニットをどのように見せたいと思うかです
public class UnitOfWork
{
IKernel _kernel;
public UnitOfWork()
{
_kernel = new StandardKernel();
}
private IRepository<Account> _accountRepository;
public IRepository<Account> AccountRepository
{
get
{
if (this._accountRepository == null)
{
_accountRepository = _kernel.Get<IRepository<Account>>();;
}
return _accountRepository;
}
}
}