Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:22:25+00:00 2026-05-30T15:22:25+00:00

first of all here is my situation. I am programming an intranet application using

  • 0

first of all here is my situation. I am programming an intranet application using ASP.NET MVC 3 with Entity Framework 4.1. My application has been developed using the “Unit of Work” and “Repository” design patterns.

How ever in my opinion it should go the way that my application has an unit of work that provides a central access to all the repositories which further provide access to the entities.

Lets say I have a entity called “ProductApprovalDocument” with the properties “id”, “creationDate” and “approvalDecission” stored in the database. Now I want the user to be able to access a PDF file of the document thats shortly described by the entity. Because the files are stored in a central directory on a file server using the URL format “[fileServerDirectoryPath]/[ProductApprovalDocument.id].pdf”, I do not want to save an extra property for that filepath on the database. What I would like to do, is give the entity an extra property called “filepath” that automatically constructs the path with the given information and returns it.

Now the Problem:

I use an interface called FileService to abstract file access from the rest of the application. Now in my case I would have to access the UnitOfWork object out of the entity model, to retrieve the current FileService implementetion and get the preconfigured filepath. I think that’s the totaly wrong way because to me an entity model should only be used as a data container not more or less.

Now the Question:

How do I handle such a situation. I would not like to always set the filepath property through the controller because ist more or less static and therefore could be done somehow automatic by the model.

Edit (final solution):

Thanks to the answer of Andre Loker I gained another point of view to my problem.

  • What was the central target I wanted to reach?
    • I wanted the user to gain access to a file stored on a fileserver.
  • Do I have to provide every displayed entity with the total filepath?
    • No! Think about the principle of MVC! User actions get processed by the controller just in time. You don’t have to provide information untill it really get’s used.

So the solution is just to render all data as usual but instead of displaying a static html link to the files, you have to include an ActionLink to the Controller which calculates the filepath on the fly and automatically redirects the user to the file.

In the View do this:

@Html.ActionLink(Model.ID.ToString(), "ShowProductApprovalDocumentFile", "ProductApprovalDocument", new { ProductApprovalDocumentID = Model.ID }, null)

instead of this:

<a href="@Model.FilePath">@Model.ID</a>

And add an corresponding Action to the controller:

public ActionResult ShowProductApprovalDocumentFile(int ProductApprovalDocumentID )
{
   return Redirect(_unitOfWork.FileService.GetFilePathForProductApprovalDocument(ProductApprovalDocumentID));
}

Thanks to the guys that took the time to give me an answer and special thanks to Andre who lead me to the satisfying answer! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:22:26+00:00Added an answer on May 30, 2026 at 3:22 pm

    If I understand the property correctly, there are several options:

    1) Make the FilePath property use a service locator to find the FileService:

    public string FilePath {
        get {
            FileService fileService = DependencyResolver.Current.GetService<FileService>();
            return fileService.GetFilePathForDocument(this);
        }
    }
    

    While I’m not a hugh fan of static service locators as they make testing more difficult, this could be a viable option. To make it more easily testable you can make the file service locator injectable:

    private static readonly Func<FileService> defaultFileServiceLocator = ()=>DependencyResolver.Current.GetService<FileService>():
    private Func<FileService> fileServiceLocator = defaultFileServiceLocator;
    
    public Func<FileService> FileServiceLocator { 
        get { return fileServiceLocator; }
        set { fileServiceLocator = value ?? defaultFileServiceLocator; }
    }
    

    And then use this in FilePath

    public string FilePath {
        get {
            FileService fileService = fileServiceLocator();
            return fileService.GetFilePathForDocument(this);
        }
    }
    

    This way you can inject your own file service locator during testing.

    2) Explicitly require the FileService when retrieving the file path. Instead of a FilePath property you’d have:

    public string GetFilePath(FileService service){
        service.GetFilePathForDocument(this);
    }
    

    The problem with this is of course that now the caller of GetFilePath needs to have a FileService. This isn’t much of a problem for controllers, because if you use an IoC you can inject a FileService into the controller constructor. This approach is the cleaner one as it doesn’t depend on service locators, but as you see it is slightly more inconvenient for the caller.

    3) Inject the FileService into the document class itself.

    Instead of using a file service locator you’d inject the file service itself when you construct your ProductApprovalDocument. With this approach you can use a simple FilePath property again. The main problem is that this often doesn’t play too well with ORMs, as they often construct the objects using a default constructor and you’d have to somehow hook into the object construction process to inject the dependencies. Also, I’m not a big fan of injection services into domain objects.

    4) You set the FilePath from outside the entity. As you said this should be done somewhat automatically as you don’t want to do it manually every time. This would require some layer through which all entities need to pass which sets up the FilePath property.

    5) Don’t make FilePath a property of ProductApprovalDocument at all. This would be a reasonable choice, too. ProductApprovalDocument doesn’t know anything about its FilePath, so why should it be a property? Its the FileService that calculates the value. You can still have a distinct view model version of ProductApprovalDocument which does have a FilePath property. You’d set the property when you create your view model:

    var model = new ProductApprovalDocumentViewModel();
    mapper.Map(realDocument, model); // map common properties with AutoMapper or so
    model.FilePath = fileService.GetFilePathForDocument(realDocument);
    

    However, if ProductApprovalDocument needs to do something with its FilePath (why would it?) this approach doesn’t work anymore.

    Personally I’d go with solution 5, 2 or 1 in that order of precedence, where applicable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my situation: I have been working on an ASP.NET MVC 3 application for
I've been using a function I made when I first started programming (Or shortly
There is a situation here, I'm developing an Android application, using Java. I'm pretty
first of all this is my third question about web services here and i
I am new here so first of all my greetings to you I am
First of all, I do not believe this belongs on Superuser. This belongs here
Greetings to all! This is my first question here on stackoverflow. I have a
First of all, I know how to build a Java application. But I have
First of all (in case this is important) I'm using ActiveState's Perl (v5.8.7 built
First of all, it's great. However, I came across a situation where my benchmarks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.