开发者

Is this ok in a DI world?

I have implemented the Unity DI in a project of mine, but I have, what I believe is a simple question.

My DataContext:

public partial class AuctionDataContext : DataContext {
  public Table<AuctionItem> AuctionItems;

  ...
 }

Some code for inserting an AuctionItem in the database. Please notice how I cast the interface to the actual type. This is for everyt开发者_JS百科hing to work correct with the DataContext.

 public void Insert(IAuctionItem item) {
  _dataStore.DataContext.AuctionItems.InsertOnSubmit((AuctionItem)item);
  _dataStore.DataContext.SubmitChanges();
 }

Neither the AuctionItem nor the DataContext type is ever exposed to the client code but are only accessible inside the database layer. I guess my question is, is this an ok architecture ?


Your architecture is "ok", but all architectures are contextual, in that one pattern might be perfect in one situation but horrible in another.

Pattern wise it is always smart to abstract your Repository/DAO from the persistence mechanism, and from what i can see you have done that. I do suggest that you another interface around your implementation that would give you the flexibility to change implementation.

 public interface IRepository<T>{
    void Insert(T item);
 }

 public class AuctionItem : IRepository<IAuctionItem> {

   public void Insert(IAuctionItem item) {
      _dataStore.DataContext.AuctionItems.InsertOnSubmit((AuctionItem)item);
      _dataStore.DataContext.SubmitChanges();
   }

 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