Should I have separate units of work for each EF bounded context?

I have some EF bounded contexts like follows

public class BoundedContext_1 : DbContext
{
    IDbSet<A> As { get; set; }
    IDbSet<B> Bs { get; set; }
}

public class BoundedContext_2 : DbContext
{
    IDbSet<C> Cs { get; set; }
    IDbSet<D> Ds { get; set; }
}

public class BoundedContext_3 : DbContext
{
    IDbSet<D> Ds { get; set; }
    IDbSet<E> Es { get; set; }
    IDbSet<F> Fs { get; set; }
}

I am trying to implement UoW/Repository pattern for my solution.

Now it seems to me that I will end up creating many repositoris (7 in this case) each of which dedicated to a separate IDbSet of a bounded context and therefore having many units of work (3 in this case) having their own repositories – that’s a lot of effort and does not seem feasible since my actual model is much more complicated than the example above.

The question is “Am I forced to go that way? should I have a separate UoW for each bounded context with its own repos?” Or there is a way to avoid this and do it in a simpler way?

Thanks in advance

No, you are not forced to go that way. In fact, you could use your DbContexts and IDbSets just the way they are.

A DbContext is an implementation of the unit of work pattern. Once created, it will aggregate any changes made to its entities, and will commit when instructed.

In a same way, an IDbSet is an implementation of the repository pattern. It serves as an abstract collection of entities (IQueryable), that can be queried and has basic CRUD operations.

Here are a couple of examples:

public class BService
{
    public void DoSomethingToB(Guid bId)
    {
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            var b = BRepository.Find(bId);
            b.DoSomething();
            unitOfWork.SaveChanges();
        }
    }

    public B AddNewB()
    {
        var newB = new B();
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            BRepository.Add(newB);
            unitOfWork.SaveChanges();
        }
        return newB;
    }
}

This will most definitely work. Not a single class created.

The question is whether the EF implementation is sufficient. You should check whether your project’s requirements call for a layer of abstraction around EF’s implementation.

Such reasons could be:

  1. You have some non-generic logic in a repository or a unit of work that you want to unit test. It isn’t straightforward to unit test EF, so you could consider calling EF from your own repo/uow and mock it when unit testing.

  2. You want an explicit API for your repositories. e.g. BRepository.AddNewB(newB) instead of BRepository.Add(newB), or bRepo.GetBWithId(bId) instead of bRepo.Find(bId).

  3. You estimate that you will have to replace your DAL one day and support a technology other than EF. I hear this a lot but never seen such replacement in practice. Usually when a project changes its DAL, the new technology is significantly different and the API is changed together with the implementation (e.g. switching from a RDBS to a NoSQL DB).

3

The simple answer to your question is that EF is itself an implementation of the UoW/Repository pattern. The fact that you are trying to implement a Uow/Repository pattern as if EF were your data source is a good indication you are thinking about your problem incorrectly.

Or there is a way to avoid this and do it in a simpler way?

Yes. Simply use EF as your UoW/Repo implementation, or scrap EF for an alternative implementation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Should I have separate units of work for each EF bounded context?

I have some EF bounded contexts like follows

public class BoundedContext_1 : DbContext
{
    IDbSet<A> As { get; set; }
    IDbSet<B> Bs { get; set; }
}

public class BoundedContext_2 : DbContext
{
    IDbSet<C> Cs { get; set; }
    IDbSet<D> Ds { get; set; }
}

public class BoundedContext_3 : DbContext
{
    IDbSet<D> Ds { get; set; }
    IDbSet<E> Es { get; set; }
    IDbSet<F> Fs { get; set; }
}

I am trying to implement UoW/Repository pattern for my solution.

Now it seems to me that I will end up creating many repositoris (7 in this case) each of which dedicated to a separate IDbSet of a bounded context and therefore having many units of work (3 in this case) having their own repositories – that’s a lot of effort and does not seem feasible since my actual model is much more complicated than the example above.

The question is “Am I forced to go that way? should I have a separate UoW for each bounded context with its own repos?” Or there is a way to avoid this and do it in a simpler way?

Thanks in advance

No, you are not forced to go that way. In fact, you could use your DbContexts and IDbSets just the way they are.

A DbContext is an implementation of the unit of work pattern. Once created, it will aggregate any changes made to its entities, and will commit when instructed.

In a same way, an IDbSet is an implementation of the repository pattern. It serves as an abstract collection of entities (IQueryable), that can be queried and has basic CRUD operations.

Here are a couple of examples:

