MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

How to retrieve multiple values to Grid Angular 5 + .NET WEB API #531

Open Vin1206 opened 6 years ago

Vin1206 commented 6 years ago

Hi All,

I am totally new to CSLA.NET framework. i have developed a sample application which needs to retrieve List to the grid and the user have permission to do the multiple edits in the grid and finally can save. The issue i am facing is i am not able to retrieve the multiple values, please find below my code, please let me know what i am missing. Any help is much appreciated

 [Serializable]
    public class LineOfList : ReadOnlyListBase<LineOfList, LobInfo>
 public LineOfList Get()
        {
            var returnValue = DataPortal.Fetch<LineOfList>();

            return returnValue;

        }

        private void DataPortal_Fetch()
        {
            List<LOBDto> response = new List<LOBDto>();

        var list = lobRepository.GetAll().Select(x => new LobInfo { LineOfId = x.LineOfId, LoName = x.LoName }).ToList();

        }
}
rockfordlhotka commented 6 years ago

If you are new to CSLA, you should read the first 4 books of the Using CSLA 4 series so you understand the basics.

Your code has a couple issues.

First, your Get method is returning a new instance of a business object, and so it should typically be static. Otherwise you create an instance of LineOfList just to call Get, and then you'd need to throw away that first instance - that's confusing.

Second, the DataPortal_Fetch method is running inside an instance of the business object, and it is responsible for loading that existing instance with data. Your code retrieves the data, but never puts it into the existing instance.

This is probably closer to what you need:

 [Serializable]
 public class LineOfList : ReadOnlyListBase<LineOfList, LobInfo>
 {
    public static LineOfList Get()
    {
        var returnValue = DataPortal.Fetch<LineOfList>();
        return returnValue;
    }

    private void DataPortal_Fetch()
    {
        List<LOBDto> response = new List<LOBDto>();
        var list = lobRepository.GetAll().Select(x => new LobInfo { LineOfId = x.LineOfId, LoName = x.LoName }).ToList();
        this.AddRange(list);
    }
}
Vin1206 commented 6 years ago

Thank you for your response. I have already the option whatever you mentioned and learnt this exception "{"Can not change a read-only list or collection"}".

The reason why i did not use Static is i am using Dependency injection which injects the instance during the app load. Please let me know if that will not work. Also, please let me know whether you have any Video tutorial explaining CSLA.NET completely. I went through the overview book alone.

Vin1206 commented 6 years ago

i solved that issue by putting these code in the DataPortal_Fetch var rlce = RaiseListChangedEvents; RaiseListChangedEvents = false; IsReadOnly = false;

rockfordlhotka commented 6 years ago

There's not a current video series. The most current content is the Using CSLA 4 ebook.

With Dependency Injection, the question is always what are you trying to gain by using that design pattern, and how does it work within the frameworks you've selected (both IoC like autofac, and any MVVM or other UI framework).

You can not use DI to create a business domain object. You must use the data portal. This is because the data portal knows how to talk to application servers, and DI does not. Literally, the DI pattern has no way to cross network boundaries.

The primary goal of using DI is to allow unit testing that relies on mocks. You can use DI to create an instance of the data portal, which allows you to create a mock data portal. Your mock data portal can return a mock business object, because business objects can also be interface-based.

@JasonBock has written about using DI with CSLA: https://magenic.com/thinking/abstractions-in-csla

Vin1206 commented 6 years ago

Hello,

I have sent a request asking your opinion to know how I implemented dependency injection. Request your option please.

On Fri, May 4, 2018, 10:27 AM Rockford Lhotka notifications@github.com wrote:

If you are new to CSLA, you should read the first 4 books of the Using CSLA 4 series so you understand the basics.

Your code has a couple issues.

First, your Get method is returning a new instance of a business object, and so it should typically be static. Otherwise you create an instance of LineOfList just to call Get, and then you'd need to throw away that first instance - that's confusing.

Second, the DataPortal_Fetch method is running inside an instance of the business object, and it is responsible for loading that existing instance with data. Your code retrieves the data, but never puts it into the existing instance.

This is probably closer to what you need:

[Serializable] public class LineOfList : ReadOnlyListBase<LineOfList, LobInfo> { public static LineOfList Get() { var returnValue = DataPortal.Fetch(); return returnValue; }

private void DataPortal_Fetch()
{
    List<LOBDto> response = new List<LOBDto>();
    var list = lobRepository.GetAll().Select(x => new LobInfo { LineOfId = x.LineOfId, LoName = x.LoName }).ToList();
    this.AddRange(list);
}

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MarimerLLC/cslaforum/issues/531#issuecomment-386670835, or mute the thread https://github.com/notifications/unsubscribe-auth/Agw2RCEx5OhhmJRKeu4lV4BG-3Zit7Pmks5tvI9lgaJpZM4Ty_LG .

Vin1206 commented 6 years ago

namespace BusinessLogicLayer { [Serializable] public class LineOfBusinessList : ReadOnlyListBase<LineOfBusinessList, LobInfo>, ILineOfBusiness {

    public static IRepository<ICD_LineOfBusiness> lobRepository { get; set; }
    public LineOfBusinessList(IRepository<ICD_LineOfBusiness> _lobRepository)
    {
        lobRepository = _lobRepository;

    }
    //public static readonly PropertyInfo<LobInfo> ResultProperty = RegisterProperty<LobInfo>(c => c.Result);
    //public LobInfo Result
    //{
    //    get { return ReadProperty(ResultProperty); }
    //    private set { LoadProperty(ResultProperty, value); }
    //}

    public LineOfBusinessList GetLineOfBusiness()
    {
        var returnValue = DataPortal.Fetch<LineOfBusinessList>();
        return returnValue;
    }

    private void DataPortal_Fetch()
    {
        var rlce = RaiseListChangedEvents;
        //RaiseListChangedEvents = false;
        List<LobInfo> response = new List<LobInfo>();
        IsReadOnly = false;
        var list = lobRepository.GetAll().Select(x => new LobInfo { LineOfBusinessId = x.LineOfBusinessId, LoBName = x.LoBName }).ToList();

        this.AddRange(list);
        // this.AddRange(list);
    }

    public LineOfBusinessList()
    {
        // this.AllowNew = true;
    }
}

}