[Architecture Pattern] Context
动机
在设计面向对象应用程序的时候,简单的分层会将系统分为Presentation Layer(PL)、 Business Logic Layer(BLL)、Data Access Layer(DAL)。但是三层之间的对象生成、依赖注入等等的设计会是一件很复杂的事情。例如:PL对象该怎么去生成BLL的对象、DAL的对象该怎么注入BLL。
本文介绍一个简单的Context模式,用来框出一个面向对象应用程序的架构,让每一层的对象设计有可循的规范。
结构
参与者
UserReportPage
-用户接口,呈现用户数据的报表。
-使用ContextContainer的静态属性取得Context。
-使用Context底下挂载的各种View、Repository,来做BLL边界的数据对象进出。
ContextContainer
-提供Context统一存放的对象。
-使用ContextFactory来生成Context以及底下挂载的各种View、Repository。
ContextFactory
-生成Context以及底下挂载的各种View、Repository。
-使用例如Spring.Net、Provider Pattern来反射生成DAL的各种View、Repository。
Context
-提供挂载的各种Repository,来做BLL边界的数据对象进出。
User
-进出BLL边界的数据对象。
IUserRepository
-数据对象进出BLL边界的接口。
-单纯的新增、修改、删除、查询。
SqlUserRepository
-数据对象进出BLL边界的接口IUserRepository的实作。
-依靠ContextFactory生成。
-将数据对象存入SQL数据库。
*也可实做OracleUserRepository将数据对象存入Oracle数据库。
OtherService
-BLL内,有功能却不需要进出边界的服务对象。
范例程序
*范例只包含关键片段的程序代码
public class UserReportPage
{
// Methods
public void ShowAllUser()
{
// 透过BLL边界取得数据对象
IEnumerable<User> userCollection = ContextContainer.CurrentContext.UserRepository.GetAll();
// Show
this.ShowUser(userCollection);
}
private void ShowUser(IEnumerable<User> userCollection)
{
//.....
}
}
public class ContextContainer
{
// Properties
private static Context _currentContext = null;
public static Context CurrentContext
{
get
{
if (_currentContext == null)
{
ContextFactory contextFactory = new ContextFactory();
_currentContext = contextFactory.CreateContext();
}
return _currentContext;
}
}
}
public class ContextFactory
{
// Methods
public Context CreateContext()
{
IUserRepository userRepository = null; // 使用例如Spring.Net、Provider Pattern来反射生成。
return new Context(userRepository);
}
}
public class Context
{
// Constructor
public Context(IUserRepository userRepository)
{
this.UserRepository = userRepository;
}
// Properties
public IUserRepository UserRepository { get; set; }
}
public interface IUserRepository
{
// Methods
void Add(User item);
void Modify(User item);
void Remove(Guid id);
User GetByID(Guid id);
IEnumerable<User> GetAll();
}
public class User
{
// Properties
public Guid UserID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
期許自己~
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。


浙公网安备 33010602011771号