Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Which layer is the best to add a new method?
Message
 
 
General information
Forum:
ASP.NET
Category:
Class design
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01619738
Message ID:
01621918
Views:
48
>Another alternative (maybe better) would be an ActionFilterAttribute which could be added to applicable methods :
>
>https://msdn.microsoft.com/en-us/library/system.web.http.filters.actionfilterattribute%28v=vs.118%29.aspx

Do you have samples, Viv?

I found this

http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

but I don't understand how to access the model's properties in that filter.

Say, this is my Update method in the controller (quite complicated in this particular case)
[Route("")]
        [HttpPut]
        public IHttpActionResult UpdateAccount(EditAccountViewModel accountViewModel)
        {
            // Update Contacts
            _contactsAdapter.UpdateContactsAndChildren(accountViewModel.ContactPerson);

            Accounts account = AutoMapper.Mapper.Map<EditAccountViewModel, Accounts>(accountViewModel);
            // The web page assigns false/true or 0/1, converting here.
		    if (account.WebITtype == 0)
		    {
		        account.WebITtype = 2;
		    }
		    else
		    {
		        account.WebITtype = 1;
		    }
            List<DualListViewModel> currentSecAcct = (from secAcct in _secAcctAdapter.GetAll()
                                                      join secRoles in _secRoleAdapter.GetAllRoles()
                                                      on secAcct.RoleNo equals secRoles.RoleNo
                                                      where secAcct.AcctName == accountViewModel.AcctName
                                                      select new DualListViewModel
                                                      {
                                                          Descrip = secRoles.Descrip,
                                                          ItemId = secAcct.PriKey
                                                      }).ToList();

            if (accountViewModel.AssignedRoles.Any())
            {
                if (currentSecAcct.Any())
                {
                    List<DualListViewModel> newSecRoles = accountViewModel.AssignedRoles.Except(currentSecAcct.AsEnumerable()).ToList();
                    List<DualListViewModel> removedSecRoles = currentSecAcct.Except(accountViewModel.AssignedRoles.AsEnumerable()).ToList();
                    foreach (var role in newSecRoles)
                    {
                        SecAcct secAcct = new SecAcct();
                        secAcct.RoleNo = role.ItemId;
                        secAcct.AcctName = accountViewModel.AcctName;
                        _secAcctAdapter.Add(secAcct);
                    }

                    foreach (var role in removedSecRoles)
                    {
                        _secAcctAdapter.Delete(role.ItemId);
                    }
                }
                else
                {
                    foreach (var role in accountViewModel.AssignedRoles)
                    {
                        SecAcct secAcct = new SecAcct();
                        secAcct.RoleNo = role.ItemId;
                        secAcct.AcctName = accountViewModel.AcctName;
                        _secAcctAdapter.Add(secAcct);
                    }
                }
            }
            else if (currentSecAcct.Any())
            {
                foreach (var roles in currentSecAcct)
                {
                    _secAcctAdapter.Delete(roles.ItemId);
                }
            }

            SetOperatorAndSalespoint(account);
            // Update contactId
            var contatId = accountViewModel.ContactPerson.ContactId;
            account.ContactId = contatId;

            _accountsAdapter.Update(account);
            
            accountViewModel = AutoMapper.Mapper.Map<Accounts, EditAccountViewModel>(account);

            // Update contact with latest information
            accountViewModel.ContactPerson = _contactsAdapter.GetContactsAndChildrenById(contatId);

            return Ok(accountViewModel);
        }
The SetOperatorAndSalespoint(account) is what I'd like to make a filter if I am going to go with the filter route. But the question is - how from the filter I can access the accountViewModel (any view model) and set the properties of it (and these properties are only in a few of the models, not in every model).

So, I still don't understand what I am supposed to do here, although filter's approach looks interesting.

I was also thinking of the simplest approach (for me). Create a new layer ViewModel class based on our abstract BaseEditViewModel class. This class will have these extra properties (Salespoint, Operator, DateTime, ModSp, ModOp). Have methods to update these properties. Re-point models that will have these properties to that class instead of just the BaseEditViewModel class. And then just call the method of this new class in the Save method.

This approach looks the simplest to me and I would know how to implement it. Do you think it's an OK route to take with the extra layer class? I think having extra properties in the ViewModel even if they are not used in the model class is OK and should not be a problem.

Thanks again.

UPDATE. Found this http://stackoverflow.com/questions/872796/asp-net-mvc-accessing-view-model-from-a-custom-action-filter which may be a way to go.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform