Skip to main content
deleted 47 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

ViewModel Creator what What can I do better in this ViewModel Creator?

I'm currently creating a ASP.NET MVC page in C#.

I want to hide everything regarding the creation of our "models" and "viewmodels".

I have seen much of the fancy stuff regarding Dependency Injection aswellas well as Action Filters, Custom Controllers and so on! But the people I work with are pretty new to this so I want it to keep it dead simple, but I still want to avoid all the new statement and keep the controller really lightweight.

So please adwisePlease advise me if I'm doing this the wrong way, and be constructive I'm asking for advise and help not flaming ;).

Factory Class

 public class Factory
 {
    public T BuildViewModel<T>() where T : class, new()
    {
        IViewModel<T> iVmInterf = (IViewModel<T>)Activator.CreateInstance<T>();

        return iVmInterf.getViewModel();
    }
 }

IViewModel InterFace

 interface IViewModel<T>
 {
    T getViewModel();
 }

interface IProductViewModel

interface IProductViewModel
{
    int ProductCost { get; }
    string ProductName { get; }
}

class ProductViewModel

class ProductViewModel : IViewModel<ProductViewModel>, IProductViewModel
{
    private int _productCost;
    private string _productName;
    private IProductViewModel interf;
    
    int IProductViewModel.ProductCost
    {
        get { return _productCost; }
    }

    string IProductViewModel.ProductName
    {
        get { return _productName; }
    }

    ProductViewModel IViewModel<ProductViewModel>.getViewModel()
    {
        return this;
    }
}

Usage in controller

Factory fact = new Factory();
IProductViewModel vmdl = fact.BuildViewModel<ProductViewModel>(); 

ViewModel Creator what can I do better?

I'm currently creating a ASP.NET MVC page in C#.

I want to hide everything regarding the creation of our "models" and "viewmodels".

I have seen much of the fancy stuff regarding Dependency Injection aswell as Action Filters, Custom Controllers and so on! But the people I work with are pretty new to this so I want it to keep it dead simple, but I still want to avoid all the new statement and keep the controller really lightweight.

So please adwise me if I'm doing this the wrong way, and be constructive I'm asking for advise and help not flaming ;)

Factory Class

 public class Factory
 {
    public T BuildViewModel<T>() where T : class, new()
    {
        IViewModel<T> iVmInterf = (IViewModel<T>)Activator.CreateInstance<T>();

        return iVmInterf.getViewModel();
    }
 }

IViewModel InterFace

 interface IViewModel<T>
 {
    T getViewModel();
 }

interface IProductViewModel

interface IProductViewModel
{
    int ProductCost { get; }
    string ProductName { get; }
}

class ProductViewModel

class ProductViewModel : IViewModel<ProductViewModel>, IProductViewModel
{
    private int _productCost;
    private string _productName;
    private IProductViewModel interf;
    
    int IProductViewModel.ProductCost
    {
        get { return _productCost; }
    }

    string IProductViewModel.ProductName
    {
        get { return _productName; }
    }

    ProductViewModel IViewModel<ProductViewModel>.getViewModel()
    {
        return this;
    }
}

Usage in controller

Factory fact = new Factory();
IProductViewModel vmdl = fact.BuildViewModel<ProductViewModel>(); 

What can I do better in this ViewModel Creator?

I'm currently creating a ASP.NET MVC page in C#.

I want to hide everything regarding the creation of our "models" and "viewmodels".

I have seen much of the fancy stuff regarding Dependency Injection as well as Action Filters, Custom Controllers and so on! But the people I work with are pretty new to this so I want it to keep it dead simple, but I still want to avoid all the new statement and keep the controller really lightweight.

Please advise me if I'm doing this the wrong way, and be constructive.

Factory Class

 public class Factory
 {
    public T BuildViewModel<T>() where T : class, new()
    {
        IViewModel<T> iVmInterf = (IViewModel<T>)Activator.CreateInstance<T>();

        return iVmInterf.getViewModel();
    }
 }

IViewModel InterFace

 interface IViewModel<T>
 {
    T getViewModel();
 }

interface IProductViewModel

interface IProductViewModel
{
    int ProductCost { get; }
    string ProductName { get; }
}

class ProductViewModel

class ProductViewModel : IViewModel<ProductViewModel>, IProductViewModel
{
    private int _productCost;
    private string _productName;
    private IProductViewModel interf;
    
    int IProductViewModel.ProductCost
    {
        get { return _productCost; }
    }

    string IProductViewModel.ProductName
    {
        get { return _productName; }
    }

    ProductViewModel IViewModel<ProductViewModel>.getViewModel()
    {
        return this;
    }
}

Usage in controller

Factory fact = new Factory();
IProductViewModel vmdl = fact.BuildViewModel<ProductViewModel>(); 
Source Link

ViewModel Creator what can I do better?

I'm currently creating a ASP.NET MVC page in C#.

I want to hide everything regarding the creation of our "models" and "viewmodels".

I have seen much of the fancy stuff regarding Dependency Injection aswell as Action Filters, Custom Controllers and so on! But the people I work with are pretty new to this so I want it to keep it dead simple, but I still want to avoid all the new statement and keep the controller really lightweight.

So please adwise me if I'm doing this the wrong way, and be constructive I'm asking for advise and help not flaming ;)

Factory Class

 public class Factory
 {
    public T BuildViewModel<T>() where T : class, new()
    {
        IViewModel<T> iVmInterf = (IViewModel<T>)Activator.CreateInstance<T>();

        return iVmInterf.getViewModel();
    }
 }

IViewModel InterFace

 interface IViewModel<T>
 {
    T getViewModel();
 }

interface IProductViewModel

interface IProductViewModel
{
    int ProductCost { get; }
    string ProductName { get; }
}

class ProductViewModel

class ProductViewModel : IViewModel<ProductViewModel>, IProductViewModel
{
    private int _productCost;
    private string _productName;
    private IProductViewModel interf;
    
    int IProductViewModel.ProductCost
    {
        get { return _productCost; }
    }

    string IProductViewModel.ProductName
    {
        get { return _productName; }
    }

    ProductViewModel IViewModel<ProductViewModel>.getViewModel()
    {
        return this;
    }
}

Usage in controller

Factory fact = new Factory();
IProductViewModel vmdl = fact.BuildViewModel<ProductViewModel>();