public class BService
{
    public void DoSomethingToB(Guid bId)
    {
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            var b = BRepository.Find(bId);
            b.DoSomething();
            unitOfWork.SaveChanges();
        }
    }

    public B AddNewB()
    {
        var newB = new B();
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            BRepository.Add(newB);
            unitOfWork.SaveChanges();
        }
        return newB;
    }
}

This will most definitely work. Not a single class created.

The question is whether the EF implementation is sufficient. You should check whether your project’s requirements call for a layer of abstraction around EF’s implementation.

Such reasons could be:

  1. You have some non-generic logic in a repository or a unit of work that you want to unit test. It isn’t straightforward to unit test EF, so you could consider calling EF from your own repo/uow and mock it when unit testing.

  2. You want an explicit API for your repositories. e.g. BRepository.AddNewB(newB) instead of BRepository.Add(newB), or bRepo.GetBWithId(bId) instead of bRepo.Find(bId).

  3. You estimate that you will have to replace your DAL one day and support a technology other than EF. I hear this a lot but never seen such replacement in practice. Usually when a project changes its DAL, the new technology is significantly different and the API is changed together with the implementation (e.g. switching from a RDBS to a NoSQL DB).

3

The simple answer to your question is that EF is itself an implementation of the UoW/Repository pattern. The fact that you are trying to implement a Uow/Repository pattern as if EF were your data source is a good indication you are thinking about your problem incorrectly.

Or there is a way to avoid this and do it in a simpler way?

Yes. Simply use EF as your UoW/Repo implementation, or scrap EF for an alternative implementation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Should I have separate units of work for each EF bounded context?

I have some EF bounded contexts like follows

public class BoundedContext_1 : DbContext
{
    IDbSet<A> As { get; set; }
    IDbSet<B> Bs { get; set; }
}

public class BoundedContext_2 : DbContext
{
    IDbSet<C> Cs { get; set; }
    IDbSet<D> Ds { get; set; }
}

public class BoundedContext_3 : DbContext
{
    IDbSet<D> Ds { get; set; }
    IDbSet<E> Es { get; set; }
    IDbSet<F> Fs { get; set; }
}

I am trying to implement UoW/Repository pattern for my solution.

Now it seems to me that I will end up creating many repositoris (7 in this case) each of which dedicated to a separate IDbSet of a bounded context and therefore having many units of work (3 in this case) having their own repositories – that’s a lot of effort and does not seem feasible since my actual model is much more complicated than the example above.

The question is “Am I forced to go that way? should I have a separate UoW for each bounded context with its own repos?” Or there is a way to avoid this and do it in a simpler way?

Thanks in advance

No, you are not forced to go that way. In fact, you could use your DbContexts and IDbSets just the way they are.

A DbContext is an implementation of the unit of work pattern. Once created, it will aggregate any changes made to its entities, and will commit when instructed.

In a same way, an IDbSet is an implementation of the repository pattern. It serves as an abstract collection of entities (IQueryable), that can be queried and has basic CRUD operations.

Here are a couple of examples:

public class BService
{
    public void DoSomethingToB(Guid bId)
    {
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            var b = BRepository.Find(bId);
            b.DoSomething();
            unitOfWork.SaveChanges();
        }
    }

    public B AddNewB()
    {
        var newB = new B();
        using (var unitOfWork = new BoundedContext_1())
        {
            var BRepository = unitOfWork.Bs;
            BRepository.Add(newB);
            unitOfWork.SaveChanges();
        }
        return newB;
    }
}

This will most definitely work. Not a single class created.

The question is whether the EF implementation is sufficient. You should check whether your project’s requirements call for a layer of abstraction around EF’s implementation.

Such reasons could be:

  1. You have some non-generic logic in a repository or a unit of work that you want to unit test. It isn’t straightforward to unit test EF, so you could consider calling EF from your own repo/uow and mock it when unit testing.

  2. You want an explicit API for your repositories. e.g. BRepository.AddNewB(newB) instead of BRepository.Add(newB), or bRepo.GetBWithId(bId) instead of bRepo.Find(bId).

  3. You estimate that you will have to replace your DAL one day and support a technology other than EF. I hear this a lot but never seen such replacement in practice. Usually when a project changes its DAL, the new technology is significantly different and the API is changed together with the implementation (e.g. switching from a RDBS to a NoSQL DB).

3

The simple answer to your question is that EF is itself an implementation of the UoW/Repository pattern. The fact that you are trying to implement a Uow/Repository pattern as if EF were your data source is a good indication you are thinking about your problem incorrectly.

Or there is a way to avoid this and do it in a simpler way?

Yes. Simply use EF as your UoW/Repo implementation, or scrap EF for an alternative implementation.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật