Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Best way to initialize master data on application start? #4150


User avatar
0
manojreddy created

Hi, I have some master data in DB which I need throughout my application. So What is the best way to define this data in the application? and Where should I define in the application so that every time applications starts I will initialize this master data in my application. And Where should I define the method which will fetch data from DB?

I'm thinking of having the dictionary in AppConsts.cs file in the Application project


12 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    You can use ICacheManager similar to usage in TestAppService of this document <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Caching">https://aspnetboilerplate.com/Pages/Documents/Caching</a>.

    Instead of an app service, you can create a domain service class and use it in your applicaiton's startup, maybe in your web module.

  • User Avatar
    0
    manojreddy created

    Thanks for your response.

    Could you please give an example of "Instead of an app service, you can create a domain service class and use it in your application's startup, maybe in your web module", So that It would be more clear.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    You can check definition of TaskManager and ITaskManager in this document <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Domain-Services">https://aspnetboilerplate.com/Pages/Doc ... n-Services</a>.

  • User Avatar
    0
    manojreddy created

    Thanks for your response.

    But <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Domain-Services">https://aspnetboilerplate.com/Pages/Doc ... n-Services</a> and <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Caching">https://aspnetboilerplate.com/Pages/Documents/Caching</a> are not clear. Do you have any working example with code?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Then only difference is deriving your class from *DomainServiceBase instead of *AppServiceBase. Other than that, it is same as an App Service.

  • User Avatar
    0
    manojreddy created

    Please provide sample code

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    Here is a sample code for a domain service and it's interface.

    public interface ITaskManager : IDomainService
    {
        void AssignTaskToPerson(Task task, Person person);
    }
    
    public class TaskManager : DomainService, ITaskManager
    {
        public const int MaxActiveTaskCountForAPerson = 3;
    
        private readonly ITaskRepository _taskRepository;
    
        public TaskManager(ITaskRepository taskRepository)
        {
            _taskRepository = taskRepository;
        }
    
        public void AssignTaskToPerson(Task task, Person person)
        {
            if (task.AssignedPersonId == person.Id)
            {
                return;
            }
    
            if (task.State != TaskState.Active)
            {
                throw new ApplicationException("Can not assign a task to a person when task is not active!");
            }
    
            if (HasPersonMaximumAssignedTask(person))
            {
                throw new UserFriendlyException(L("MaxPersonTaskLimitMessage", person.Name));
            }
    
            task.AssignedPersonId = person.Id;
        }
    
        private bool HasPersonMaximumAssignedTask(Person person)
        {
            var assignedTaskCount = _taskRepository.Count(t => t.State == TaskState.Active && t.AssignedPersonId == person.Id);
            return assignedTaskCount >= MaxActiveTaskCountForAPerson;
        }
    }
    
  • User Avatar
    0
    manojreddy created

    Whats next to be done?

    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Application.Services.Dto;
    using Abp.Authorization;
    using Abp.Runtime.Caching;
    using Test.Authorization;
    using Test.Caching.Dto;
    
    namespace Test.Caching
    {
        [AbpAuthorize(AppPermissions.Pages_Administration_Host_Maintenance)]
        public class CachingAppService : TestAppServiceBase, ICachingAppService
        {
            private readonly ICacheManager _cacheManager;
    
            public CachingAppService(ICacheManager cacheManager)
            {
                _cacheManager = cacheManager;
            }
    
            public ListResultDto<CacheDto> GetAllCaches()
            {
                var caches = _cacheManager.GetAllCaches()
                                            .Select(cache => new CacheDto
                                            {
                                                Name = cache.Name
                                            })
                                            .ToList();
    
                return new ListResultDto<CacheDto>(caches);
            }
    
            public async Task ClearCache(EntityDto<string> input)
            {
                var cache = _cacheManager.GetCache(input.Id);
                await cache.ClearAsync();
            }
    
            public async Task ClearAllCaches()
            {
                var caches = _cacheManager.GetAllCaches();
                foreach (var cache in caches)
                {
                    await cache.ClearAsync();
                }
            }
        }
    }
    
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
    	services.AddMemoryCache();
    }
    
  • User Avatar
    0
    manojreddy created

    Despite being a premium member, I'm asking for support on StackOverflow.

    [https://stackoverflow.com/questions/47648086/how-to-use-caching-in-asp-net-boilerplate])

  • User Avatar
    0
    aaron created
    Support Team

    Despite being a premium member, I'm asking for support on StackOverflow.

    Let me try to mediate.

    That's an implementation request using Aspnetboilerplate framework, not a support request nor is it specific to Asp.Net Zero. Note that the team has been helping you with implementation requests and will continue to do so, out of their pure goodwill. Keep in mind though, every hour spent is one hour less development time making everyone's license an even greater bargain.

    That said, the questions on Stack Overflow end up being answered by the Asp.Net Zero team anyway. I also help when I can. Here's what the different platforms are for*:

    • GitHub: Bug reports ("I know something's wrong"), Feature requests ("this would be great for everyone, not just me")
    • Forum: Support requests ("why is it not working"), Implementation requests ("how to use this") specific to Asp.Net Zero
    • Stack Overflow: Implementation requests about Aspnetboilerplate framework, Dependency injection, C# syntax, etc. *Disclaimer: I do not claim to express the views of the Asp.Net Zero team.
  • User Avatar
    0
    manojreddy created

    Thanks for your response, I will keep this in mind :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @aaron,

    Thanks for the clarification :),

    Hi @ManojReddy,

    We want to help you as much as we can but as @aaron pointed out, we cannot share/write code for specific use cases. It is very generous what @aaron is doing on stackoverflow, Thanks again @aaron for your great effor :).

    @ManojReddy, I hope you have solved your problem.