如何编写从工作单元获取某种类型的存储库的方法

发布于 2024-12-16 00:43:18 字数 1851 浏览 2 评论 0原文

您好,有人可以告诉我如何编写一种方法来从工作单元获取某种类型的存储库吗?

所以我的工作单元是:

public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private Context context = new Context();
        private VectorCheckRepository<Invoice> _invoiceRepository;
        private VectorCheckRepository<InvoiceLine> _invoiceLineRepository;

        public virtual Repository<Invoice> InvoiceRepository
        {
            get
            {
                if (this._invoiceRepository == null) {
                    this._invoiceRepository = new VectorCheckRepository<Invoice>(context);
                }
                return _invoiceRepository;
            }
        }

        public virtual VectorCheckRepository<InvoiceLine> InvoiceLineRepository
        {
            get
            {
                if (this._invoiceLineRepository == null) {
                    this._invoiceLineRepository = new VectorCheckRepository<InvoiceLine>(context);
                }
                return _invoiceLineRepository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed) {
                if (disposing) {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

但是在运行时我想从基于类型的工作单元获取存储库。

假设我去了:

_unitOfWork.GetRepository(invoice);

我在这里要做的是想要取回 InvoiceRepository,因为我向它传递了一张发票。

或者:

_unitOfWork.GetRepository(invoiceLine);

我希望它返回 InvoiceLineRepository。

有谁知道如何实现这一目标?

Hi can anybody tell me how you could write a method to get a repository of a certain type from the unit of work?

So my unit of work is:

public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private Context context = new Context();
        private VectorCheckRepository<Invoice> _invoiceRepository;
        private VectorCheckRepository<InvoiceLine> _invoiceLineRepository;

        public virtual Repository<Invoice> InvoiceRepository
        {
            get
            {
                if (this._invoiceRepository == null) {
                    this._invoiceRepository = new VectorCheckRepository<Invoice>(context);
                }
                return _invoiceRepository;
            }
        }

        public virtual VectorCheckRepository<InvoiceLine> InvoiceLineRepository
        {
            get
            {
                if (this._invoiceLineRepository == null) {
                    this._invoiceLineRepository = new VectorCheckRepository<InvoiceLine>(context);
                }
                return _invoiceLineRepository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed) {
                if (disposing) {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

However at run time I want to get a repository from the unit of work based on a type.

So say I went:

_unitOfWork.GetRepository(invoice);

What I would be doing here is wanting to get back the InvoiceRepository because I passed it an invoice.

or:

_unitOfWork.GetRepository(invoiceLine);

I would want it to return the InvoiceLineRepository.

Does anyone know how to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混浊又暗下来 2024-12-23 00:43:18

没有通用的方法可以做到这一点(除了一些丑陋的缓慢反射与命名约定相结合=没有编译时检查和容易破坏的解决方案)。我通过简单地创建带有大量 if 的方法或使用准备好的字典来实现此目的,在字典中我根据实体类型或动态创建的存储库类型获取整个存储库实例。

There is no generic way to do this (except some ugly slow reflection combined with naming conventions = no compile time check and easy to break solution). I did this by simply creating either method with a lot of ifs or by using prepared dictionary where I got either whole repository instance based on entity type or repository type for dynamic creation.

埋葬我深情 2024-12-23 00:43:18

假设您的工作单元包装了一个 ObjectContext,您可以使用 CreateObjectSet ,它为您的存储库提供了要包装的数据源。

例如:

public class SqlRepository<T> : IRepository<T>
                                 where T : class, IEntity {

    public SqlRepository(ObjectContext context) {
           _objectSet = context.CreateObjectSet<T>();
    }

    protected ObjectSet<T> _objectSet;
    // ....
}

您可以在此 MVC3 项目中看到一个示例:http://dl.dropbox。 com/u/13430676/employeetimecards.zip

该代码附带一篇关于使用 UoW 和存储库进行 EF 可测试性的文章:http://msdn.microsoft.com/en-us/library/ff714955.aspx

希望有帮助,

Assuming your unit of work wraps an ObjectContext, you can use CreateObjectSet<T> , which gives you the data source for your repository to wrap.

For example:

public class SqlRepository<T> : IRepository<T>
                                 where T : class, IEntity {

    public SqlRepository(ObjectContext context) {
           _objectSet = context.CreateObjectSet<T>();
    }

    protected ObjectSet<T> _objectSet;
    // ....
}

You can see an example in this MVC3 project: http://dl.dropbox.com/u/13430676/employeetimecards.zip

The code accompanies an article on EF Testability using UoW and Repositories: http://msdn.microsoft.com/en-us/library/ff714955.aspx

Hope that helps,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文