Entity framework can't add with singleton - c#

My persons class:
public class User
{
public int Id { get; private set; }
public string Isim { get; set; }
public string Soyad { get; set; }
public User(string isim, string soyad)
{
Isim = isim;
Soyad = soyad;
}
}
My UserBusiness class:
public sealed class UserBusiness
{
JuqueryDbEntities entity = new JuqueryDbEntities();
private static volatile UserBusiness instance;
private static readonly object syncRoot = new Object();
private UserBusiness() { }
public static UserBusiness Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new UserBusiness();
}
}
return instance;
}
}
public void AddUser(User userToAdd)
{
entity.PersonelTable.Add(userToAdd);
entity.SaveChanges();
}
}
And lastly my webform codebehind onclick of a login button:
protected void Button1_Click(object sender, EventArgs e)
{
string isim = TextBox1.Text;
string soyad = TextBox2.Text;
var newUser = new User(isim, soyad);
UserBusiness.Instance.AddUser(newUser);
}
Here is my problem: I get an error in AddUser method of my UserBusiness class.
An error in 'entity.PersonelTable.Add(newUser);' line says
'The best overload method match for 'System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)' has some invalid arguments.' What am I doing wrong(Btw my Id is autoincrement so I am not setting it any value.)

your problem here is that the method Add of System.Data.Entity.DbSet does simply not take argument of type user.
like mentioned in the error
System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)' has some
invalid arguments
it takes objects of type SuperQquery.PersonelTable
So what you need to do is change AddUser method
public void AddUser(User userToAdd)
{
SuperQquery.PersonelTable pt = new SuperQquery.PersonelTable();
pt.FieldName1 = userToAdd.Isim;
pt.FieldName2 = userToAdd.Soyad;
entity.PersonelTable.Add(pt);
entity.SaveChanges();
}
where FieldName are names of columns in the table of the database
EDIT: if this works, we can say that you made a conversion of type user to type SuperQquery.PersonelTable the way you can insert data in database, but the best way here is to change the class User by a new class PersonelTable with the same logic. No need to do a conversion.

Related

How to get data from an Object from another Class?

Hey I have a problem with my code.
Sorry if the question is too easy but I can't find a solution.
I have an object called user in class 1
The Object User has this variables.
public class User
{
public string email { get; set; }
public string password { get; set; }
public string mobilenumber { get; set; }
public string service { get; set; }
public override string ToString()
{
return $"{email}: {password}: {mobilenumber}: {service}";
}
}
These variables are filled with data in class 1
Now I want to access these data in class 2 and display them to me.
I tried something like
Class Firstclass{
public void OnLogin(){
public User user = new User();
user.email = abc#abc.com
}
}
Class B{
protected override async Task OnInitializedAsync(){
Firstclass firstclass = new Firstclass();
string output = firstclass.user.email;
}
}
There are several ways to do that. A simple one would be to instantiate the User inside the constructor:
public Class Firstclass
{
User user;
public FirstClass()
{
this.user = new User();
this.user.email = "abc#abc.com";
// more data could be here or use a local method to fill user's fields
}
public User
{
get
{
return this.user;
}
set
{
this.user = value;
}
}
}
Try to avoid using a public field, rather use a public property like User in above. Then in Class B, you would have:
Class B
{
protected override async Task OnInitializedAsync()
{
Firstclass firstclass = new Firstclass();
string output = firstclass.User.email;
}
}

Change business layer class to be loosely coupled

