开发者

How to prohibit repositories usage?

Let's imagine the following contour (I assumed we use ASP.NET MVC but it doesn't matter much for this question)

  1. We have repositories that are used directly from controllers

  2. Some of repositories methods need to be treated some specific way, combined, cached etc. So we create services for these cases. So now some of repositories methods should be 开发者_开发百科accessed from services but not directly. What are the ways to control repositories methods are used from the right place?

I see the following way:

Do not use repositories directly at all - use them only from services (we even may make repositories internal and visible only to services assembly). But in this case we have to create repostories methods' wrappers even for methods that could be used from repositories directly.

But I'm pretty sure there are more interesting, beauty and flexible ways to implement it...


The general best practice recommendation I've seen is to always use a service layer if you need it sometimes. That way you get used to having it and using it and won't mistakenly bypass it when you're supposed to use it.

However, that's not what I do in my application in a very similar situation. In our application we have DAL (Data Access Layer) classes and BAL *Business Access Layer) classes. However, the vast majority of calls don't require any business rules. Out of 25 DAL's, we only really need 3 BAL's. So the rest of the 22 BAL's would just be straight facade/proxy classes.

So to get around this we defined an interface that all our DAL/BAL classes implement (one per type). So for UserDal and UserBal both implement IUser (or UserBal may be missing entirely, as is actually the case for users).

Then we use reflection to implement the following method:

BalFactory.GetClass<IUser>()

Which will look for an implementation of IUser in the BAL namespace, and if it doesn't find one, it returns the IUser implementation from the DAL namespace. Then if we ever need to add a BAL class later on when we didn't have one originally, we simply need to add it to the BAL namespace with the appropriate interface and it's automatically picked up and used.

We started using this in the application about four years ago with reflection and we've been very happy with it. Today, you can do it even easier with MEF I think.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