Interfaces and Facade Design Patterns
I have been using a facade design pattern to group together all the administrative funcionality I need in my program.
in my class library Company.Infrastructure.Repositories.Administration I have:
[Pluggable("Default")]
public class AdminRepository : IAdminRepository
{
#region private members
#endregion
#region protected members
protected Membership _membership;
开发者_JAVA技巧 protected Permissions _permissions;
protected Application _application;
protected Profile _profile;
#endregion
public AdminRepository()
{
_membership = new Membership();
_permissions = new Permissions();
_application = new Application();
_profile = new Profile();
}
protected class Profile
{
public Profile() {}
public void ProfileMethod1(){}
public void ProfileMethod2(){}
}
protected class Membership
{
public Membership() {}
public User GetUser(Guid id)
{
using (var dc = new My_SdbDataContext())
{
var user = dc.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
var membership = dc.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
return Convert.ToEntity(user, membership);
}
}
public User GetUser(string userName)
{
using (var dc = new My_SdbDataContext())
{
var user = dc.aspnet_Users.Where(x => x.UserName == userName).FirstOrDefault();
var membership = dc.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault();
return Convert.ToEntity(user, membership);
}
}
public IEnumerable<User> GetUsers(Guid applicationId)
{
var userList = new List<User>();
using (var dc = new My_SdbDataContext())
{
var users = dc.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
userList.AddRange((IEnumerable<User>) (from user in users
let membership = dc.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault()
select Convert.ToEntity(user, membership)));
}
return userList;
}
}
etc...
}
This has worked well for me. However, we have moved to a DDD model and I am trying to figure out how to continue to access my AdminRepository (formerly AdminFactory) from a WCF service.
To achieve access, I have been including the Interfaces to my Repository classes in my Domain logic. However, I am not quite sure how to go about creating an Interface to a Facade such as the one I have (with subclasses and all). Is this possible?
How do I do this?
I'm confused as to why you would want to create an interface to your AdminRepository
that includes nested classes . Based on your code, I would think you would have
public interface IAdminRepository
{
bool MemberHasPermission(int id)
//and so on - you didn't provide any methods, so I'm making one up
// ...
}
At this point, your IAdminRepository
specifies the contract, why would the contract also specify the implementation (in your case protected nested classes) ?
Note that this doesn't mean that I understand your usage of the protected nested classes, or why you call it the "facade pattern" when all of the abstracted components are actually internal implementation, or why moving to a DDD model makes accessing your repository from WCF hard. However, I certainly don't see why you'd try to specify nested classes in an interface, but c# does not allow it in any case (note that VB does allow nested types in interfaces). If you wish to specify contracts this way, you can use abstract classes without implementation instead of an interface, but this creates inheritance issues.
精彩评论