I have below code I am working on. I am working on data access adon.net layer. I have problem with my business layer class called UserBAL. The problem is that I am creating instance of dal and dbmanager inside constructor of UserBAL. How can I change this to be loosely coupled for UserBAL? Hope you get my point.
public interface IEntity
{
int Id { get; set; }
int DoSomething(string one, int two);
}
public class User : IEntity
{
public int Id { get; set; }
public int DoSomething(string one, int two)
{
throw new NotImplementedException();
}
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class UserBal //busines logic
{
private readonly IRepositoryDal<User> _userRepositoryDal;
public UserBal()
{
_userRepositoryDal = new UserRepositoryDal(new DbManager("sqlserver?"));
}
public IEnumerable<User> SearchByName(string name)
{
return _userRepositoryDal.SearchByName(name);
}
}
interface IRepositoryDal<T> where T : IEntity
{
IEnumerable<T> SearchByName(string username);
T SearchById(string id);
void Update(T entity);
void Remove(T entity);
void Add(T entity);
}
public class UserRepositoryDal: IRepositoryDal<User>
{
private readonly IDbManager _dbManager;
public UserRepositoryDal(IDbManager dbManager)
{
//read from either singleton or configuration file !!
_dbManager = dbManager;
}
public IEnumerable<User> SearchByName(string username)
{
var parameters = new List<IDbDataParameter>
{
_dbManager.CreateParameter("#FirstName", 50, username, DbType.String),
};
var userDataTable = _dbManager.GetDataTable("storedpr2",
CommandType.StoredProcedure, parameters.ToArray());
foreach (DataRow dr in userDataTable.Rows)
{
var user = new User
{
Id = int.Parse(dr["Id"].ToString()),
Firstname = dr["Firstname"].ToString(),
Lastname = dr["LastName"].ToString(),
Email = dr["Email"].ToString()
};
yield return user;
}
}
public User SearchById(string id)
{
var parameters = new List<IDbDataParameter>
{
_dbManager.CreateParameter("#Id", 50, id, DbType.Int32),
};
var userDataTable = _dbManager.GetDataTable("storedpr2",
CommandType.StoredProcedure, parameters.ToArray());
return new User
{
Id = int.Parse(userDataTable.Rows[0]["Id"].ToString()),
Firstname = userDataTable.Rows[0]["Firstname"].ToString(),
Lastname = userDataTable.Rows[0]["LastName"].ToString(),
Email = userDataTable.Rows[0]["Email"].ToString()
};
}
public void Update(User entity)
{
throw new System.NotImplementedException();
}
public void Remove(User entity)
{
throw new System.NotImplementedException();
}
public void Add(User entity)
{
throw new System.NotImplementedException();
}
}
public partial class FrmLogin : Form
{
private readonly UserBal _userBal;
public FrmLogin()
{
InitializeComponent();
_userBal = new UserBal();
}
}
You should use dependency injection, and for required dependencies, you can use constructor injection, e.g:
public class UserBal
{
private readonly IRepositoryDal<User> _userRepositoryDal;
public UserBal(IRepositoryDal<User> userRepositoryDal)
{
_userRepositoryDal = userRepositoryDal
?? throw new ArgumentNullException(nameof(userRepositoryDal));
}
...
}
Dependency injection is the way to go. Here's a simplified example of your situation.
Given your classes could be like this:
public interface IEntity { }
public interface IRepositoryDal<T> where T : IEntity { }
public interface IDbManager { }
public class User : IEntity { }
public class UserBal //busines logic
{
[Injectivity.Attributes.Inject]
private IRepositoryDal<User> _userRepositoryDal;
}
public class UserRepositoryDal: IRepositoryDal<User>
{
[Injectivity.Attributes.Inject]
private IDbManager _dbManager;
}
public class DbManager : IDbManager
{
[Injectivity.Attributes.Construct()]
public DbManager([Injectivity.Attributes.Key("dbKey", typeof(string))] string x)
{
Console.WriteLine($"DbManager created with parameter \"{x}\"");
}
}
...then this code:
var context = Injectivity.Context.CreateRoot();
context.SetConfig<string>("dbKey", "sqlserver?");
context.SetFactory<IDbManager, DbManager>();
context.SetFactory<IRepositoryDal<User>, UserRepositoryDal>();
context.SetFactory<UserBal, UserBal>();
var user = context.Resolve<UserBal>();
...will produce this output:
DbManager created with parameter "sqlserver?"
Now, I've used a DI container that I wrote a number of years back. It's very flexible.
So the call to context.Resolve<UserBal>(); runs down the chain of seeing what needs to be injected to create all of the instances of all of the objects. Ultimately the creation of UserBal requires the DbManager to be created and hence the output.
You would normally not explicitly register each factory. Normally you would put attributes on all of the classes you want to register and then use context.Register(Assembly.LoadFrom("My.DLL")); or create an XML config file and call context.LoadConfig(XDocument.Load("config.xml"));.
You can even do things like this:
context.SetDecorator<IRepositoryDal<User>, UserRepositoryDalDecorator>();
This will cause all calls to context.Resolve<IRepositoryDal<User>>() or [Inject] attributes to automatically wrap the real instances in this decorator. Ideal to intercepting method calls for debugging purposes.

Static Nested Type

So i have nested type classes that go something like this:
namespace MyNamespace
{
public class User
{
private int id = 0;
private string userId = "";
private string userPassword = "";
public int Id
{
get { return id; }
set { id = value; }
}
public string UserId
{
get { return userId; }
set { userId = value; }
}
public string UserPassword
{
get { return userPassword; }
set { userPassword = value; }
}
public User()
{
id = 0;
userId = "";
userPassword = "";
}
public static class SignonInfo
{
private static string sesstoken = "";
private static DateTime sessstart = new DateTime();
public static string SessToken
{
get { return sesstoken; }
set { sesstoken = value; }
}
public static DateTime SessStart
{
get { return sessstart; }
set { sessstart = value; }
}
}
}
}
My end goal here was to be able to access the nested static class like this:
User user = new User();
string token = user.SignonInfo.SessToken;
I'm trying to avoid instantiating the class like this:
User.SignonInfo user = new User.SignonInfo()
I need to be able to access properties of both User and SignonInfo classes.
Could someone help me to get on track or slap me about and tell me i'm doing it all wrong?
TIA
The problem is that it's a static class, so an "instance" doesn't have access to it. This is a good thing, as it prevents global state from masquerading as a well-encapsulated object.
I'd suggest making the nested class non-static, and having theUser class create an instance as needed by the caller (maybe add a public SignonInfo GetSignonInfo() method.)
You are trying to access your nested class as if it were a member of the instance of the outer class.
Change
string token = user.SignonInfo.SessToken;
to
User.SignonInfo.SessToken;
Note, that you do not get an instance of the static nested type per outer instance, there is only one for the entire outer class.

Using singleton asp.net c#

I am new in programming and trying to learn singleton but stuck in somewhere.
Here is my user class:
public class User
{
private static User user;
private User()
{
}
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string isim;
public string Isim
{
get { return isim; }
set { isim = value; }
}
private string soyad;
public string Soyad
{
get { return soyad; }
set { soyad = value; }
}
public static User CreateUser()
{
if (user == null)
user = new User();
return user;
}
}
In my web form I tried this:
User myuser = User.CreateUser();
to create an object but it gives me an error like there is nothing as CreateUser()..What am I doing wrong
All you have to do is like following:
You have to modify The User class to make it like this
public class User
{
public int Id { get; set; }
public string Isim { get; set; }
public string Soyad { get; set; }
public class User(){}
public User(int id, string isim, string soyad)
{
Id = id;
Isim = isim;
Soyad = soyad;
}
}
Then you implement Singleton (user buisiness logic) class like this
using System;
public sealed class UserBusiness
{
private static volatile UserBusiness instance;
private static readonly object syncRoot = new Object();
private UserBusiness() { }
public static UserBusiness Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new UserBusiness();
}
}
return instance;
}
}
public void AddUser(User userToAdd)
{
//TODO use your ORM or whatever to acces database and add the user
//for example if you use entityFramework you will need to do
//Context.Customers.Add(user)
//Context.SaveChanges();
//Just For Example
}
}
There are many implementation of singleton like mentioned in this MSDN article
and finally in your webForm Code you put the following :
var newUser = new User(1, "user1Isim", "user1Soyad");
UserBusiness.Instance.AddUser(newUser);
After All, there are many ways to do this, depends on your needs. I found this as the simpliest way to explain.

Help with Singleton - Setting Auto Properties?

I'm trying to set my Auto Properties but they are non-static and so I get the error "Cannot access non-static property in static context" when trying to set the properties Credentials, Certificate, and UrlEndPoint.
public class PayPalProfile
{
#region Fields
static PayPalProfile _instance;
#endregion
#region Constructors
PayPalProfile()
{
// is only called if a new instance is created
SetProfileState();
}
#endregion
#region Properties
public static PayPalProfile CurrentProfile
{
get
{
if (_instance == null)
_instance = new PayPalProfile();
return _instance;
}
}
public CustomSecurityHeaderType Credentials { get; private set; }
public X509Certificate2 Certificate { get; private set; }
public string UrlEndPoint { get; private set;}
#endregion
#region Methods
private static void SetProfileState()
{
// Set the profile state
SetApiCredentials();
SetPayPalX509Certificate();
}
private static void SetApiCredentials()
{
Credentials = new CustomSecurityHeaderType
{
Credentials =
{
Username = PayPalConfig.CurrentConfiguration.ApiUserName,
Password = PayPalConfig.CurrentConfiguration.ApiPassword
}
};
UrlEndPoint = PayPalConfig.CurrentConfiguration.ExpressCheckoutSoapApiEndPoint;
}
private static void SetPayPalX509Certificate()
{
PayPalCerfiticate paypalCertificate = new PayPalCerfiticate();
Certificate = paypalCertificate.PayPalX509Certificate;
}
#endregion
}
There is no need for SetProfileState, SetApiCredentials and SetPayPalX509Certificate to be static.
SetApiCredentials and SetPayPalX509Certificate are setting values for non static properties and so an instance is required. By removing the static modifiers from the above mentioned methods the properties will be set on the instance being constructed when SetProfileState is called.
This means that you have a static method where you are trying to assign instance properties. As there is no instance available in static methods/properties, the error is given.
The example:
public class Test {
public int InstanceProperty { get; set; }
public static void StaticMethod() {
InstanceProperty = 55; // ERROR HERE
}
}
Instead bothe should either be in static or instance context:
public class Test {
public static int StaticProperty { get; set; }
public static void StaticMethod() {
StaticProperty = 55; // Ok
}
}
public class Test {
public int InstanceProperty { get; set; }
public void InstanceMethod() {
InstanceProperty = 55; // Ok
}
}

Categories