English 中文(简体)
ASP示范。 NET
原标题:Login Model for ASP.NET

你们能够帮助我解决我的问题:

i 设有伙伴关系。 NET网站具有一些基本授权特征(Login/Logoout e.t.c)。 目前有3种主要方法:

  • bool Login(string name, string password);
  • bool IsLogged();
  • string GetUserID();

所有这些方法都“硬编码”为一类。 伐木方法检查数据库,以确定用户。 但是,我当时决定使用XML文档储存用户,这意味着,对用户进行检查的所有逻辑都应当改写。

我提出使用以下“家长”的想法:

  1. Create interface (e.g. ILoginProvider) that declares those 3 methods that described above
  2. Implement this interface in any class and write specific logic in Login() method to check XML file or database
  3. Pass this class to ??? (here is my problem)

我想做一个能够把IloginProvider接口作为论点的班子(如LoginHelper):

class LoginHelper {

    private static ILoginProvider provider;

    // this method should be called somewhere in Application_Startup event in Global.asax
    public static void RegisterLoginProvider(ILoginProvider inst) {
        provider = inst; 
    }
}

其后编写必要方法:

public static bool IsLogged() {
    return provider.IsLogged();
}

之后称“全球登记记录仪”方法。 简称表 启动活动:

MyCustomProvider prov = new MyCustomProvider(); // this class implements ILoginProvider interface
LoginHelper.RegisterSecurityProvider(prov);

采用这种逻辑与他人一道改变某些“提供人”的正确方法吗?

最佳回答

这样做的方式往往是通过服务地点(团结、结构管理等)或习惯工厂,由您要求进行IloginProvider的检查,并归还正确的例子。

选择哪类IloginProvider来创建的责任并不取决于电话班。

//get a login provider from via a service locator
ILoginProvider provider = DependencyLookup.Resolve<ILoginProvider>();

有了服务定位器,你将在XML组合或代码中登记哪些执行工作。 这是团结的一个例子。

public static class ContainerConfiguration
{
    public static void Configure()
    {
        ServiceLocator.Current.RegisterType<ILoginProvider, XmlLoginProvider>();
    }
}
问题回答

你试图从极具功能的标志中抽出。 此外,你们还必须使这名助手能够通过依赖性感受,即你必须给这几类人增加一个警示,把LoginInterface作为理由。 然后,你可以使用像“结构”这样的公用事业,在运行时间接手。

通过建立接口,我们可以为今后可能转向非行或网络服务的情况提供适应性。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签