Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>About question #2, I would do this using Dependency Injection and set up your service implementation something like this:</p> <pre><code>class MyService : IMyService { public MyService() : this(new UserAuthorization()) { } public MyService(IAuthorization auth) { _auth = auth; } private IAuthorization _auth; public EntityInfo GetEntityInfo(string entityId) { _auth.CheckAccessPermission(PermissionType.GetEntity, user, entityId); //Get the entity info } } </code></pre> <p>Note that IAuthorization is an interface that you would define. </p> <p>Because you are going to be testing the service type directly (that is, without running it inside the WCF hosting framework) you simply set up your service to use a dummy IAuthorization type that allows all calls. However, an even BETTER test is to mock the IAuthorization and test that it is called when and with the parameters that you expect. This allows you to test that your calls to the authorization methods are valid, along with the method itself.</p> <p>Separating the authorization into it's own type also allows you to more easily test that it is correct in isolation. In my (albeit limited) experience, using DI "patterns" give you vastly better separation of concerns and testability in your types as well as leading to a cleaner interface (this is obviously open to debate).</p> <p>My preferred mocking framework is <a href="http://ayende.com/projects/rhino-mocks.aspx" rel="nofollow noreferrer">RhinoMocks</a> which is free and has very nice fluent interface but there are lots of others out there. If you'd like to know more about DI here are some good primers and .Net frameworks:</p> <ul> <li><a href="http://www.martinfowler.com/articles/injection.html" rel="nofollow noreferrer">Martin Fowler on DI</a></li> <li><a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/10/06/132825.aspx" rel="nofollow noreferrer">Jeremy Miller on DI</a></li> <li><a href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx" rel="nofollow noreferrer">Scott Hanselman's List of DI Containers</a></li> <li>My personal favorite DI container: <a href="http://www.castleproject.org/container/index.html" rel="nofollow noreferrer">The Castle Project Windsor Container</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload